Using a Batch File to Execute Commands After S3 Sync

You can use a batch file to execute commands after S3 Sync completes. Say you want to upload files to S3 then delete the local files if the upload was successful. The following batch file is an example of this process.

Upload files to S3 then delete local files.

C:\Program Files (x86)\SprightlySoft\S3 Sync\S3Sync.exe" -AWSAccessKeyId xxxxxxxxxxxx -AWSSecretAccessKey xxxxxxxxxxxx -BucketName mybucket -S3FolderKeyName "myfolder/" -SyncDirection Upload -LocalFolderPath "C:\myfolder" -DeleteS3Items true -LogOnlyMode false -OutputLevel 1 -CompareFilesBy Timestamp -LicenseKey xxxxxxxxxxxx -LogFilePath "C:\Temp\S3 Sync <BucketName> <S3FolderKeyName> <<yyyy-MM-dd HH.mm.ss>> <SyncDirection>.txt"

@if %ERRORLEVEL% neq 0 goto error

@echo S3 upload successful.
RMDIR "C:\myfolder" /Q /S
REM pause
exit 0

:error
@echo S3 upload error. Local delete not executed.
REM pause
exit 1
  • The first line in the script above uploads files with S3 Sync.
  • The %ERRORLEVEL% line checks the error level returned from S3 Sync. If the error level is not 0, the application skips the next block of code and goes to the :error section.
  • The @echo line writes a message to the console window.
  • The line with RMDIR deletes the local folder. This line will only be executed if error level from S3 Sync was 0 which means success.
  • The REM pause line would wait for the use to press a key to continue. The REM statement comments out the line and it will not be executed. Remove REM if you would like to test the script and want to be notified before continuing.
  • The exit 0 line exits the batch script with a success code. The remainder of the script will not be executed.
  • The :error line is a section marker of the script.
  • The @echo line writes a message to the console window.
  • The REM pause line can be uncommented during testing.
  • The exit 1 line exits the batch script with an error code.

The following is an example of downloading files with S3 Sync then deleting those files with S3 Delete. You can get S3 Delete for free at http://sprightlysoft.com/S3Delete/

Download files from S3 then delete files on S3.

"C:\Program Files (x86)\SprightlySoft\S3 Sync\S3Sync.exe" -AWSAccessKeyId xxxxxxxxxxxx -AWSSecretAccessKey xxxxxxxxxxxx -BucketName mybucket -S3FolderKeyName "myfolder/" -SyncDirection Download -LocalFolderPath "C:\myfolder" -DeleteLocalItems true -LogOnlyMode false -OutputLevel 1 -CompareFilesBy Timestamp -LicenseKey xxxxxxxxxxxx -LogFilePath "C:\Temp\S3 Sync <BucketName> <S3FolderKeyName> <<yyyy-MM-dd HH.mm.ss>> <SyncDirection>.txt"

@if %ERRORLEVEL% neq 0 goto error

@echo S3 download successful.
"C:\Program Files (x86)\SprightlySoft\S3 Delete\S3Delete.exe" -AWSAccessKeyId xxxxxxxxxxxx -AWSSecretAccessKey xxxxxxxxxxxx -BucketName mybucket -S3KeyName "myfolder/" -LogFilePath "C:\Temp\S3 Sync delete.txt" -OutputLevel 2
REM pause
exit 0

:error
@echo S3 download error. S3 delete not executed.
REM pause
exit 1
  • The first line in the script above downloads files with S3 Sync.
  • The %ERRORLEVEL% line checks the error level returned from S3 Sync. If the error level is not 0, the application skips the next block of code and goes to the :error section.
  • The @echo line writes a message to the console window.
  • The line with S3Delete.exe deletes the files from S3. This line will only be executed if error level from S3 Sync was 0 which means success.
  • The REM pause line would wait for the use to press a key to continue. The REM statement comments out the line and it will not be executed. Remove REM if you would like to test the script and want to be notified before continuing.
  • The exit 0 line exits the batch script with a success code. The remainder of the script will not be executed.
  • The :error line is a section marker of the script.
  • The @echo line writes a message to the console window.
  • The REM pause line can be uncommented during testing.
  • The exit 1 line exits the batch script with an error code.

To use the batch files above copy the code an place them in a text file using a tool such as Notepad. Change the parameters such as AWSAccessKeyId to your values. Save the file with a .bat extension. An example of the file name is “DownloadThenDelete.bat”. You can double click the bat file to execute it.