Uploading a list of files

Using WinSCP .NET Assembly

The following example uses WinSCP .NET assembly from a PowerShell script. If you have another preferred language, you can easily translate it.

Advertisements:
try
{
    # Load WinSCP .NET assembly
    Add-Type -Path "WinSCPnet.dll"
 
    # Setup session options
    $sessionOptions = New-Object WinSCP.SessionOptions -Property @{
        Protocol = [WinSCP.Protocol]::Sftp
        HostName = "example.com"
        UserName = "user"
        Password = "mypassword"
        SshHostKeyFingerprint = "ssh-rsa 2048 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx"
    }
 
    $session = New-Object WinSCP.Session
 
    try
    {
        # Connect
        $session.Open($sessionOptions)
 
        $remotePath = "/home/user/"
 
        $lines = Get-Content "list.txt"
 
        foreach ($line in $lines)
        {
            Write-Host ("Uploading {0} ..." -f $line)
            $session.PutFiles($line, $remotePath).Check()
        }
    }
    finally
    {
        # Disconnect, clean up
        $session.Dispose()
    }
 
    exit 0
}
catch [Exception]
{
    Write-Host ("Error: {0}" -f $_.Exception.Message)
    exit 1
}

Using WinSCP Scripting

You may use following batch file that calls WinSCP script:

@echo off
set SESSION=sftp://user:password@example.com/
set REMOTE_PATH=/home/user/
 
echo open %SESSION% >> script.tmp
 
rem Generate "put" command for each line in list file
for /F %%i in (list.txt) do echo put "%%i" "%REMOTE_PATH%" >> script.tmp
 
echo exit >> script.tmp
 
winscp.com /script=script.tmp
set RESULT=%ERRORLEVEL%
 
del script.tmp
 
rem Propagating WinSCP exit code
exit /b %RESULT%

Further Reading

 
  script_upload_file_list.txt · Last modified: by martin
 

Search Documentation

This page

Donate

About donations

$9   $19   $49   $99

About donations

Recommend

Associations

Site design by Black Gate