Windows Batch script to SFTP files to remote location and then move them up a directory once finished

Viewed 37

I have an issue where the files I am uploading to a vendor are being consumed before they are finished uploading. To get around it the vendor has provided me with a location underneath where I can upload the files first then requested I move them once the upload is done.

Stripping out sensitive information, I am referencing this file in my sftp command in batch myfile.scr

cd /from_client/payments/upload
lcd E:/uploadfiles
mput uploadfiles.xml.*
quit

That would upload the files to the remote directory upload. I know need to get them into the level above. The theory is this will then process as normal and the files will already exist so the application won't consume them before they are done uploading.

I would assume I need to do a job to build a list of the files and then a 2nd to loop through them to do a rename on the remote folder to get them to go up a level.

Any ideas on how this could work? The vendor is unable to do anything here.

already tried various solutions to see if I could use mv but thats not an option as its not using Winscp. We can only use batch scripts and then native sftp commands.

1 Answers

You can generate put and rename commands for each file using a batch file like this:

set SOURCE=E:\uploadfiles
set SCRIPT=myfile.scr

(
  echo cd /from_client/payments/upload
  echo lcd %SOURCE%
  for %%F in (%SOURCE%\*) do (
    echo put %%~nF
    echo rename %%~nF /final/path/%%~nF
  )
  echo quit
) > %SCRIPT%

sftp user@host -b %SCRIPT%

It should generate a script like this:

cd /from_client/payments/upload
lcd E:\uploadfiles
put one.txt
rename one.txt /final/path/one.txt
put two.txt
rename two.txt /final/path/two.txt
put three.txt
rename three.txt /final/path/three.txt
quit

Though with a more capable client, it would be easier. For example my WinSCP supports wildcards with mv command, so you can simply do:

WinSCP.com /ini=nul /log=winscp.log /command ^
    "open sftp://user:password@host/ -hostkey=""...""" ^
    "put E:\uploadfiles\* /from_client/payments/upload/" ^
    "mv /from_client/payments/upload/* /final/path/" ^
    "exit"

I'm aware that you wrote that you cannot use other clients, because:

  1. "It's not using WinSCP" – What "it"? It's you! Use the best tool for your task.
  2. "We can only use ... native sftp commands" – There are no "native sftp commands". There are only commands of the client you are using. Currently you are using OpenSSH sftp client. But that's not set in stone. It's easy to switch (you do not even to read it, everything is already in my code above).
Related