Solved When trying to copy only newest I copy entire directory

January 13, 2012 at 10:45:03
Specs: Windows XP
I've tried using all advice from Google to do this, but I end up copying the entire directory rather than just the newest file. Here's my code:

SET MTBF_CHART="\\ussd-file\Depts\Ops\MFG\Reagent\Charts\AMMS"
SET SAVEPATH = "C:\Documents and Settings\reagent\Desktop\Slideshow"

FOR /F "delims=" %%I IN ('DIR "%MTBF_CHART%" /b /od /a-d *.png*') DO SET NewestFile=%%I
copy "%MTBF_CHART%\%NewestFile%" "C:\"


See More: When trying to copy only newest I copy entire directory

Report •

✔ Best Answer
January 16, 2012 at 19:22:00
Ok now I think we've got it... I had to modify the line counting method.

@echo off
setlocal enabledelayedexpansion
REM setting your variables
SET MTBF_CHART=\\ussd-file\Depts\Ops\MFG\Reagent\Charts\AMMS
SET SAVEPATH=C:\Documents and Settings\reagent\Desktop\Slideshow
REM loading output of dir (bare and sorted by date) into tmp.tmp
dir %MTBF_CHART% /b /od > tmp.tmp
REM creating null incremental variable to hold line count
set /a incr=0
REM looping through tmp.tmp to count lines
for /f %%a in ('type "tmp.tmp"^|find "" /v /c') do (
set /a incr=%%a
)
REM incr variable is now equal to the number of lines from the dir output
REM setting skip variable equal to incr minus 1
set /a skip=%incr% - 1
REM looping through tmp.tmp to find newest file at bottom of list by skipping lines minus 1
for /f "skip=%skip%" %%a in (tmp.tmp) do (
echo %%a > newest_file.tmp
)
REM loading contents of newest_file.tmp into variable
set /p NewestFile=<newest_file.tmp
REM copying newest file to savepath
copy "%MTBF_CHART%\%NewestFile%" "%SAVEPATH%"
endlocal



#1
January 13, 2012 at 17:33:27
Try enabling Delayed Expansion and use ! instead of % in the Copy command line.... %NewestFile% may not be expanding.


Please come back & tell us if your problem is resolved.


Report •

#2
January 16, 2012 at 07:52:15
Unfortunately that didn't work. Let me know if I'm doing this right:

SETLOCAL ENABLEDELAYEDEXPANSION
:Variables
SET MTBF_CHART="\\ussd-file\Depts\Ops\MFG\Reagent\Charts\AMMS"
SET SAVEPATH = "C:\Documents and Settings\reagent\Desktop\Slideshow"


FOR /F "delims=" %%I IN ('DIR "%MTBF_CHART%" /b /od /a-d *.png*') DO SET NewestFile=%%I
echo"%MTBF_CHART%\!NewestFile!"
ENDLOCAL

It will list all of the files in the directory and then a line which says "....\Charts\AMMS"\!NewestFile!" "
The system cannot find the path specified.


Report •

#3
January 16, 2012 at 08:17:57
I'm not exactly clear on what you want to do. I'm thinking you want to find the single newest file in a directory, and copy it elsewhere. Is that correct? If so, then your script needs to contain some function to figure out what the newest file is. That's what your existing script is missing. Try this:

@echo off
setlocal
REM setting your variables
SET MTBF_CHART="\\ussd-file\Depts\Ops\MFG\Reagent\Charts\AMMS"
SET SAVEPATH = "C:\Documents and Settings\reagent\Desktop\Slideshow"
REM loading output of dir (bare and sorted) into tmp.tmp
dir %MTBF_CHART% /b /od > tmp.tmp
REM creating incremental variable to hold line count
set incr=0
REM looping through tmp.tmp to count lines
for /f %%a in (tmp.tmp) do (
set /a incr=%incr% + 1
)
REM incr variable is now equal to the number of lines from the dir output
REM setting skip variable equal to incr minus 1
set /a skip=%incr% - 1
REM looping through tmp.tmp to find newest file at bottom of list by skipping lines minus 1
for /f "skip=%skip%" %%a in (tmp.tmp) do (
echo %%a > newest_file.tmp
)
REM loading contents of newest_file.tmp into variable
set /p NewestFile=<newest_file.tmp
REM copying newest file to savepath
copy "%MTBF_CHART%\%NewestFile%" "%SAVEPATH%"


