Search code examples
powershellftpwinscpwinscp-net

Do not transfer whole local folder to remote directory using WinSCP but only files in it


I made an PowerShell script that transfers .txt and .csv files from local folders to remote directories.

I would like to exclude the subdirectories and transfer only the .txt and .csv files from my local folders.

$transferOptions.FileMask = "*.txt, *.csv |*/";

If I add here the exclude mask | */ the scripts does nothing. No files are transferred or excluded.

If I remove it, the script works, but the whole local info folder and its files are transferred to the remote directory.

I just want the .txt and .csv files to be remotely transferred and not the local folder itself.

This is how the transfer part of my script looks like:

# Set the folder name variables
$DirCustomerData = @("001201", "001753", "001952", "002587", "002833", "002863", "003150", "003716", 

"004826", "003010", "004099", "006222", "002545", "006805", "001433", "006839")

$LocalDir = "\customer_data\"+$DirCustomerData[$i]+"\info\"

# Check for files
if((Get-ChildItem $LocalDir | Measure-Object).Count -eq 0)
{
    "Local directory $LocalDir has currently no files. Skipping to next folder"
    "`n"
} else {
    "Local directory $LocalDir contains "+(Get-ChildItem $LocalDir -Recurse -File -Exclude -Directory | Measure-Object | %{$_.Count})+" files. Starting now FTP Upload Session..."
    "`n"
    
    # Retrieve the local files
    $LocalDirFiles = Get-ChildItem $LocalDir

    # Open the FTP Session
    $session = New-Object WinSCP.Session
    $session.Open($sessionOptions)

    # Upload the files
    $transferOptions = New-Object WinSCP.TransferOptions
    $transferOptions.TransferMode = [WinSCP.TransferMode]::Binary
    $transferOptions.FileMask = "*.txt, *.csv";

    $RemoteDir = "/"+$DirCustomerData[$i]+"/info/"

    foreach ($LocalDirFiles in $LocalDir) {

        $transferResult = $session.PutFiles($LocalDir, $RemoteDir, $true, $transferOptions)

        # Throw on any error
        $transferResult.Check()

        # Print results
        foreach ($transfer in $transferResult.Transfers)
        {
            Write-Host "Upload of $($transfer.FileName) to remote FTP directory $RemoteDir succeeded."
        }
    }       
}

Could you help me here to transfer only the required files and exclude the subdirectories? Thanks!


Solution

  • The documentation for localPath argument of Session.PutFiles says:

    Full path to local file or directory to upload. Filename in the path can be replaced with Windows wildcard to select multiple files. To upload all files in a directory, use mask *.

    Your code is missing the mask, it should be:

    $LocalDir = "\customer_data\"+$DirCustomerData[$i]+"\info\*"
    

    Note the *. And the localDir variable name does not math the value's purpose.


    More straightforward is to use Session.PutFilesToDirectory, which would work with your current path values:

    $session.PutFilesToDirectory($LocalDir, $RemoteDir, $true, $transferOptions)
    

    See also https://winscp.net/eng/docs/faq_script_vs_gui#inputs