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
|
as a delimiter? Windows file names cannot contain them so you should be able to use"Delims="
."delims="
or"tokens=*"
does not change the result.dir
command will sort the files in each directory by the date. It will not sort all the files in date order. To do that you would need to output the file date as YYYYMMDD with the file name and sort the file.