This is an old revision of the document!

Useful Scripts

Advertisement

Uploading single file

Upload single file involves so little commands that it it enough to provide them on command line, saving writing a script file:

winscp.com user@example.com /command "put examplefile.txt /home/user/" "exit"

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 commmand-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 user@example.com /script=example.txt

The script example.txt can use syntax %TIMESTAMP% to retrieve the date string in TIMESTAMP environment variable:

get /home/user/examplefile.txt *.%TIMESTAMP%.txt
exit

Advertisement

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:

get /home/user/examplefile.txt *.<?=date("YmdHis")?>.txt
exit

When executed, the generated WinSCP script file may look like:

get /home/user/examplefile.txt *.20060605090825.txt
exit

Now pass the generated script file as input to WinSCP:

php -q download.php | winscp.com user@example.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.

# 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

Downloading the most recent file

The script below requires opening separate shell session to invoke remote-side scripting.

# 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

Advertisement

Moving local files to different location after upload

WinSCP does not support move command for local files. Instead you can combine WinSCP script with batch file:

# Make the script abort on any error
option batch abort
# 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

Last modified: by martin