This is an old revision of the document!
Generate Session URL/Code/Transfer Code Dialog
Generate session URL/code dialog is used to generate a session URL, a file URL or a code template.
To open the dialog to generate the session URL or code, use:
- Session > Generate Session URL/Code command on the main window to generate session URL or code template for the current session. (preferred)
- Manage > Generate Session URL/Code command on the Login dialog to generate session URL or code template for the selected site.
To open the dialog to generate a file transfer code, use:
- Transfer Settings > Generate Code command on the Transfer Options dialog.
To open the dialog to generate the file URL, use:
- File(s) > File Names > Generate File URL command on main window to generate file URL for files selected in the remote file panel.
The dialog has three tabs. Each is described individually.
open sftp:user2:password@example.com/ # Change the remote directory cd /home/user2 # Upload the file to current working directory put d:\examplefile.txt # Disconnect close # Exit WinSCP exit
Script Tab
Use the Script tab to generate a WinSCP script template for the session or the file transfer.
The script will include:
- the
open
command with the session URL and all other switches needed to configure the session. - (When generating a code for a session) a placeholder text to be replaced with commands.
- (When generating a code for a file transfer) the
cd
andlcd
commands to navigate to the source and destination folders of the transfer and theput
orget
commands to actually transfer the files (with all needed switches to setup any non-default transfer settings).- If you had all files selected in the source panel, a code to upload all files from the source directory will be generated.
- If you had only some files selected in the source panel, a code to upload only the selected files (but three at most) will be generated. If file names contain any file mask special characters, they will be escaped.
- the
exit
command to end the script.
Use the Format to select a format of the script to generate:
- Script file – Generates a script file template that you execute using the
/script
command-line switch. - Batch file – Generates a Windows batch file (
.bat
) template that executes thewinscp.com
with the above commands, specified using the/command
command-line switch. Additionally the batch file enables session logging using the/log
switch and isolates the script run from a GUI configuration using the/ini=nul
switch. At the end, the batch file checks WinSCP exit code to choose further actions to take; and propagates the WinSCP exit code to a batch file exit code using theexit /b
command. Make sure you keep the exact format of the multi-linewinscp.com
command, as multi-line batch file commands are fragile. - Command-line – Generates a command-line for the
winscp.exe
(but usable forwinscp.com
too) with the above commands, session logging and configuration isolation (see the previous point). The command-line is suitable for task managers of all kinds, like Windows Scheduler or SSIS. Depending on your requirements, you may want prepend the/console
switch to make the script console visible, while the script is running. - PowerShell script – Generates a PowerShell script file (
.ps1
) template that executes thewinscp.com
with the above commands, specified using the/command
command-line switch. Additionally the script file enables session logging using the/log
switch and isolates the script run from a GUI configuration using the/ini=nul
switch. At the end, the script file checks WinSCP exit code to choose further actions to take; and propagates the WinSCP exit code to a script exit code using theexit
keyword. Instead of runningwinscp.com
, consider using WinSCP .NET assembly in PowerShell.
In the Command-line (and possibly also in the Batch file), you should modify a path to a session log file in the /log
switch.
The script will not include options to verify a host key or an untrusted certificate, if the session was not opened before yet. Open the session at least once, before generating the script.
You can click on script commands and switches and command-line switches to open their respective help pages.
.NET Assembly Code Tab
Use the .NET assembly tab to generate a code template using WinSCP .NET assembly for the session or the file transfer.
The code will:
- Set up
SessionOptions
with all properties needed to configure the session. - Create
Session
instance. - Open the session using
Session.Open
- (When generating a code for a session) Include a placeholder text to be replaced with calls to
Session
methods. - (When generating a code for a file transfer) Transfer files using
Session.PutFiles
orSession.GetFiles
. TheTransferOptions
class instance will be constructed and set up, if any non-default transfer settings are used. TheOperationResultBase.Check
method will be called on results of the methods to throw an exception on any error.- If you had all files selected in the source panel, a code to upload all files from the source directory will be generated.
- If you had only some files selected in the source panel, a code to upload only the selected files (but three at most) will be generated. If file names contain any file mask special characters, the
RemotePath.EscapeFileMask
method will be called to escape them.
- Dispose the
Session
instance.
Use the Language to select a language to generate the template for. Available languages are C#, VB.NET and PowerShell.
The code will not set properties to verify a host key (SessionOptions.SshHostKeyFingerprint
) or an untrusted certificate (SessionOptions.TlsHostCertificateFingerprint
), if the session was not opened before yet. Open the session at least once, before generating the code.
You can click on classes, methods and properties to open their respective help pages.
Copying Generated URL or Code to Clipboard
Use Copy to Clipboard// to copy generated URL(s) or code to a clipboard.