Synchronize Script - External Command After Each File Sync?

Advertisement

gsidhu
Joined:
Posts:
2

Synchronize Script - External Command After Each File Sync?

Hi there,
I am wondering whether it is possible to execute an external command after each file has been synchronized using the synchronize feature in WinSCP? Ie., I would like to run an unzip command if the file has been updated, and move the files to a separate, unique directory. Currently, I am using 7-Zip batch scripts to unzip and move archives, but would like to simplify it within the WinSCP script itself, if possible.
Or, if there is a means to trigger the 7-Zip batch files if a new archive has been synced, that would also be great.

\Staging Folder
-> \Database 1
->Database.zip - No Sync Required
-> \Database 2
->Database2.zip - File Updated (File to be unzipped and moved)
-> \Database 3
->Database.zip3 - No Sync Required
-> \...

The batch and script that I am currently using are as following:

winscp.com /script="F:\Scripts\ScriptNotificationTest.txt" /log="F:\Scripts\FTPNotificationTestLog.txt"
if errorlevel 1 goto error

echo Synchronization Successful.
goto end

:error
echo Synchronization Failed.

end:

Script:

#Automatically abort script on errors
option batch abort

#Disable overwrite confirmations that conflict with the previous
option confirm off

#Connect to ******* server using existing login credentials
open *******

#Synchronize database releases
#Synchronize Test Zip
synchronize local -criteria=time -resumesupport=on -filemask="TestZipFile.zip" "F:\*******\Databases\Stage\Test\"

#Synchronize Test Zip 2
synchronize local -criteria=time -resumesupport=on -filemask="TestZipFile2.zip" "F:\*******\Databases\Stage\Test\"

Thanks!

Reply with quote

Advertisement

Guest

+Update:
I tried to create a second batch script to extract and move the synchronized files as required. Works fine when executed on it's own. However, I am now trying to have it execute automatically after synchronization has completed. I have tried to combine the two scripts into one with no luck. Any suggestions? Thanks!

"C:\Program Files (x86)\WinSCP\WinSCP.com" /script="C:\Users\*******\Desktop\NestedTestScript.txt"

REM Extract & Move

REM Variables
SET DATE=%date:~10,4%_%date:~4,2%_%date:~7,2%

If exist "F:\*******\Databases\Stage\TestZipFile1\TestZipFile1.zip" (
   7z x "F:\*******\Databases\Stage\TestZipFile1\TestZipFile1.zip" -o"F:\*******\Databases\Stage\TestZipFile1\" -y
   mkdir "F:\*******\Databases\TestZipFile1\Current As Of - %DATE%"
   move "F:\*******\Databases\Stage\TestZipFile1\*.*" "F:\*******\Databases\TestZipFile1\Current As Of - %DATE%"
   goto database2
) 
:database2
If exist "F:\*******\Databases\Stage\TestZipFile1\TestZipFil21.zip" (
   7z x "F:\*******\Databases\Stage\TestZipFile2\TestZipFile2.zip" -o"F:\*******\Databases\Stage\TestZipFile2\" -y
   mkdir "F:\*******\Databases\TestZipFile2\Current As Of - %DATE%"
   move "F:\*******\Databases\Stage\TestZipFile2\*.*" "F:\*******\Databases\TestZipFile2\Current As Of - %DATE%"
   goto end
)
:end

pause

Reply with quote

Guest

After doing some more reading, I have tried to create a third batch file which first calls the WinSCP script, followed by the second batch script. Unfortunately, when using this method, the WinSCP script executes, but the second batch does not. If this is a viable solution, any help with this method would also be appreciated.

Thanks!

Reply with quote

digitaltoast
Joined:
Posts:
7

Re: Synchronize Script - External Command After Each File Sync?

gsidhu wrote:

Hi there,
I am wondering whether it is possible to execute an external command after each file has been synchronized

Hope I'm not thread-hijacking I'd also be really interested in exactly this as I'm trying to write a very simple "click and go" DOS script for a blind person who simply needs to sync his local folder with a remote server with ftp explicit TLS, so plain old ftp is out of the question.

I know I can make the system bell go "ding" with a special character in a batch script, and I can make soundrec.exe do something similar with a short wav file.

But what I can't figure out, despite much googling, is how to let the blind user know after each file has completed, and again when the whole lot is done. Advice appreciated!

Reply with quote

martin
Site Admin
martin avatar
Joined:
Posts:
41,468
Location:
Prague, Czechia

Re: Synchronize Script - External Command After Each File Sync?

As I already answered on
https://winscp.net/forum/viewtopic.php?p=51229#51229
using WinSCP .NET assembly for such a task is probably easier than using a script.

Similar example is here:
https://winscp.net/eng/docs/library_example_delete_after_successful_download

Or you can leverage Session.FileTransferred event to make it more real-time, if needed:
https://winscp.net/eng/docs/library_session_filetransferred
https://winscp.net/eng/docs/library_session_synchronizedirectories#example

Anyway, regarding your 3-batch-file solution: If you are calling two batch files in a sequence from a master batch file, you need to use call command, otherwise master batch file never continues after the first child batch file completes. This is a general rule (nothing to do with WinSCP). Though I do not understand what you need 3 batch files for :)

Reply with quote

Advertisement

digitaltoast
Joined:
Posts:
7

Re: Synchronize Script - External Command After Each File Sync?

martin wrote:

using WinSCP .NET assembly...

Thanks for the reply. I don't know much (anything!) about programming .NET - is this a standalone self-contained .dll?

I can't test how it works without .NET installed as I don't have such a machine here, but this MUST be totally standalone, even runnable on XP (yes, I know!), without user prompting or installation of third party libraries. If it can't run from a USB on a "basic" windows, it's no good here.

Is this likely to work in this case?

Reply with quote

martin
Site Admin
martin avatar
Joined:
Posts:
41,468
Location:
Prague, Czechia

Re: Synchronize Script - External Command After Each File Sync?

The WinSCP .NET assembly does not need anything else apart from WinSCP itself (winscp.exe) and the Microsoft .NET framework.

The .NET framework is part of Windows XP SP1:
https://en.wikipedia.org/wiki/Windows_XP#Service_Pack_1
And is included by default with Windows Vista and later:
https://en.wikipedia.org/wiki/.NET_Framework#History

Reply with quote

digitaltoast
Joined:
Posts:
7

Re: Synchronize Script - External Command After Each File Sync?

martin wrote:

The WinSCP .NET assembly does not need anything else apart from WinSCP itself [...] The .NET framework is part of Windows XP SP1:

Brilliant - perfect answer! Thanks. Sounds like that's a safe route to go then. Problem solved, for me at least. Many thanks.

Reply with quote

Advertisement

You can post new topics in this forum