Click here for important information about

Batch file - copy last modified?

January 28, 2010 at 21:13:59
Specs: Windows 7
So I'm attempting to copy the last file modified in a directory and I'm having trouble. I refered to a post made about 2 years ago, and while it appeared to be valid code it did not work in W7. My exact code is:

pushd "%kindle%:\documents"
for /f "tokens=*" %%a in ('dir /b /o-d') do (
copy %%a %dir%\Kindle\Temp
copy %%a %dir%\Kindle\Temp2
goto drm)

The above code does not work. HOWEVER, when removing the "-" from 'dir /b /o-d' it does copy the oldest file into the specified directory.

See More: Batch file - copy last modified?

January 28, 2010 at 21:43:14
simplest way just do minor surgery on the loop to use ascending dates (although slight time-penalty involved):

for /f "tokens=*" %%a in ('dir /b /od') do set xx=%%a
copy %xx% %dir%\Kindle\Temp
copy %xx% %dir%\Kindle\Temp2

should get the last ie most recent dated file.

Report •

January 28, 2010 at 22:49:10
Since it works with /od but not /o-d I guess the newest file has a LFN.


copy "%%a"

Helping others achieve escape felicity


Report •

January 28, 2010 at 23:17:06
What is LFN? And wouldn't a simple copy work only if the Windows settings automatically listed files from newest to oldest?

Report •

Related Solutions

January 28, 2010 at 23:45:04
lfn ~sigh~ when, o lord, when, will these not be biting us on the ass. Thankyou, Bill.
(edit: apologies to N4ru if it looked like i was referring to your post in the above. i was referring to long filenames (with spaces), not you or your query! reading it today it looks that way so i wanted to clarify my stupid remarks.
rant/rave alert!
lfn: Long File Name, ie: open season to wreck any/all code that tries to be logical and consistant.

ever wondered why dir /b only gives lfn, and dir /x gives shortnames but also all the crap? he MADE his programmers
make these two options mutually exclusive.
planned obsolescence, engineered self-destruction, punishment for being "nonmainstream". All so we can have "spaces" in our filenames. what was wrong with _? oh well... sorry.

yes, put quotes around it (var. that is filename). hopefully that fixes and lets Bill rest comfortably. ;)

ps, i hope the quotes made it work.

Report •

Ask Question