Differences

This shows you the differences between the selected revisions of the page.

scripts 2009-04-10 scripts 2021-03-31 (current)
Line 1: Line 1:
====== Useful Scripts ====== ====== Useful Scripts ======
-===== [[upload]] Uploading 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 single file involves so little commands that it is enough to provide them on command line, saving writing a script file:+===== Other Examples ===== 
 +  * Guide to [[guide_automation|scripting/automation]]; 
 +  * Guide to [[guide_automation_advanced|advanced scripting]]; 
 +  * See [[library_examples|*]] for advanced tasks.
-<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 (proving you have saved the script to file ''example.txt''): 
- 
-<code> 
-winscp.com /script=example.txt 
-</code> 
- 
- 
-===== 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 [[http://www.robvanderwoude.com/datetiment.html|produce almost any date string]] using Windows command-line tools, easier is to use dedicated tool, such as [[http://www.huweb.hu/maques/realdate.htm|realdate]]: 
- 
-First create wrapper batch file to store the date string into environment variable: 
- 
-<code> 
-for /f %%T in ('realdate.com /f="CCYYMMDDhhmmss"') do (set TIMESTAMP=%%T) 
-winscp.com /script=example.txt 
-</code> 
- 
-The script ''example.txt'' can use syntax ''%TIMESTAMP%'' to retrieve the date string in ''TIMESTAMP'' environment variable: 
- 
-<code winscp> 
-option batch abort 
-option confirm off 
-open session 
-get /home/user/examplefile.txt *.%TIMESTAMP%.txt 
-exit 
-</code> 
- 
-==== 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: 
- 
-<code php> 
-option batch abort 
-option confirm off 
-open session 
-get /home/user/examplefile.txt *.<?=date("YmdHis")?>.txt 
-exit 
-</code> 
- 
-When executed, the generated WinSCP script file may look like: 
- 
-<code winscp> 
-option batch abort 
-option confirm off 
-open session 
-get /home/user/examplefile.txt *.20060605090825.txt 
-exit 
-</code> 
- 
-Now pass the generated script file as input to WinSCP: 
-<code> 
-php -q download.php | winscp.com /script="%temp%\download.tmp" 
-</code> 
- 
- 
-==== 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. 
- 
-<code winscp> 
-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 
-</code> 
- 
-===== Downloading the most recent file ===== 
-The script below requires opening separate [[shell session]] to invoke remote-side scripting. 
- 
-<code winscp> 
-option batch abort 
-option confirm off 
-open session 
-# Make copy of the most recent file using remote command to new name 
-# with unique extension to easily find the file in the temporary directory. 
-call cp `ls -t | head -1` /tmp/`ls -t | head -1`.latest 
-# 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/*.latest *. 
-# Remove the temporary file. 
-rm /tmp/*.latest 
-exit 
-</code> 
- 
-===== [[local_move]] 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 session 
-# 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]] 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): 
-<code> 
-set LOCAL=%1 
-set REMOTE="/home/user/www/%~n1%~x1" 
-winscp.com /script=example.txt 
-</code> 
- 
-The script ''example.txt'' may look like: 
- 
-<code winscp> 
-# Being intended for interactive session, we are not enabling batch mode 
-# Connect 
-open session 
-# Synchronize paths provided via environment variables 
-synchronize remote %LOCAL% %REMOTE% 
-</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. 

Last modified: by martin