问题描述
我正在使用批处理脚本 (.bat) 将文件从源文件夹复制到目标文件夹。如果目标文件夹中存在文件,我想用时间戳作为后缀重命名文件并复制新文件。如果不存在,我想新文件复制自己。
@ECHO OFF
REM ### Needed so the variables will expand properly within the FOR DO loops ###
Setlocal EnableDelayedExpansion
REM ### Set the source file(s) and call the "copyIt" subroutine ###
SET Sourcefile=Arthematic.psf
CALL :copyIt
REM ### End of the program. Otherwise the subroutine will run again ###
GOTO END
REM ### The subroutine ###
:copyIt
REM ### Set the date and time to a variable called "DaTime" and remove offending ###
REM ### characters (/ and :) that can't be used in a filename ###
FOR /f "tokens=1-4 delims=/ " %%a in ("!date!") do SET DaTime=%%b-%%c-%%d
FOR /f "tokens=1-3 delims=':'" %%e in ("!time!") do SET DaTime=!DaTime!_%%e-%%f-%%g
REM ### Set a variable called "DestFile" to source filename minus ".psf" ###
REM ### to be used to set the new destination file name + "DaTime" ###
SET Destfile=!Sourcefile:.psf=!
REM ### Check to see if the source filename exists in the destination directory ###
DIR "C:\Program Files\Math\Core Simulate\M020\Arthematic\bin\!Sourcefile!" > NUL
IF !ErrorLevel!==0 (
REM ### If it does exist,set the loop count to 0 ###
SET /a Count=0
REM ### Have xcopy check to see if the source file is newer,but only report and not copy ###
REM ### Thanks to selbie for suggesting the /L option ###
FOR /f "tokens=1 delims=0" %%s in ('xcopy /Y /D /L "C:\Users\Public\Documents\Kollective\!Sourcefile!" "C:\Program Files\Math\Core Simulate\M020\Arthematic\bin\"') DO (
REM ### Increment the loop count ###
SET /a Count=!Count!+1
REM ### Only pick up the first iteration. If the files are different,it will be the full path and the filename ###
If !Count!==1 SET Source=%%s
)
REM ### If the source is the same as the destination,then !Source! will = " File(s)". Change it to NONE ###
SET Source=!Source: File^(s^)=NONE!
REM ### If it's not equal to NONE,copy the file and rename it to source file + date & time + .txt ###
IF !Source! NEQ NONE copY /Y "!Source!" "C:\Program Files\Math\Core Simulate\M020\Arthematic\bin\!Destfile!-!DaTime!.psf"
) ELSE (
REM ### If it does not exist,copy the file without renaming it ###
copY /Y "C:\Users\Public\Documents\Kollective\!Sourcefile!" "C:\Program Files\Math\Core Simulate\M020\Arthematic\bin\"
)
REM ### Exit the subroutine ###
EXIT /b
:END
pause
但是,从桌面执行脚本时,我收到以下错误。我是不是忽略了什么?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)