Report •

Related Solutions

#4
January 16, 2012 at 08:38:03
I didn't think it contained anything that found the newest file, but this is pretty ubiquitous whenever I do a search for finding the newest file using batch files. When using your code I get this:

C:\Documents and Settings\reagent\Desktop\Slideshow>set /p NewestFile= 0<newest_file.tmp
The system cannot find the file specified.

C:\Documents and settings\reagent\desktop\slideshow>copy ""\\ussd-file\Depts\Ops\Mfg\Reagent\Charts\AMMS"\" ""
The sysem cannot find the path specified.


When opening the tmp.tmp file I do see the files with timestamps.


Report •

#5
January 16, 2012 at 09:43:41
What? I'm not exactly clear on what you want to do. I'm thinking you want to find the single newest file in a directory, and copy it elsewhere. Is that correct?

Report •

#6
January 16, 2012 at 12:33:33
That's right.

Report •

#7
January 16, 2012 at 13:35:57
The problem we had earlier was probably due to variables expanding at the wrong time in the for loop that skips down to the newest file. I've added enabledelayedexpansion and change the variable wrappers in that line. I'm writing this from memory on a linux box so this isn't tested. Test and let me know what happens.

@echo off
setlocal enabledelayedexpansion
REM setting your variables
SET MTBF_CHART="\\ussd-file\Depts\Ops\MFG\Reagent\Charts\AMMS"
SET SAVEPATH = "C:\Documents and Settings\reagent\Desktop\Slideshow"
REM loading output of dir (bare and sorted) into tmp.tmp
dir %MTBF_CHART% /b /od > tmp.tmp
REM creating incremental variable to hold line count
set incr=0
REM looping through tmp.tmp to count lines
for /f %%a in (tmp.tmp) do (
set /a incr=%incr% + 1
)
REM incr variable is now equal to the number of lines from the dir output
REM setting skip variable equal to incr minus 1
set /a skip=%incr% - 1
REM looping through tmp.tmp to find newest file at bottom of list by skipping lines minus 1
for /f "skip=!skip!" %%a in (tmp.tmp) do (
echo %%a > newest_file.tmp
)
REM loading contents of newest_file.tmp into variable
set /p NewestFile=<newest_file.tmp
REM copying newest file to savepath
copy "%MTBF_CHART%\%NewestFile%" "%SAVEPATH%"


Report •

#8
January 16, 2012 at 14:52:29
Still doesn't work. Looks like it doesn't like the skip line, it says "!skip! was unexpected at this time.

Report •

#9
January 16, 2012 at 19:22:00
✔ Best Answer
Ok now I think we've got it... I had to modify the line counting method.

@echo off
setlocal enabledelayedexpansion
REM setting your variables
SET MTBF_CHART=\\ussd-file\Depts\Ops\MFG\Reagent\Charts\AMMS
SET SAVEPATH=C:\Documents and Settings\reagent\Desktop\Slideshow
REM loading output of dir (bare and sorted by date) into tmp.tmp
dir %MTBF_CHART% /b /od > tmp.tmp
REM creating null incremental variable to hold line count
set /a incr=0
REM looping through tmp.tmp to count lines
for /f %%a in ('type "tmp.tmp"^|find "" /v /c') do (
set /a incr=%%a
)
REM incr variable is now equal to the number of lines from the dir output
REM setting skip variable equal to incr minus 1
set /a skip=%incr% - 1
REM looping through tmp.tmp to find newest file at bottom of list by skipping lines minus 1
for /f "skip=%skip%" %%a in (tmp.tmp) do (
echo %%a > newest_file.tmp
)
REM loading contents of newest_file.tmp into variable
set /p NewestFile=<newest_file.tmp
REM copying newest file to savepath
copy "%MTBF_CHART%\%NewestFile%" "%SAVEPATH%"
endlocal


Report •

Ask Question