How to execute WinSCP commands from a batch file - batch-file

I wanna know if there is a way to run WinSCP commands like open sftp..., from a batch file, without having to enter commands after WinSCP opens up.
Here's what I wanna happen:
I create a batch file (say demo.bat) that opens WinSCP command-line.
I insert WinSCP commands (open, get, put, etc.) in demo.bat.
The batch file demo.bat opens up WinSCP command-line and executes the commands (get, put, etc.) automatically.

Use /command or /script command-line switches.
There's a guide to automating file transfers to SFTP server with WinSCP.
A simple batch file using /command switch is like:
WinSCP.com ^
/log="C:\writable\path\to\log\WinSCP.log" /ini=nul ^
/command ^
"open sftp://username:password#example.com/" ^
"put C:\local\path\file.txt /remote/path/" ^
"exit"
And WinSCP GUI can actually even generate the batch file for you:

Related

Automatic FTP download via batch

I need to download a file from my company's FTP server daily, I tried to do this via batch
open wget --user=USERNAME --password=PASSWORD FTP://000.00.0.0
cd/company/file
get *.* forfiles /D+0
put C:\loads\path
pause
I tried some alternatives seen in the WinSCP documentation, but I was not successful.
To download files from an FTP server using WinSCP from a batch file, you can do:
"C:\Program Files (x86)\WinSCP\WinSCP.com" /log=winscp.log /ini=nul /command ^
"open ftp://username:password#example.com/ ^
"cd /company/file" ^
"get *.*" ^
"put C:\loads\path\*" ^
"exit"
pause
See https://winscp.net/eng/docs/guide_automation.
Also WinSCP GUI can generate a FTP download batch file template for you.

WinSCP - Download file from SFTP server to network location, then delete

I need to write a WinSCP script or batch file that will download the contents of a remote SFTP folder, delete contents and then upload to a mapped network folder. I know how to write the login part of the script. But I am confused as to what the specific command line should look like. From what I've read the command script to download and delete the file is something like this:
get -delete -transfer=binary *
The part that I'm not getting is the upload to the network folder command. I understand put is the WinSCP upload command operator. But how do I use it in conjunction with the network folder location files should be uploaded to? Also, can this be scripted out in a batch file or do the batch file and script need to be written separately? Thanks!
An "upload" to a mapped network folder is not a job for WinSCP. A mapped network folder behaves as a local drive. A plain Windows copy command will do. So first do your WinSCP download and then copy (it's not really called an upload) the files to the network drive.
winscp /ini=nul /command ^
"open sftp://user:password#example.com/" ^
"get -delete /sftp/path/* c:\local\drive\path\" ^
"exit"
copy c:\local\drive\path\* n:\mapped\network\drive\path\
Though, if you do not need the files locally, you can have WinSCP download the files from the SFTP folder directly to the mapped network drive:
winscp /ini=nul /command ^
"open sftp://user:password#example.com/" ^
"get -delete /sftp/path/* n:\mapped\network\drive\path\" ^
"exit"

Executing WinSCP command in batch file

I am new to batch and trying to execute a script I wrote, essentially I want to go to a remote server copy the files there and transfer it to a folder in my local directory. I am executing the bactch file but nothing is being copied any suggestions would be great. This is my script
open sftp://site:#ftp.site.net -hostkey="server finger print"
synchronize local C:\Users\localdirectory\Desktop\test2 /Home/folderA/NewFiles
exit
I am positive all the information is correct because that's how I login with WinSCP. I got this script from https://www.youtube.com/watch?v=ndvEYOQLc4c
This is WinSCP script. There's no batch file in your question.
To run WinSCP script, use for example:
"C:\Program Files (x86)\WinSCP\WinSCP.com" /script="C:\path\to\winscp.txt"
(assuming your WinSCP script is in C:\path\to\winscp.txt).
You can put the above command to a batch file.
See guide to scripting with WinSCP.
You can also have WinSCP GUI generate both the script and batch file for you (or even a batch file that directly contains the WinSCP commands).

Downloading new files from FTP server to Windows in a batch file

I am writing a batch file that downloads files from an FTP server.
Now, I want these files to be checked against the existing files, so that they are not being downloaded again. There are multiple files being downloaded and I do not know the name of the files that may be present in the FTP server.
Thanks
Just use any Window command-line FTP client that supports synchronization.
For example with WinSCP FTP client, you can use a batch file like:
#echo off
winscp.com /ini=nul /log=synchronize.log /command ^
"open ftp://username:password#ftp.example.com/" ^
"synchronize local C:\local\path /remote/path" ^
"exit"
References:
Guide to automating synchronization from FTP server;
Scripting command synchronize.
You can have WinSCP GUI generate a batch file template for you.
(I'm the author of WinSCP)

Automated FTP to upload new files to web server?

I'm looking for a FTP client that I can use to upload new files from a local development machine to a remote web-server. I only want to upload the newly edited files though.
Is there a command line utility that can do this, that I can add into an automated process? Is there a GUI client available that can do this? Would be nice to have it cross-platform too. Any ideas?
The Mercurial FTP Extension should do this for you, although I haven't tried it myself.
There is a 'backup' program called SyncBack that does this.
You can find out more about it here: http://www.2brightsparks.com
You have two options:
Schedule a frequent synchronization of a local folder against a remote folder (or moving all files from local folder to a remote folder, if that's more appropriate)
Use a tool that can watch for changes in a local directory and reflect them on a remote directory
You can implement both these options with WinSCP FTP client.
Scheduling
To synchronize changes in a local directory to a remote directory, use the WinSCP synchronize script command from a batch file like:
winscp.com /ini=nul /log=c:\writable\path\to\synchronize.log /command ^
"open ftp://username:password#ftp.example.com/" ^
"synchronize remote C:\local\path /remote/path" ^
"exit"
And schedule the batch file to be run frequently using Windows scheduler.
If you do not want to keep a local copy of the images, just move them to the FTP server, instead of synchronizing them. For that, replace the
"synchronize remote C:\local\path /remote/path" ^
with the put -delete command like:
"put -delete C:\local\path\* /remote/path/" ^
For details see also a guide to automating file transfers (or synchronization) to FTP server.
Watching for changes
Use "Keep remote directory up to date" function of WinSCP.
It can be used both in command-line/console mode using the keepuptodate command, like:
winscp.com /ini=nul /log=c:\writable\path\to\synchronize.log /command ^
"open ftp://username:password#ftp.example.com/" ^
"keepuptodate C:\local\path /remote/path" ^
"exit"
Or in a graphical/GUI mode. You can launch the graphical mode in WinSCP GUI (after logging in) or from a command-line using the /keepuptodate switch like:
winscp.exe ftp://username:password#ftp.example.com/ /keepuptodate C:\local\path /remote/path
(I'm the author of WinSCP)

Resources