Writing a batch file for sftp

Last week, I was given the task of trying to write a batch file to upload and download a folder via SFTP. It wasn't as nearly as hard as I thought. Here is how I did it.

Firstly, I downloaded PuTTY for Windows. I made a folder on the c: drive call sftp.

I copied PuTTY to this folder. The batch files should go into the same folder as PuTTY.

Next I opened PuTTY and logged onto the SFTP server so that the local server knew about the keys. The batch file will fail if this isn't done first. Their are ways to add this part to a batch file, but this seemed easier.

I then made a batch file that was called scheduled_download.bat

In the file I put the following code:

psftp username@sftp server name -v -bc -batch -pw logon_password -b
download_files.bat

You don't seem to be able to put file commands like get and put into this batch file so I had to made a second one. This was called download_files.bat. You can see this in the second line of code. That batch file had the following lines:

lcd c:\patches
get -r test

lcd changes the local direct on the windows machine. This is the folder that the files will be downloaded to. The, get -r test, part will download all the files in the folder test on the sftp server.

If you want to copy files to the sftp server, you can use this code:

lcd c:/patches
cd /test
put -r v3

Again, lcd changes the directory you are using to upload from. cd changed the upload folder on the sftp server to test and put -r v3 uploaded all the files from the folder v3 that is in test.

I hope this helps.
|
Web Analytics