Move Remote Files Via Batch/Script

Advertisement

roleki
Joined:
Posts:
2
Location:
United States

Move Remote Files Via Batch/Script

One of our FTP partners dumps .txt and .xlsx files to a root directory; when we have finished grabbing the files they have dumped, we move them to an Archive directory under root.

Currently we do all this in the WinSCP GUI, but I would like to automate this process, and I have got it to where I can pull the files without trouble using a batch file and Task Scheduler, but I have a problem:

I don't know how to move files on the remote server into the Archive folder to show them we have retrieved these.

This is the text file I have set up so far, which is called by the .bat file by Task Scheduler.
***
# ftpfromXYZ.txt
option batch on
option confirm off
open sftp://username:Password@partner.site.com:22
get -resume *.txt "\\Local-srv\Secure$\Downloads\XYZ\"
get -resume *.xlsx "\\Local-srv\Secure$\Downloads\XYZ\"
close
exit
***

Any ideas how I would move a wildcard file to a directory under root on the source server?

Reply with quote

Advertisement

roleki
Joined:
Posts:
2
Location:
United States

Answered it myself BUT could use advice!

I found the MV command and that does what I need it to

***
# ftpfromXYZ.txt
option batch on
option confirm off
open sftp://username:Password@partner.site.com:22
get -resume *.txt "\\Local-srv\Secure$\Downloads\XYZ\"
get -resume *.xlsx "\\Local-srv\Secure$\Downloads\XYZ\"
mv *.txt Archive/
mv *.xlsx Archive/
close
exit
***

But say one day they stick a *.csv in the root; how would I download ALL files EXCEPT the Archive directory, and move all files regardless of format to the Archive directory when done? How would I make sure I successfully copied the file first? I'm new to this, sorry!

Reply with quote

martin
Site Admin
martin avatar
Joined:
Posts:
29,791
Location:
Prague, Czechia

Re: Answered it myself BUT could use advice!

You can exclude the Archive folder from get command using -filemask=|Archive/.
https://winscp.net/eng/docs/scriptcommand_get#filemask
Though that not possible with mv. And even if it was, it won't be transactionally safe. What if a file is added just between get and mv?
For transactionally safe solution, you better use WinSCP .NET assembly.
There's an example for upload:
https://winscp.net/eng/docs/script_local_move_after_successful_upload
Converting it to download should be trivial.

Anyway, if you want to stick with simple scripting, one trick is to use *.?* instead of *.*. That will match only files (and folders) which have some extension (so not Archive).

Reply with quote

Advertisement

You can post new topics in this forum