Find last modified file in all subfolders

1.7k views Asked by At

I need a Windows batch code to find the last modified file among all the files in all the subfolders of a specific folder.

It doesn't seem to be so simple as it appears.

Some files could have been modified in the same minute, but in different seconds. I need only the lastest one.

1

There are 1 answers

1
MC ND On BEST ANSWER
@echo on
    setlocal enableextensions disabledelayedexpansion

    set "root=c:\somewhere"

    for %%r in ("%root%\.") do for /f "tokens=3,*" %%a in ('
        robocopy "%%~fr." "%%~fr." /l /nocopy /s /is /njh /njs /ndl /nc /ns /ts 
        ^| sort /r 
        ^| findstr /n "^"
        ^| findstr /l /b /c:"1:"
    ') do echo %%b

This code uses robocopy (native for Vista and later OS versions, downloadable from Microsoft for XP or 2003) to obtain a list of files with timestamp in yyyy-mm-dd hh:nn:ss format, that is sorted in decreasing order and then only the first line (that is, the newer file) is retrieved.