alexleen
alexleen

Reputation: 145

Windows 'dir' command: sort files by date when '/s' is specified

Goal: I want to copy the latest file with a certain extension from a "source directory" to a "destination directory" using a batch file. The latest file may be under several sub-directories within the source directory.

This question/answer is exactly what I want, however it does not seem to sort when the /s option is specified (as this comment would suggest):

FOR /F "delims=|" %%I IN ('DIR "K:\path\tp\source\dir\*.ext" /B /S /O:D') DO SET NewestFile=%%I
copy "%NewestFile%" "C:\path\to\destination\dir"

You can test DIR "K:\path\tp\source\dir\*.ext" /B /S /O:D by itself to see that it does not sort.

What I've Tried: This command by itself does work: DIR "K:\path\tp\source\dir\*.ext" /S /B | sort but I can't figure out how to use it in a for loop (batch file exits before I can determine the error - even with a pause at the end).

Any ideas?

See: dir docs

Upvotes: 6

Views: 55349

Answers (3)

MC ND
MC ND

Reputation: 70923

A simple solution could be

@echo off
    setlocal enableextensions disabledelayedexpansion

    set "root=K:\path\tp\source\dir"
    set "mask=*.ext"

    for %%r in ("%root%\.") do for /f "tokens=2,*" %%a in ('
        robocopy "%%~fr" "%%~fr" "%mask%" /njh /njs /nc /ns /ts /s /ndl /nocopy /is /r:0 /w:0 /l
        ^| sort /r 
        ^| cmd /v /e /c"(set /p .=&echo(!.!)"
    ') do set "lastFile=%%b"

    echo Last file : "%lastFile%"

This code uses robocopy to generate the list of files with a timestamp prefix (the switches just request no job header, no job summary, no file class, no file size, timestamp, recursive, no directory list, no directory information copy, include same files, no retry, no wait, don't copy only generate list).

This timestamp prefixed list (UTF yyyy/mm/dd hh:mm:ss last file write) is then sorted in reverse order to get the last file in the first line. This line is retrieved with a separate cmd instance (this avoids a time problem with a for /f reading long lists of data) so the for /f only reads one line.

As the robocopy lines contain the date, time and file name, to retrieve this last field, we request the for /f to retrieve two tokens: one containing the hour (will be stored in %%a) and the remaining text until the end of the line (stored in %%b)

The additional for %%r is included just to prevent a usual problem using robocopy. As we are quoting paths to prevent problems with spaces, we need to ensure the paths do not end with a backslash that will escape the path's closing quote and make the command fail.

Upvotes: 2

Squashman
Squashman

Reputation: 14290

You can call out to Powershell from you batch file if you want to. This code can be shortened on newer versions of Powershell.

for /F "delims=" %%G IN ('powershell "gci -rec | where { ! $_.PSIsContainer } | sort LastWriteTime | select -last 1 | Select-Object fullname"') do set NewestFile=%%G

Full answer:

cd /d "K:\path\to\source\dir"
for /F "delims=" %%G IN ('powershell "gci -rec *.ext | where { ! $_.PSIsContainer } | sort LastWriteTime | select -last 1 | Select-Object fullname"') do set NewestFile=%%G
copy "%NewestFile%" "C:\path\to\dest\dir"

Upvotes: 6

John Kens
John Kens

Reputation: 1679

Everything seems to be working fine. I believe you problem lies within the "delims=|"statement from your original function. By using a "delims=" all it would be doing is cutting off the first | from the loops output. Being that | would not be in a normal windows file path, it's completely not needed. Try just throwing an "tokens=*" in the statement instead.

I also recommend using Set "String=" Instead of the traditional Set String= as spaces can be a problem in many cases. I updated that in the function.

Batch Script:

@ECHO OFF

Rem | Configuration
Set "MainDirectory=C:\Folder1"
Set "FileExtension=*.txt"
Set "CopyDestination=C:\Folder2"

Rem | Search for the newest file in a directory/sub-directory
for /f "tokens=*" %%A in ('DIR "%MainDirectory%\%FileExtension%" /B /S /O:D') do (SET "NewestFile=%%A")

Rem | Copy file to a destination
copy "%NewestFile%" "%CopyDestination%"

goto :EOF

Upvotes: 1

Related Questions