2

I am using the following script to copy files from my local folder to SFTP. Once I upload the file I then want to move the file to a subfolder. I want to upload only files in the C:\Users\Administrator\Desktop\ftp folder, and not files in the other subfolders.

param (
    $backupPath = "C:\Users\Administrator\Desktop\ftp\moved"
)

# Load the Assembly and setup the session properties
try
{
    # Load WinSCP .NET assembly
    Add-Type -Path "C:\Program Files (x86)\WinSCP\WinSCPnet.dll" 

    $session = New-Object WinSCP.Session

    $filelist = Get-ChildItem C:\Users\Administrator\Desktop\ftp 

    # Connect And send files, then close session
    try
    {
        # Connect
        $session.Open($sessionOptions)

        $transferOptions = New-Object WinSCP.TransferOptions
        $transferOptions.TransferMode = [WinSCP.TransferMode]::Binary

        foreach ($file in $filelist)
        {
            $transferResult = $session.PutFiles("C:\Users\Administrator\Desktop\ftp\$file", "/", $False, $transferOptions)
            foreach ($transfer in $transferResult.Transfers)
            {
                Write-Host "Upload of $($transfer.FileName) succeeded"
                Move-Item $transfer.FileName $backupPath
            }
        }
    }
    finally
    {
        # Disconnect, clean up
        $session.Dispose()
    }

    exit 0
}
# Catch any errors
catch
{
    Write-Host "Error: $($_.Exception.Message)"
    exit 1
}

At this moment, if I run the script all files under the moved folder will also get uploaded through SFTP folder, and I just need to upload the files in the root directory of the ftp folder.

I guess I will need to change this line here, but not sure on how to change it.

$filelist = Get-ChildItem C:\Users\Administrator\Desktop\ftp 
1
  • 1
    Get-ChildItem -File - only returns file objects Commented Nov 8, 2019 at 5:55

2 Answers 2

2

To skip the folders add -File switch to the Get-ChildItem (as already commented by @Scepticalist):

$filelist = Get-ChildItem -File C:\Users\Administrator\Desktop\ftp 

Though you can achieve the same with less code, if you let WinSCP iterate the files (and skip the folders):

$transferOptions = New-Object WinSCP.TransferOptions
$transferOptions.TransferMode = [WinSCP.TransferMode]::Binary
$transferOptions.FileMask = "|*/" # Exclude the folders

$transferResult = $session.PutFiles(
    "C:\Users\Administrator\Desktop\ftp\*", "/", $False, $transferOptions)
foreach ($transfer in $transferResult.Transfers)
{
    Write-Host "Upload of $($transfer.FileName) succeeded"
    Move-Item $transfer.FileName $backupPath
}

And you do not need any Get-ChildItem call.

The above is basically the code from WinSCP article Moving local files to different location after successful upload, just with an exclusion of subfolders.


Though note that your code (contrary to the article) does not have a test for a successful upload. So you will move even files that fail to upload. Make sure you add the $transfer.Error -eq $Null test.

foreach ($transfer in $transferResult.Transfers)
{
    # Success or error?
    if ($transfer.Error -eq $Null)
    {
        Write-Host "Upload of $($transfer.FileName) succeeded, moving to backup"
        # Upload succeeded, move source file to backup
        Move-Item $transfer.FileName $backupPath
    }
    else
    {
        Write-Host "Upload of $($transfer.FileName) failed: $($transfer.Error.Message)"
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

try to use

Get-Item C:\Users\Administrator\Desktop\ftp -include *.*

this should not get into subfolders.

1 Comment

thank you, unfortunately when I try this, it still gets into subfolders

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.