我有一个简单的批处理文件,用于将文件复制到E,F,G或H上的USB驱动器的Windows XP计算机,一切正常,但我想确认该文件已转移到通过文本消息“成功复制到USB驱动器的文件”驱动器的最佳方法是什么?
REM ------ Creation of the ZIP file ------
%SupervisorPath%\7-ZipPortable\App\7-Zip\7z a -tzip %BackupPath%\Backup\%FileStamp%.zip %BackupPath%\Backup\
REM ------ Copy the backup file to a USB drive with File Name and Date Stamp -----
IF EXIST E: (echo copying files to USB drive E:
copy %BackupPath%\Backup\%FileStamp%.zip E: /y )
IF EXIST F: (echo copying files to USB drive F:
copy %BackupPath%\Backup\%FileStamp%.zip F: /y )
IF EXIST G: (echo copying files to USB drive G:
copy %BackupPath%\Backup\%FileStamp%.zip G: /y )
IF EXIST H: (echo copying files to USB drive H:
copy %BackupPath%\Backup\%FileStamp%.zip H: /y )
REM ------ Delete the temporary zip file from the backup folder ------
echo Deleting temporary zip file from the backup folder
Del %BackupPath%\Backup\%FileStamp%.zip
该文件的新部分如下,但它不会移动文件
REM ------ Creation of the ZIP file ------
%SupervisorPath%\7-ZipPortable\App\7-Zip\7z a -tzip %BackupPath%\Backup\%FileStamp%.zip %BackupPath%\Backup\
REM ------ Move the backup file to a USB drive with File Name and Date Stamp ------
for %%D in (E F G H) do if exist %%D: (
echo Moving files to USB drive %%D:
move /y "%BackupPath%\Backup\%FileStamp%.zip" %%D: >nul && (
echo Files moved to USB drive successfully
goto :break
)
)
:break
答案 0 :(得分:2)
您可以使用&&
运算符在成功时有条件地执行命令
for %%D in (E F G H) do if exist %%D: (
echo copying files to USB drive %%D:
copy /y "%BackupPath%\Backup\%FileStamp%.zip" %%D: >nul && echo Files copied to USB drive successfully
)
echo Deleting temporary zip file from the backup folder
del "%BackupPath%\Backup\%FileStamp%.zip"
我怀疑你只有一个USB驱动器,而且你不确定它被分配给哪个驱动器号。在这种情况下,您可以使用MOVE而不是COPY,然后使用DEL,并且您可以在成功时中止循环。
for %%D in (E F G H) do if exist %%D: (
echo Moving files to USB drive %%D:
move /y "%BackupPath%\Backup\%FileStamp%.zip" %%D: >nul && (
echo Files moved to USB drive successfully
goto :break
)
)
:break
||
运算符在失败时有条件地执行命令。如果同时使用&&
和||
,则||
应遵循&&
。
someCommand && (
commandToRunIfSuccess
) || (
commandToRunIfError
)
如果成功块中的最后一个命令失败,则会触发后续错误块。因此,如果最后一个命令失败,那么您应该添加另一个保证成功的命令。保证成功的最简单(和最快)命令是(call )
。请注意,有一个必要的空间。
someCommand && (
commandToRunIfSuccess
someCommandThatMayFail
(call )
) || (
commandToRunIfError
)