Batch File- Only Copy most recent file

July 1, 2010 at 20:30:47
Specs: Windows XP, 1.86 GB

Hello- First off...this is a great site and it has helped me quite a bit. I am in need of a batch file that finds the most recent file out on a shared drive...and then copies it to my desktop. I have similar requests for this on your site....and have tried using the code from several examples....but so far nothing has worked for me. This is my error: The filename, directory name, or volume label syntax is incorrect. The system cannot find the path specified.

Below is an example of code I am currently trying:

@echo off
setlocal
set srcDir= "O:\Historical_AOT\"
set destdir= "O:\Test\"
set lastmod=
pushd "%srcDir%"
for /f "tokens=*" %%a in ('dir /b /od 2^>NUL') do set lastmod=%%a
if "%lastmod%"=="" echo Could not locate files.&goto :eof
copy "%lastLog%" "%destDir%"
pause

Any thoughts/suggestions would be appreciated.

Thanks

Frank Sweeney


See More: Batch File- Only Copy most recent file

Report •


#1
July 1, 2010 at 20:43:40

var. %lastlog% has not been defined. You prob'ly wanted %lastmod%.
also, i think you want:
for /f "tokens=*" %%a in ('dir /b /o-d 2^>NUL') do set lastmod=%%a
if "%lastmod%"=="" echo Could not locate files.&goto :eof
copy "%lastmod%" "%destDir%"
::--------
in other words, date sorting should be reversed (newest first).

Report •

#2
July 1, 2010 at 20:56:18

Thanks nbrane- I really appreciate your response...and your time. I made the changes and immediately below is my code...unfortunately I am still getting the same error. I am fairly new to Batch files...so any advice would be greatly appreciated. The source folder has 1 text file and 20 or so folders within it. I wouldn't think that this would be an issue and that the batch file would simply just copy the 1 text file...unless I have it set up wrong.

@echo off
setlocal
set srcDir= "O:\Historical_AOT\"
set destdir= "O:\Test\"
set lastmod=
pushd "%srcDir%"
for /f "tokens=*" %%a in ('dir /b /o-d 2^>NUL') do set lastmod=%%a
if "%lastmod%"=="" echo Could not locate files.&goto :eof
copy "%lastmod%" "%destDir%"
pause


Report •

#3
July 1, 2010 at 21:46:53

i did notice you put dbl quotes around srcdir and then dbl quoted it. could this be the problem?

set srcDir= "O:\Historical_AOT\"
set destdir= "O:\Test\"
set lastmod=
pushd "%srcDir%"

looks like (to interpreter): pushd ""O:\historical_AOT\""
note that %srcdir% is already dbl-quoted, (also space in front!) then the pushd adds another set of quotes.
Keep in mind, batch interprets everything very literally.
f/e: set destdir= "O:\test\"
won't give destdir=:[O:\test\]
it will give destdir=:[ "O:\test\"]

we are used to using quotes, but batch is another game altogether. beware where and how you use them, both single, double and reverse. All have very special meanings to the interpreter. Not sure this is source of problem, but it might be.
Hth!


Report •

Related Solutions

#4
July 2, 2010 at 05:46:22

Awesome- the spacing and quotes were the issue. Thanks nbrane for your feedback. Like I said, I am new to batches so I had no clue that spacing or quotes would have much if any impact. I new I was doing somethng wrong....but didn't know what. Thanks again for the info nbrane.

So...One more quick item....the batch file is copying however it is copying the oldest not the most recently modified file. I actually need the most recent. For example, I have files that were created in my folder every day from June 4th until this morning, July 2. The batch moved the June 4th file (which I have not touched since June 4th). Below is my code. Sorry...again I am not quite sure what I am doing wrong here. Thanks for the great feedback thus far.....and any feedback would be greatly appreciated.

@echo off
setlocal
set srcDir=O:\Historical_AOT\
set destdir=O:\Test\
set lastmod=
pushd %srcDir%
for /f "tokens=*" %%a in ('dir /b /o-d 2^>NUL') do set lastmod=%%a
if "%lastmod%"=="" echo Could not locate files.&goto :eof
copy "%lastmod%" "%destDir%"
pause


Report •

#5
July 2, 2010 at 09:43:46

I was able to get it to work however I had to change the following line of my batch file:

Previous Text: for /f "tokens=*" %%a in ('dir /b /o-d 2^>NUL') do set lastmod=%%a

Revised Text: for /f "tokens=*" %%a in ('dir /b /od 2^>NUL') do set lastmod=%%a

I previously had a dash between o and d ("o-d"). However once removed, it seemed to correctly identify and copy the most current file. Below is the full text of the working batch file:

@echo off
setlocal
set srcDir=O:\Historical_AOT
set destdir=O:\Test
set lastmod=
pushd %srcDir%
for /f "tokens=*" %%a in ('dir /b /od 2^>NUL') do set lastmod=%%a
if "%lastmod%"=="" echo Could not locate files.&goto :eof
copy "%lastmod%" "%destDir%"
pause

Thanks nbrane for your guidance and helping me set this up- Much appreciated!!!


Report •

#6
July 2, 2010 at 09:46:22

ah, my bad, should have seen this. You need a goto to jump out of the for-loop:
for /f "tokens=*" %%a in ('dir /b /o-d /a-d 2^>NUL') do set lastmod=%%a & goto :xx
:xx
if "%lastmod%"=="" echo Could not locate files.&goto :eof
:::
note i also added /a-d in order to make sure you don't grab a directory. If you know your file's name and/or extension you could further refine your search, like:
dir *.log
to make sure you don't get other files.


Report •


Ask Question