Search code examples
windowsbatch-filelast-modified

Copy all files except the last modified file to a new directory


I need to move files from c:\prueba1 to c:\prueba99 but I don't know how to make a comparison betweeen all files in the source directory (c:\prueba99) to move all files in the directory with the exception of the last modified file in the directory. I know there is a wmic command with get InstallDate, LastModified, but I don't know the ms-dos syntaxis to asign a variable and compare it to know that one file readed is the last modified

I found an example:

for /f "delims=" %%A in ('wmic datafile where "drive = 'c:' and path='\\windows\\'"
   get LastModified^,Name /format:table^|find ":"^|sort /r') do @echo %%A

And tried to modify it with no result because it appears to just list the datafile names but not the files themselves.

This is my modified version:

for /f "skip=1 delims=" %%A  in ('wmic datafile where "drive = 'c:' and path='\\prueba1\\'"
    get LastModified^,Name /format:table^|find ":"^| sort/r') do move (%%A) c:\prueba99

Solution

  • for /f "skip=1 delims=" %%a in ('dir /b /tw /o-d /a-d c:\prueba1\*.*'
    ) do move "c:\prueba1\%%a" "c:\prueba99"
    

    dir command get the files in descending creation date order, so the first is the latest. for command iterates over this list, skipping the first one, moving the files to the target folder.