0

在这里发现了同样的问题。我正在尝试使用代码,但我的数据。我绝不是程序员。我可以接受几乎任何代码并让它为我工作。

如何编写批处理文件来复制和重命名最近日期的文件?

这是我的

 @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

而结果...

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 . . .

我认为这与文件名中的空格有关。我尝试了一些我发现的其他代码,它给出了相同的结果。我很近,只需要一点推动。感谢您的时间和帮助。

4

2 回答 2

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
于 2012-07-13T15:18:09.197 回答
0

你是对的,你的直接问题是文件名中的空格。您可以通过添加引号来解决该问题: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%
于 2012-07-12T21:58:34.980 回答