如何创建批处理文件以将文件复制到具有完全访问权限的共享文件夹,然后记录所有结果和信息文件错误?
我创建了下面的批处理文件,但没有成功。
@ECHO OFF
@ECHO "Backup process started."
@SET TARGET_FILES = "\\pc1\temp\*.mdb"
@ECHO "TARGET_FILES:%TARGET_FILES%"
@SET DESTINATION_FOLDER = "\\pc1\temp\Backups"
@ECHO "DESTINATION_FOLDER:%DESTINATION_FOLDER%"
@SET BACKUP_FOLDER = "Backup%date:~10,4%%date:~4,2%%date:~7,2%"
@ECHO "BACKUP_FOLDER:%BACKUP_FOLDER%"
@SET FINAL_FOLDER = "%DESTINATION_FOLDER%\%BACKUP_FOLDER%"
MKDIR %FINAL_FOLDER%
@ECHO "DIRECTORY CREATED:%FINAL_FOLDER%"
XCOPY %TARGET_FILES% %FINAL_FOLDER% >> %FINAL_FOLDER%\%BACKUP_FOLDER%.log
@ECHO "Backup process ended successfully."
@ECHO ON
环境: 它将使用 Windows XP SP3 在 C:\Windows\Tasks 中计划任务
输出:
"Backup process started."
"TARGET_FILES:"
"DESTINATION_FOLDER:"
"BACKUP_FOLDER:"
The syntax of the command is incorrect.
"DIRECTORY CREATED:"
Invalid number of parameters
"Backup process ended successfully."
任何关于正确方向的指导都值得赞赏。
更新1: 我注意到我的变量没有设置。
UPDATES2:
@ECHO OFF
ECHO Backup process started.
set TARGET_FILES=\\pc1\temp\*.mdb
ECHO TARGET_FILES "%TARGET_FILES%"
SET DESTINATION_FOLDER=\\pc1\temp\Backups
ECHO DESTINATION_FOLDER "%DESTINATION_FOLDER%"
SET BACKUP_FOLDER=Backup%date:~0,4%%date:~5,2%%date:~8,2%
ECHO BACKUP_FOLDER "%BACKUP_FOLDER%"
SET FINAL_FOLDER=%DESTINATION_FOLDER%\%BACKUP_FOLDER%
MKDIR "%FINAL_FOLDER%"
ECHO DIRECTORY CREATED "%FINAL_FOLDER%"
XCOPY "%TARGET_FILES%" "%FINAL_FOLDER%" >> "%FINAL_FOLDER%\%BACKUP_FOLDER%.log"
ECHO Backup process ended successfully.
@ECHO ON
OUTPUT FOR UPDATES2:
Backup process started.
TARGET_FILES "\\pc1\temp\*.mdb"
DESTINATION_FOLDER "\\pc1\temp\Backups"
BACKUP_FOLDER "Backup20120214"
DIRECTORY CREATED "\\pc1\temp\Backups\Backup20120214"
Backup process ended successfully.
LOG
\\pc1\temp\file1.mdb
\\pc1\temp\file1_be.mdb
2 File(s) copied
使用新代码 (UPDATES2),我已成功将所有 mdb 文件复制到目标文件夹但是我的所有回显消息均未记录。如何包含要记录的所有回显消息?
I have created the below batch file but no success.
@ECHO OFF
@ECHO "Backup process started."
@SET TARGET_FILES = "\\pc1\temp\*.mdb"
@ECHO "TARGET_FILES:%TARGET_FILES%"
@SET DESTINATION_FOLDER = "\\pc1\temp\Backups"
@ECHO "DESTINATION_FOLDER:%DESTINATION_FOLDER%"
@SET BACKUP_FOLDER = "Backup%date:~10,4%%date:~4,2%%date:~7,2%"
@ECHO "BACKUP_FOLDER:%BACKUP_FOLDER%"
@SET FINAL_FOLDER = "%DESTINATION_FOLDER%\%BACKUP_FOLDER%"
MKDIR %FINAL_FOLDER%
@ECHO "DIRECTORY CREATED:%FINAL_FOLDER%"
XCOPY %TARGET_FILES% %FINAL_FOLDER% >> %FINAL_FOLDER%\%BACKUP_FOLDER%.log
@ECHO "Backup process ended successfully."
@ECHO ON
Environment:
It will be scheduled task in C:\Windows\Tasks using Windows XP SP3
OUTPUT:
"Backup process started."
"TARGET_FILES:"
"DESTINATION_FOLDER:"
"BACKUP_FOLDER:"
The syntax of the command is incorrect.
"DIRECTORY CREATED:"
Invalid number of parameters
"Backup process ended successfully."
Any guidance on the right direction is appreciated.
UPDATES1:
I've noticed that my variables were not set.
UPDATES2:
@ECHO OFF
ECHO Backup process started.
set TARGET_FILES=\\pc1\temp\*.mdb
ECHO TARGET_FILES "%TARGET_FILES%"
SET DESTINATION_FOLDER=\\pc1\temp\Backups
ECHO DESTINATION_FOLDER "%DESTINATION_FOLDER%"
SET BACKUP_FOLDER=Backup%date:~0,4%%date:~5,2%%date:~8,2%
ECHO BACKUP_FOLDER "%BACKUP_FOLDER%"
SET FINAL_FOLDER=%DESTINATION_FOLDER%\%BACKUP_FOLDER%
MKDIR "%FINAL_FOLDER%"
ECHO DIRECTORY CREATED "%FINAL_FOLDER%"
XCOPY "%TARGET_FILES%" "%FINAL_FOLDER%" >> "%FINAL_FOLDER%\%BACKUP_FOLDER%.log"
ECHO Backup process ended successfully.
@ECHO ON
OUTPUT FOR UPDATES2:
Backup process started.
TARGET_FILES "\\pc1\temp\*.mdb"
DESTINATION_FOLDER "\\pc1\temp\Backups"
BACKUP_FOLDER "Backup20120214"
DIRECTORY CREATED "\\pc1\temp\Backups\Backup20120214"
Backup process ended successfully.
LOG
\\pc1\temp\file1.mdb
\\pc1\temp\file1_be.mdb
2 File(s) copied
With the new code (UPDATES2), I have successfully copied all mdb files to target folder but all my echo messages were not logged. How can I include all my echo messages to be logged?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为你需要
添加(“%~dp0”可以将运行时目录替换为批处理文件的完整路径)
在第一行
.. cd /d %~dp0 表示 cd /dd:\currentfolder
更新:
好的,我想您只是想将文件从 A 复制到 B 。
我根据您的代码为您创建一个示例。
我的文件夹位于
main.bat
我希望它能帮助你。
I think you need add
("%~dp0" can replace the runtime directory to the full path of the batch file)
in your first line ..
cd /d %~dp0 means cd /d d:\currentfolder
UPDATE:
Ok , I think you just want to copy your files from A to B .
I create a sample for you base on your code.
My folder is at
the main.bat is
i hope it would help you .