如何创建批处理文件以将文件复制到具有完全访问权限的共享文件夹,然后记录所有结果和信息文件错误?

发布于 2025-01-04 21:04:23 字数 2133 浏览 1 评论 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."

任何关于正确方向的指导都值得赞赏。

更新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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心碎无痕… 2025-01-11 21:04:23

我认为你需要

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

我希望它能帮助你。

I think you need add

**cd /d %~dp0\**   

("%~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

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

the main.bat is

@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

i hope it would help you .

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文