Need Assistance Uploading All files within a Directory (while excluding subfolders)
I am trying to create a Powershell Script to upload all files within a directory, but every time I run the script manually, it only moves a single file. Here is part of what I have been able to build so far (minus SessionOptions):
param (
$localPath = "C:\Data\SFTP\xxx\to_xxx\*",
$remotePath = "/toxxx/*",
$backupPath = "C:\Data\SFTP\xxx\to_xxx\Transfered"
)
# Set up transfer options
$transferOptions = New-Object WinSCP.TransferOptions -Property @{
FileMask = "*.* | */"
}
$transferOptions.AddRawSettings("IgnorePermErrors", "1")
# Upload files, collect results
$transferResult = $session.PutFiles($localPath, $remotePath, $transferOptions )
# Iterate over every transfer
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)"
}
}
}
finally
{
# Disconnect, clean up
$session.Dispose()
}
exit 0
}
catch
{
Write-Host "Error: $($_.Exception.Message)"
exit 1
}
param (
$localPath = "C:\Data\SFTP\xxx\to_xxx\*",
$remotePath = "/toxxx/*",
$backupPath = "C:\Data\SFTP\xxx\to_xxx\Transfered"
)
# Set up transfer options
$transferOptions = New-Object WinSCP.TransferOptions -Property @{
FileMask = "*.* | */"
}
$transferOptions.AddRawSettings("IgnorePermErrors", "1")
# Upload files, collect results
$transferResult = $session.PutFiles($localPath, $remotePath, $transferOptions )
# Iterate over every transfer
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)"
}
}
}
finally
{
# Disconnect, clean up
$session.Dispose()
}
exit 0
}
catch
{
Write-Host "Error: $($_.Exception.Message)"
exit 1
}