sftpwinscp

Copy and delete files from SFTP folder


I have to pick (remove) the files with file mask FileName_A_* and FileName_B_* from SFTP location and place them in an sharedrive.

I tried using WinSCP. I have created an HourlyFile.txt file with below code and placed it under C:\Program Files (x86)\WinSCP . Another batch file HourlyFile.bat to execute the script from HourlyFile.txt

HourlyFile.txt:

option batch abort
option confirm off
open sftp..........
get -filemask="FileName_A_*" /outbound/test/* \\sharedrive
get -filemask="FileName_B_*" /outbound/test/* \\sharedrive
del /outbound/test/FileName_A_*
del /outbound/test/FileName_B_* 
exit

HourlyFile.bat:

winscp.com /script=HourlyFile.txt
pause

I tried with below options to delete the file but got the error message "Unknown command". Also the above code is copying subfolder from /outbound/test/ , which it should not.

Commands tried:

del /outbound/test/FileName_A_*
-del /outbound/test/FileName_A_*
delete /outbound/test/FileName_A_*
delete /outbound/test/FileName_A_20190604_090002
delete /outbound/test/FileName_A_20190604_090002.csv

Solution

  • If you want to download and delete the files, you better use -delete switch of the get command. This way, you can be sure that WinSCP deletes only those files that were really successfully downloaded.

    get -delete /outbound/test/FileName_A_* \\sharedrive\
    get -delete /outbound/test/FileName_B_* \\sharedrive\
    

    See WinSCP article How do I create script that synchronizes files and deletes synchronized files from source afterward?


    To answer your literal question: WinSCP has no del command. WinSCP has rm command:

    rm /outbound/test/FileName_A_*
    rm /outbound/test/FileName_B_*