Differences
This shows you the differences between the selected revisions of the page.
| scripts 2009-11-21 | scripts 2021-03-31 (current) | ||
| Line 1: | Line 1: | ||
| ====== Useful Scripts ====== | ====== Useful Scripts ====== | ||
| - | ~~SPLIT~~ | ||
| - | ===== [[upload_single_file]] Uploading a single file ===== | + | ··* [[script_upload_single_file|*]] |
| + | * [[script_upload_multiple_servers|*]] | ||
| + | * [[script_download_timestamped_filename|*]] | ||
| + | * [[script_download_most_recent_file|*]] | ||
| + | * [[script_upload_most_recent_file|*]] | ||
| + | * [[script_checking_file_existence|*]] | ||
| + | * [[script_local_move_after_successful_upload|*]] | ||
| + | * [[script_locking_files_while_uploading|*]] | ||
| + | * [[script_downloading_when_done_file_exists|*]] | ||
| + | * [[script_download_files_to_same_folder|*]] | ||
| + | * [[script_upload_file_list|*]] | ||
| + | * [[script_synchronize_any_local_file|*]] | ||
| + | * [[script_auto_compress_download|*]] | ||
| + | * [[script_custom_listing_format_csv|*]] | ||
| + | * [[script_retry|*]] | ||
| + | * [[script_formatting_timestamp_batch_file|*]] | ||
| + | * [[script_email|*]] | ||
| + | * [[script_vbnet_robust_example|*]] | ||
| - | Uploading a single file involves so few commands that it is enough to provide them on the command line, saving writing a script file: | ||
| - | |||
| - | <code> | ||
| - | winscp.com /command "option batch abort" "option confirm off" "open user@example.com" "put examplefile.txt /home/user/" "exit" | ||
| - | </code> | ||
| - | |||
| - | However you may want to use a script file anyway, so you can later expand it: | ||
| - | |||
| - | <code winscp> | ||
| - | option batch abort | ||
| - | option confirm off | ||
| - | open user@example.com | ||
| - | put examplefile.txt /home/user/ | ||
| - | exit | ||
| - | </code> | ||
| - | |||
| - | To run the script use following command (providing you have saved the script to file ''example.txt''): | ||
| - | |||
| - | <code> | ||
| - | winscp.com /script=example.txt | ||
| - | </code> | ||
| - | |||
| - | |||
| - | ===== [[download_timestamped_filename]] Downloading file to timestamped-filename ===== | ||
| - | |||
| - | You may use following [[guide_automation_advanced#wsh|Windows script host JavaScript code]] (''example.js''): | ||
| - | |||
| - | <code javascript> | ||
| - | // Local path to download to (keep trailing slash) | ||
| - | var LOCALPATH = ".\\"; | ||
| - | // Remote path to download from (keep trailing slash) | ||
| - | var REMOTEPATH = "/"; | ||
| - | // File to download | ||
| - | var FILE = "winscp421.exe"; | ||
| - | // Session to connect to | ||
| - | var SESSION = "test@127.0.0.1"; | ||
| - | // Path to winscp.com | ||
| - | var WINSCP = "c:\\program files\\winscp\\winscp.com"; | ||
| - | |||
| - | // helper function to pad zeroes to the left of number | ||
| - | function pad(n, len) | ||
| - | { | ||
| - | var s = n.toString(); | ||
| - | while (s.length < len) | ||
| - | { | ||
| - | s = '0' + s; | ||
| - | } | ||
| - | return s; | ||
| - | } | ||
| - | |||
| - | var date = new Date(); | ||
| - | |||
| - | // format timestamp | ||
| - | var stamp = | ||
| - | pad(date.getFullYear(), 4) + | ||
| - | pad(date.getMonth(), 2) + | ||
| - | pad(date.getDate(), 2) + | ||
| - | pad(date.getHours(), 2) + | ||
| - | pad(date.getMinutes(), 2) + | ||
| - | pad(date.getSeconds(), 2); | ||
| - | |||
| - | var shell = WScript.CreateObject("WScript.Shell"); | ||
| - | |||
| - | // run winscp to get list of file in the remote directory into XML log | ||
| - | exec = shell.Exec("\"" + WINSCP + "\""); | ||
| - | exec.StdIn.Write( | ||
| - | "option batch abort\n" + | ||
| - | "open \"" + SESSION + "\"\n" + | ||
| - | "get \"" + REMOTEPATH + FILE + "\" \"" + LOCALPATH + FILE + "." + stamp + "\"\n" + | ||
| - | "exit\n"); | ||
| - | |||
| - | // wait until the script finishes | ||
| - | while (exec.Status == 0) | ||
| - | { | ||
| - | WScript.Sleep(100); | ||
| - | WScript.Echo(exec.StdOut.ReadAll()); | ||
| - | } | ||
| - | </code> | ||
| - | |||
| - | Run the script with command: | ||
| - | <code> | ||
| - | cscript /nologo example.js | ||
| - | </code> | ||
| - | |||
| - | ===== [[download_most_recent_file]] Downloading the most recent file ===== | ||
| - | You may use following [[guide_automation_advanced#wsh|Windows script host JavaScript code]] (''example.js''): | ||
| - | |||
| - | <code javascript> | ||
| - | // Configuration | ||
| - | |||
| - | // Local path to download to (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.Status == 0) | ||
| - | { | ||
| - | WScript.Sleep(100); | ||
| - | 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); | ||
| - | } | ||
| - | </code> | ||
| - | |||
| - | Run the script with command: | ||
| - | <code> | ||
| - | cscript /nologo example.js | ||
| - | </code> | ||
| - | |||
| - | ===== [[checking_file_existence]] Checking file existence ===== | ||
| - | You may use following [[guide_automation_advanced#wsh|Windows script host JavaScript code]] (''example.js''): | ||
| - | |||
| - | <code javascript> | ||
| - | // Configuration | ||
| - | |||
| - | // Remote file search for | ||
| - | var FILEPATH = "/home/user/myfile.dat"; | ||
| - | // 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 p = FILEPATH.lastIndexOf('/'); | ||
| - | var path = FILEPATH.substring(0, p); | ||
| - | var filename = FILEPATH.substring(p + 1); | ||
| - | |||
| - | var exec; | ||
| - | |||
| - | // run winscp to check for file existence | ||
| - | exec = shell.Exec("\"" + WINSCP + "\" /log=\"" + logfilepath + "\""); | ||
| - | exec.StdIn.Write( | ||
| - | "option batch abort\n" + | ||
| - | "open \"" + SESSION + "\"\n" + | ||
| - | "ls \"" + path + "\"\n" + | ||
| - | "exit\n"); | ||
| - | |||
| - | // wait until the script finishes | ||
| - | while (exec.Status == 0) | ||
| - | { | ||
| - | WScript.Sleep(100); | ||
| - | WScript.Echo(exec.StdOut.ReadAll()); | ||
| - | } | ||
| - | |||
| - | if (exec.ExitCode != 0) | ||
| - | { | ||
| - | WScript.Echo("Error checking for file existence"); | ||
| - | 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/w:filename[@value='" + filename + "']"); | ||
| - | |||
| - | if (nodes.length > 0) | ||
| - | { | ||
| - | WScript.Echo("File found"); | ||
| - | // signalize file existence to calling process; | ||
| - | // you can also continue with processing (e.g. downloading the file) | ||
| - | // directly fron the script here | ||
| - | WScript.Quit(0); | ||
| - | } | ||
| - | else | ||
| - | { | ||
| - | WScript.Echo("File not found"); | ||
| - | WScript.Quit(1); | ||
| - | } | ||
| - | </code> | ||
| - | |||
| - | Run the script with command: | ||
| - | <code> | ||
| - | cscript /nologo example.js | ||
| - | </code> | ||
| - | |||
| - | Following batch file shown how to continue with processing based on file exitence: | ||
| - | <code> | ||
| - | cscript /nologo example.js | ||
| - | if errorlevel 1 goto error | ||
| - | |||
| - | echo File exists, do something | ||
| - | rem Do something | ||
| - | exit | ||
| - | |||
| - | :error | ||
| - | echo Error or file not exists | ||
| - | </code> | ||
| - | |||
| - | Alternatively you can continue with the processing directly in JavaScript code as suggested by respective comments. | ||
| - | |||
| - | ===== [[local_move_after_successful_upload]] 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: | ||
| - | |||
| - | <code winscp> | ||
| - | option batch abort | ||
| - | option confirm off | ||
| - | # Connect | ||
| - | open mysession | ||
| - | # Upload the files | ||
| - | put *.* | ||
| - | </code> | ||
| - | |||
| - | Launch the above script from batch file like the one below: | ||
| - | |||
| - | <code> | ||
| - | 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 | ||
| - | </code> | ||
| - | |||
| - | ===== [[synchronize_any_local_file]] Shortcut to synchronize any local directory 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): | ||
| - | <code> | ||
| - | winscp.com /script=example.txt /parameter %1 "/home/user/www/%~n1%~x1" | ||
| - | </code> | ||
| - | |||
| - | The script ''example.txt'' may look like: | ||
| - | |||
| - | <code winscp> | ||
| - | # Being intended for interactive session, we are not enabling batch mode | ||
| - | # Connect | ||
| - | open mysession | ||
| - | # Synchronize paths provided via environment variables | ||
| - | synchronize remote %1% %2% | ||
| - | </code> | ||
| - | |||
| - | 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. | ||
| - | |||
| - | ===== [[auto_compress_download]] Automatically compress files before download ===== | ||
| - | Following script compresses selected files into tar/gzip archive and downloads it: | ||
| - | |||
| - | <code winscp> | ||
| - | option batch abort | ||
| - | option confirm off | ||
| - | open mysession | ||
| - | cd %1% | ||
| - | call tar -czf /tmp/archive.tar.gz %2% | ||
| - | lpwd | ||
| - | get -delete /tmp/archive.tar.gz | ||
| - | exit | ||
| - | </code> | ||
| - | |||
| - | Launch the above script from batch file like the one below: | ||
| - | |||
| - | <code> | ||
| - | winscp.com /script=example.txt /parameter %* | ||
| - | if errorlevel 1 goto error | ||
| - | |||
| - | echo Retrieving files succeeded | ||
| - | gzip -d archive.tar.gz | ||
| - | tar -xf archive.tar | ||
| - | del archive.tar | ||
| - | exit | ||
| - | |||
| - | :error | ||
| - | echo Retrieving files failed | ||
| - | </code> | ||
| - | |||
| - | Example of running the batch file to download all files under ''/home/user/www'': | ||
| - | |||
| - | <code> | ||
| - | example.bat /home/user/www *.* | ||
| - | </code> | ||
| - | |||
| - | The batch file needs Windows ports of ''gzip'' and ''tar'' tools. You can get them from [[http://sourceforge.net/projects/unxutils/|UnxUtils]] project. | ||
| - | |||
| - | <trailer> | ||
| - | ===== Further Reading ===== | ||
| - | * Guide to [[guide_automation|scripting/automation]]. | ||
| - | </trailer> | ||
| - | |||
| - | <nosplit> | ||
| ===== Other Examples ===== | ===== Other Examples ===== | ||
| * Guide to [[guide_automation|scripting/automation]]; | * Guide to [[guide_automation|scripting/automation]]; | ||
| - | * Guide to [[guide_automation_advanced|advanced scripting]]. | + | * Guide to [[guide_automation_advanced|advanced scripting]]; |
| - | </nosplit> | + | ··* See [[library_examples|*]] for advanced tasks. |