0

Found the same question here.. I am trying to use the code but for my data. I am in no way a programmer.. I can take almost any code and make it work for me however.

How to code a batch file to copy and rename the most recently dated file?

Here is my

 @echo on
 setLocal DisableDelayedExpansion
 pushd  N:\
 setLocal EnableDelayedExpansion

 for /f "tokens=* delims= " %%G in ('dir/b/od') do (set newest=%%G)

 copy %newest% G:\Server_Files\AMI_DATA\TEXT_FILES\NewFile.csv

 PAUSE
 POPD

And the results...

C:\Documents and Settings\belcherj\Desktop\AMI_BATCH>setLocal DisableDelayedExpa
nsion

C:\Documents and Settings\belcherj\Desktop\AMI_BATCH>pushd  N:\

N:\>setLocal EnableDelayedExpansion

N:\>for /F "tokens=* delims= " %G in ('dir/b/od') do (set newest=%G )

N:\>(set newest=System Blink Count__2012-06-27 01_00_46.csv )

N:\>(set newest=System Blink Count__2012-06-28 01_00_32.csv )

N:\>(set newest=System Blink Count__2012-06-29 01_00_33.csv )

N:\>(set newest=System Blink Count__2012-06-30 01_00_42.csv )

N:\>(set newest=System Blink Count__2012-07-01 01_00_32.csv )

N:\>(set newest=System Blink Count__2012-07-02 01_00_31.csv )

N:\>(set newest=System Blink Count__2012-07-03 01_00_29.csv )

N:\>(set newest=System Blink Count__2012-07-04 01_00_39.csv )

N:\>(set newest=System Blink Count__2012-07-05 01_00_34.csv )

N:\>(set newest=System Blink Count__2012-07-06 01_00_31.csv )

N:\>(set newest=System Blink Count__2012-07-07 01_00_32.csv )

N:\>(set newest=System Blink Count__2012-07-08 01_00_30.csv )

N:\>(set newest=System Blink Count__2012-07-09 01_00_40.csv )

N:\>(set newest=System Blink Count__2012-07-10 01_00_29.csv )

N:\>(set newest=System Blink Count__2012-07-11 01_00_44.csv )

N:\>(set newest=System Blink Count__2012-07-12 01_00_34.csv )

N:\>copy System Blink Count__2012-07-12 01_00_34.csv G:\Server_Files\AMI_DATA\TE
XT_FILES\NewFile.csv
The system cannot find the file specified.

N:\>PAUSE
Press any key to continue . . .

I think it has something to do with the spaces in the file names.. I tried some other code i found and it gave the same results. Im close just need a little push. Thanks for you time and help.

Community
  • 1
  • 1
jestexman
  • 3
  • 1
  • 1
  • 4

2 Answers2

6

This should work

@echo off
cd /d N:\
for /f "tokens=*" %%a in ('dir /b /od') do set newest=%%a
echo f| xcopy "%newest%" G:\Server_Files\AMI_DATA\TEXT_FILES\NewFile.csv
Bali C
  • 30,582
  • 35
  • 123
  • 152
  • Did the trick! Looks like the CD to get it to open the mapped network drive is what got it to work.. I am probably wrong :) .. Thank your for your help @dbnham and bali C – jestexman Jul 13 '12 at 16:00
0

You are correct, your immediate problem is the spaces in the file name. You could fix that problem by adding quotes: copy "%newest%" "G:\Server_Files\AMI_DATA\TEXT_FILES\NewFile.csv"

But there are other problems. The FOR options should be "eol=: delims=". Your existing options will will fail if file name begins with space or semicolon.

Your code can also fail if a filename contains an exclamation point because you have delayed epansion enabled when you use the FOR %%G variable. You enable delayed expansion, yet you don't use it.

Your code can also fail if the last "file" listed happens to be a directory instead of a file. You want the DIR /A-D option to filter out directory names.

Oops! my original posted code did not work because the PUSHD had been removed and DIR /B does not include path info in the output.

Here is a solution that avoids the potential problems and also adds some additional error checking:

@echo on
setlocal disableDelayedExpansion
set "quit=pause & exit /b"
pushd N:\ || %quit%
set "newest="
for /f "eol=: delims=" %%F in ('dir /b /od /a-d') do set "newest=%%F"
if defined newest copy "%newest%" G:\Server_Files\AMI_DATA\TEXT_FILES\NewFile.csv
%quit%

Disabling delayed expansion is probably not necessary since it is the default state on most installations. But it can't hurt.

Since SETLOCAL is used, any changes to the environment will be lost once the script ends. (There is an implicit ENDLOCAL once the script ends). The ENDLOCAL (implicit or explicit) also undoes the results of PUSHD, which is why POPD is not required.

I defined a quit "macro" to make it convenient to always exit the script with the same two commands: PAUSE followed by EXIT /B. The EXIT /B is not needed at the very end of the script, but it doesn't hurt.

The script can be made a bit more efficient by listing the files in descending chronological order and using GOTO to break out of the loop after the 1st file.

@echo on
setlocal disableDelayedExpansion
set "quit=pause & exit /b"
pushd N:\ || %quit%
set "newest="
for /f "eol=: delims=" %%F in ('dir /b /o-d /a-d') do set "newest=%%F" & goto :break
:break
if defined newest copy "%newest%" G:\Server_Files\AMI_DATA\TEXT_FILES\NewFile.csv
%quit%

or the file can be copied within the loop without defining an environment variable, at which point the script can end immediately:

@echo on
setlocal disableDelayedExpansion
set "quit=pause & exit /b"
pushd N:\ || %quit%
for /f "eol=: delims=" %%F in ('dir /b /o-d /a-d') do (
  copy "%%F" G:\Server_Files\AMI_DATA\TEXT_FILES\NewFile.csv
  %quit%
)
%quit%
dbenham
  • 127,446
  • 28
  • 251
  • 390
  • Thank you for the reply but your solution did not seem to do anything at all. I added the quotes.. and received the same message. i made a new bat file with the code you provided and it does not give me any message. Was i supposed to add that to my first bat file? I apologize for being so new to this. Our file names will be like System Blink Count__2012-07-12 01_00_34.csv – jestexman Jul 12 '12 at 22:13
  • @jestexman - The quotes really should fix your original code. I'm not sure where you went wrong. I've fixed my code, there were significant problems. – dbenham Jul 13 '12 at 00:09
  • I really appreciate you looking a this for me. I am still not getting any results. N and G are mapped network drives. Could that be causing the batch not to work? i am way way new at this batch file stuff. I tried all three of your files. I copied the text into a blank txt file then renamed it amirname.bat then ran it from there. The dos window pops up fro a half a sec and then closes. Thats all i get. Again thank you for your time. – jestexman Jul 13 '12 at 13:26