Home > Software design >  monitoring sftp with winscp and batch
monitoring sftp with winscp and batch

Time:10-16

I want to monitor our sftp to send email to us if a file is added, for now I tried to make a condition with IF/ELSE with a batch script, but the batch environment does not accept my condition. I am new with batch and automation, so what I tried to do is synchronise the sftp file with a local file in first place and run a batch schedule to try to synchronise again; if it does then it is going to send na email (I did not make the script for the email at the moment and to be honest I do not know how to do so for now), if it did not synchronise then exit script.

Here is my script:

option batch on
option confirm off
open sftp://[email protected]/ -privatekey=privateKey.ppk -hostkey="ssh-rsa 2048 x"
option transfer binary
if synchronize local "C:\Users\Administrateur\Desktop\x\x" "/x/x/rx" (
ECHO nouveau fichier ajouter au repertoir
)
else (ECHO aucun nouveau fichier exit
)

Here is the error:

Commande inconnue 'if'.

CodePudding user response:

  • Add -preview switch to your synchronize command to make it only check for changes, instead of actually synchronizing the files.

  • Add option failonnomatch on to your script to make the synchronize command report no differences as an error.

    option failonnomatch on
    synchronize -preview local "C:\Users\Administrateur\Desktop\x\x" "/x/x/rx"
    
  • In the actual batch file, check WinSCP exit code to determine, if there are any differences or not. Something like this:

    winscp.com /script=script.txt /log=script.log
    if errorlevel 1 (
      echo Nothing to synchronize or other problem
    ) else (
      echo There are files to synchronize
    )
    

    If you want to send an email, see WinSCP guide for Emailing script results.

  • Related