0

我创建了以下批处理文件,但没有成功。

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

感谢任何有关正确方向的指导。

UPDATES1: 我注意到我的变量没有设置。

更新2:

@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

更新的输出2:

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.

日志

\\pc1\temp\file1.mdb
\\pc1\temp\file1_be.mdb
2 File(s) copied

使用新代码 (UPDATES2),我已成功将所有 mdb 文件复制到目标文件夹,但未记录所有回显消息。如何包含要记录的所有回显消息?

4

1 回答 1

2

我认为你需要添加

**cd /d %~dp0\**   

(“%~dp0”可以将运行时目录替换为批处理文件的全路径)

在你的第一行..

cd /d %~dp0 表示 cd /dd:\currentfolder

更新:

好的,我认为您只想将文件从 A 复制到 B。

我根据您的代码为您创建了一个示例。

我的文件夹在

D:\Run
│  main.bat  >> the copy batch
│  
└─Test
    │  new.txt
    │  
    └─Backups
        └─20120214          >> created new folder

main.bat 是

@ECHO OFF

cd /d %~dp0\

ECHO Backup process started.

SET TARGET_FILES="Test\*.txt"
ECHO TARGET_FILES:%TARGET_FILES%


SET DESTINATION_FOLDER=Test\Backups\%date:/=%
ECHO DESTINATION_FOLDER:%DESTINATION_FOLDER%

SET FINAL_FOLDER=%DESTINATION_FOLDER%
ECHO FINAL_FOLDER:%FINAL_FOLDER%
MD "%FINAL_FOLDER%"
ECHO "DIRECTORY CREATED:%FINAL_FOLDER%

XCOPY %TARGET_FILES% %FINAL_FOLDER%

ECHO Backup process ended successfully.

@ECHO ON

我希望它会帮助你。

于 2012-02-14T02:13:16.363 回答