0

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
user396581
  • 119
  • 1
  • 2
  • 10

1 Answers1

2

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
)
dbenham
  • 127,446
  • 28
  • 251
  • 390
  • Thanks for the swift reply, Yes I only have 1 USB drive but if i have a keyboard and mouse plugged in then I dont know the drive letter, the move option would be good, I will give this a try tomorrow, thanks – user396581 Feb 15 '15 at 15:18
  • I have installed the batch file with the modified Move command but it does not move the file, it does create it, but no move, I haven't put the Delete line in yet just to see what is happening, so I know it is being created. – user396581 Feb 19 '15 at 08:28
  • It seems to work for me on my Windows 7 computer but it doesn't work on the machine I have written this for which is Windows XP? I am not sure if a different command is needed to run on older versions of Windows? – user396581 Feb 19 '15 at 09:36