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