This is an old revision of the document!
Useful Scripts
- Uploading single file
- Downloading file to timestamped-filename
- Downloading the most recent file
- Moving local files to different location after successful upload
- Shortcut to synchronize any local file with remote directory
- Other Examples
Advertisement
Uploading single file
Uploading single file involves so little commands that it is enough to provide them on command line, saving writing a script file:
winscp.com /command "option batch abort" "option confirm off" "open user@example.com" "put examplefile.txt /home/user/" "exit"
However you may want to use a script file anyway, so you can later expand it:
option batch abort option confirm off open user@example.com put examplefile.txt /home/user/ exit
To run the script use following command (proving you have saved the script to file example.txt
):
winscp.com /script=example.txt
Downloading file to timestamped-filename
Using local-side date printing tool
You can use any way to produce date string in desired format on command line, store that string to environment variable and use it in script. Although it is possible to produce almost any date string using Windows command-line tools, easier is to use dedicated tool, such as realdate:
First create wrapper batch file to store the date string into environment variable:
for /f %%T in ('realdate.com /f="CCYYMMDDhhmmss"') do (set TIMESTAMP=%%T) winscp.com /script=example.txt
Advertisement
The script example.txt
can use syntax %TIMESTAMP%
to retrieve the date string in TIMESTAMP
environment variable:
option batch abort option confirm off open session get /home/user/examplefile.txt *.%TIMESTAMP%.txt exit
Using local-side scripting
You can use any available scripting language you have on the local host to generate appropriate WinSCP script. Following example uses PHP language:
option batch abort option confirm off open session get /home/user/examplefile.txt *.<?=date("YmdHis")?>.txt exit
When executed, the generated WinSCP script file may look like:
option batch abort option confirm off open session get /home/user/examplefile.txt *.20060605090825.txt exit
Now pass the generated script file as input to WinSCP:
php -q download.php | winscp.com /script="%temp%\download.tmp"
Using remote-side scripting
If you do not have a scripting language on the local host, you can use remote-side script (like shell script). This approach requires opening separate shell session to invoke remote-side scripting.
option batch abort option confirm off open session # Make copy of the remote file to temporary timestamped file. # Also add unique extension to easily find the file in the temporary directory. call cp /home/user/examplefile.txt /tmp/examplefile.`date +%Y%m%d%H%M%S`.unique # Download all the files with the unique extension. # There should be only one, the one just created. # While downloading, remove the unique extension. get /tmp/*.unique *. # Remove the temporary file. rm /tmp/*.unique exit
Advertisement
Downloading the most recent file
You may use following Windows script host JavaScript code (example.js
):
// Configuration // Local path to download for (keep trailing slash) var LOCALPATH = "c:\\downloaded\\"; // Remote path to search in (keep trailing slash) var REMOTEPATH = "/home/user/"; // Mask of files to search for var FILEMASK = "*.*"; // Session to connect to var SESSION = "session"; // Path to winscp.com var WINSCP = "c:\\program files\\winscp\\winscp.com"; var filesys = WScript.CreateObject("Scripting.FileSystemObject"); var shell = WScript.CreateObject("wscript.shell"); var logfilepath = filesys.GetSpecialFolder(2) + "\\" + filesys.GetTempName() + ".xml"; var exec; // run winscp to get list of file in the remote directory into XML log exec = shell.Exec("\"" + WINSCP + "\" /log=\"" + logfilepath + "\""); exec.StdIn.Write( "option batch abort\n" + "open \"" + SESSION + "\"\n" + "ls \"" + REMOTEPATH + FILEMASK + "\"\n" + "exit\n"); // wait until the script finishes while (!exec.StdOut.AtEndOfStream) { WScript.Echo(exec.StdOut.ReadAll()); } if (exec.ExitCode != 0) { WScript.Echo("Error retrieving list of files"); WScript.Quit(1); } // look for log file var logfile = filesys.GetFile(logfilepath); if (logfile == null) { WScript.Echo("Cannot find log file"); WScript.Quit(1); } // parse XML log file var doc = new ActiveXObject("MSXML2.DOMDocument"); doc.async = false; doc.load(logfilepath); doc.setProperty("SelectionNamespaces", "xmlns:w='http://winscp.net/schema/session/1.0'"); var nodes = doc.selectNodes("//w:file"); // find the latest file var filenameLatest = null; var modificationLatest = null; for (var i = 0; i < nodes.length; ++i) { var filename = nodes[i].selectSingleNode("w:filename/@value"); var modification = nodes[i].selectSingleNode("w:modification/@value"); if ((filename != null) && (filename.value != ".") && (filename.value != "..") && (modification != null)) { // can compare timestamps stringwise if ((modificationLatest == null) || (modificationLatest < modification.value)) { modificationLatest = modification.value; filenameLatest = filename.value; } } } // no file in the log if (filenameLatest == null) { WScript.Echo("No file found"); WScript.Quit(0); } // run winscp to download the latest file exec = shell.Exec("\"" + WINSCP + "\""); exec.StdIn.Write( "option batch abort\n" + "option confirm off\n" + "open \"" + session + "\"\n" + "get \"" + REMOTEPATH + filenameLatest + "\" \"" + LOCALPATH + "\"\n" + "exit\n"); // wait until the script finishes while (!exec.StdOut.AtEndOfStream) { WScript.Echo(exec.StdOut.ReadAll()); } if (exec.ExitCode != 0) { WScript.Echo("Error downloading " + filenameLatest); WScript.Quit(1); }
Advertisement
Run the script with command:
cscript /nologo example.js
Moving local files to different location after successful upload
WinSCP does not support move command for local files. Instead you can combine WinSCP script with batch file:
option batch abort option confirm off # Connect open session # Upload the files put *.*
Launch the above script from batch file like the one below:
winscp.com /script=example.txt if errorlevel 1 goto error echo Upload succeeded, moving local files move *.* c:\backup\ exit :error echo Upload failed, keeping local files
Shortcut to synchronize any local file with remote directory
You may want to have script that synchronizes any local directory with a same-named subdirectory of fixed remote path, e.g.:
c:\www\gallery
with/home/user/www/gallery
;c:\documents and settings\martin\www\forum
with/home/user/www/forum
.
Such script is particularly useful for integrating with Windows Explorer’s ‘Send To’ menu.
First create wrapper batch file to store the paths you want to synchronize into environment variables (change /home/user/www/
to remote path root you want to synchronize against):
set LOCAL=%1 set REMOTE="/home/user/www/%~n1%~x1" winscp.com /script=example.txt
Advertisement
The script example.txt
may look like:
# Being intended for interactive session, we are not enabling batch mode # Connect open session # Synchronize paths provided via environment variables synchronize remote %LOCAL% %REMOTE%
Then you can make a shortcut to the batch file:
- When placed on desktop, you can drop any local directory to it to start synchronization;
- When placed to
c:\documents and settings\username\sendto
, you can use //Send To > Your Shortcut’' from context menu of any local directory.
Other Examples
- Guide to automation;
- Guide to advanced scripting.