PowerShell 脚本将文件而非子文件夹复制到 SFTP 并在完成后移动到子文件夹
PowerShell script to copy files but not subfolders to SFTP and move to subfolder once done
我正在使用以下脚本将文件从我的本地文件夹复制到 SFTP。上传文件后,我想将文件移动到子文件夹。我只想上传 C:\Users\Administrator\Desktop\ftp
文件夹中的文件,而不上传其他子文件夹中的文件。
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
}
此时,如果我运行脚本,moved文件夹下的所有文件也会通过SFTP文件夹上传,我只需要上传文件在 ftp
文件夹的根目录中。
我想我需要在这里更改这一行,但不确定如何更改它。
$filelist = Get-ChildItem C:\Users\Administrator\Desktop\ftp
尝试使用
Get-Item C:\Users\Administrator\Desktop\ftp -include *.*
这不应该进入子文件夹。
要跳过文件夹,请添加 -File
切换到 Get-ChildItem
(@Scepticalist 已经评论):
$filelist = Get-ChildItem -File C:\Users\Administrator\Desktop\ftp
尽管您可以用更少的代码实现同样的效果,但如果您让 WinSCP 迭代文件(并跳过文件夹):
$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
}
而且您不需要任何 Get-ChildItem
电话。
以上基本上是WinSCP文章Moving local files to different location after successful upload, just with an exclusion of subfolders.
中的代码
但请注意,您的代码(与文章相反)没有成功上传的测试。因此,您甚至会移动上传失败的文件。确保添加 $transfer.Error -eq $Null
测试。
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)"
}
}
我正在使用以下脚本将文件从我的本地文件夹复制到 SFTP。上传文件后,我想将文件移动到子文件夹。我只想上传 C:\Users\Administrator\Desktop\ftp
文件夹中的文件,而不上传其他子文件夹中的文件。
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
}
此时,如果我运行脚本,moved文件夹下的所有文件也会通过SFTP文件夹上传,我只需要上传文件在 ftp
文件夹的根目录中。
我想我需要在这里更改这一行,但不确定如何更改它。
$filelist = Get-ChildItem C:\Users\Administrator\Desktop\ftp
尝试使用
Get-Item C:\Users\Administrator\Desktop\ftp -include *.*
这不应该进入子文件夹。
要跳过文件夹,请添加 -File
切换到 Get-ChildItem
(@Scepticalist 已经评论):
$filelist = Get-ChildItem -File C:\Users\Administrator\Desktop\ftp
尽管您可以用更少的代码实现同样的效果,但如果您让 WinSCP 迭代文件(并跳过文件夹):
$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
}
而且您不需要任何 Get-ChildItem
电话。
以上基本上是WinSCP文章Moving local files to different location after successful upload, just with an exclusion of subfolders.
中的代码但请注意,您的代码(与文章相反)没有成功上传的测试。因此,您甚至会移动上传失败的文件。确保添加 $transfer.Error -eq $Null
测试。
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)"
}
}