PowerShell脚本将文件而不是子文件夹复制到SFTP并在完成后移动到子文件夹 [英] PowerShell script to copy files but not subfolders to SFTP and move to subfolder once done
问题描述
我正在使用以下脚本将文件从我的本地文件夹复制到 SFTP.上传文件后,我想将文件移动到子文件夹.我只想上传 C:\Users\Administrator\Desktop\ftp
文件夹中的文件,而不上传其他子文件夹中的文件.
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
}
此时,如果我运行脚本,moved文件夹下的所有文件也会通过SFTP文件夹上传,我只需要上传根目录下的文件ftp
文件夹.
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
推荐答案
要跳过文件夹,将 -File
开关添加到 Get-ChildItem
(就像已经@Scepticalist 评论):
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
<小时>
虽然您可以用更少的代码实现相同的效果,但如果您让 WinSCP 迭代文件(并跳过文件夹):
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
}
而且您不需要任何 Get-ChildItem
调用.
And you do not need any Get-ChildItem
call.
以上基本上是WinSCP文章上传成功后移动本地文件到不同位置的代码,只需排除子文件夹.
The above is basically the code from WinSCP article Moving local files to different location after successful upload, just with an exclusion of subfolders.
但请注意,您的代码(与文章相反)没有成功上传的测试.因此,您甚至会移动无法上传的文件.确保添加 $transfer.Error -eq $Null
测试.
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)"
}
}
这篇关于PowerShell脚本将文件而不是子文件夹复制到SFTP并在完成后移动到子文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!