I have a simple Batch File for a Windows XP machine that copies a file to a USB drive on either E, F, G or H, all works as it should but I would like a confirmation that the file has been transferred to the drive by means of a Text Message “Files copied to USB drive successfully” what is the best method to do this?
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
New portion of the file is as follows, but it does not move the files
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
You can use the &&
operator to conditionally execute a command upon success
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"
I suspect you have only one USB drive, and you are not sure which drive letter it is assigned to. In this case, you can use MOVE instead of COPY followed by DEL, and you can abort the loop upon success.
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
The ||
operator conditionally executes a command upon failure. If you use both &&
and ||
, then ||
should follow &&
.
someCommand && (
commandToRunIfSuccess
) || (
commandToRunIfError
)
If the last command in your success block should fail, then it would fire the subsequent error block. So if the last command can fail, then you should add another command that is guaranteed to succeed. The simplest (and fastest) command guaranteed to succeed is (call )
. Note there is a required space.
someCommand && (
commandToRunIfSuccess
someCommandThatMayFail
(call )
) || (
commandToRunIfError
)