问题
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:
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)"
}
}
回答2:
try to use
Get-Item C:\Users\Administrator\Desktop\ftp -include *.*
this should not get into subfolders.
来源:https://stackoverflow.com/questions/58758410/powershell-script-to-copy-files-but-not-subfolders-to-sftp-and-move-to-subfolder