Post a reply

Options
Add an Attachment

If you do not want to add an Attachment to your Post, please leave the Fields blank.

(maximum 10 MB; please compress large files; only common media, archive, text and programming file formats are allowed)

Options

Topic review

martin

Re: FTP upload script only uploading single file from directory at a time. Want all the files to go up

Please attach a full session log file showing the problem (using the latest version of WinSCP).

To generate the session log file, set Session.SessionLogPath. Submit the log with your post as an attachment. Note that passwords and passphrases not stored in the log. You may want to remove other data you consider sensitive though, such as host names, IP addresses, account names or file names (unless they are relevant to the problem). If you do not want to post the log publicly, you can mark the attachment as private.
aron

FTP upload script only uploading single file from directory at a time. Want all the files to go up

Hi All I have this script and it works but only upload 1 file at a time on a 5min task schedule. I want it to upload all files in the directory every 5 min on a task schedule.

Not sure where I have gone wrong. Any help is appreciated.

param (
    $localPath = "C:\WinDSX\output\*",
    $remotePath = "/from-dsx/",
    $backupPath = "C:\WinDSX\Uploaded files",
    $logPath = "C:\WinDSX\log.txt"
)
try
{
    # Load WinSCP .NET assembly
    Add-Type -Path "C:\Program Files (x86)\WinSCP\WinSCPnet.dll"
 
    # Setup session options
    $sessionOptions = New-Object WinSCP.SessionOptions -Property @{
        Protocol = [WinSCP.Protocol]::Sftp
        HostName = "somedomain.com"
        UserName = "someusername"
        # Password = "mypassword"
        SshHostKeyFingerprint = "somehash"
        SshPrivateKeyPath = "someprivatekey"
    }
 
    $session = New-Object WinSCP.Session
 
    try
    {
        # Connect
        $session.Open($sessionOptions)
 
        # Upload files, collect results
        $transferResult = $session.PutFiles($localPath, $remotePath)
 
        # Iterate over every transfer
        foreach ($transfer in $transferResult.Transfers)
        {
            # Success or error?
            if ($transfer.Error -eq $Null)
            {
                $logMessage = "Upload of $($transfer.FileName) succeeded, moving to backup"
                Write-Host $logMessage
                Out-File -FilePath $logPath -Append -InputObject $logMessage
               
                # Upload succeeded, move source file to backup
                Move-Item $transfer.FileName $backupPath
            }
            else
            {
                $logMessage = "Upload of $($transfer.FileName) failed: $($transfer.Error.Message)"
                Write-Host $logMessage
                Out-File -FilePath $logPath -Append -InputObject $logMessage
            }
        }
 
        # Delete all files in the output directory except the log file
        # Get-ChildItem "C:\WinDSX\output" -Exclude (Split-Path -Leaf $logPath) | Remove-Item -Force
    }
 
    finally
    {
        # Disconnect, clean up
        $session.Dispose()
    }
 
    exit 0
}
catch
{
    $errorMessage = "Error: $($_.Exception.Message)"
    Write-Host $errorMessage
    Out-File -FilePath $logPath -Append -InputObject $errorMessage
    exit 1
}