成功复制到 USB 驱动器 IF EXIST

successfully coppied to USB drive IF EXIST

我有一个用于 Windows XP 机器的简单批处理文件,可以将文件复制到 E、F、G 或 H 上的 USB 驱动器,一切正常,但我想确认文件已通过文本消息“文件已成功复制到 USB 驱动器”传输到驱动器,执行此操作的最佳方法是什么?

REM ------ Creation of the ZIP file ------

%SupervisorPath%-ZipPortable\App-Zipz 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%-ZipPortable\App-Zipz 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

您可以使用 && 运算符在成功时有条件地执行命令

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"

我怀疑你只有一个U盘,而且你不确定它分配给了哪个盘符。在这种情况下,您可以使用 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 )。请注意,需要 space.

someCommand && (
  commandToRunIfSuccess
  someCommandThatMayFail
  (call )
) || (
  commandToRunIfError
)