我做了一个PowerShell脚本,可以将.txt和.csv文件从本地文件夹传输到远程目录。
我想排除子目录,只传输本地文件夹中的.txt和.csv文件。
$transferOptions.FileMask = "*.txt, *.csv |*/";
如果我在这里添加排除掩码| */
,脚本什么也不做,没有文件被传输或排除。
如果我删除它,脚本就可以工作,但是**整个本地info
**文件夹及其文件会转移到远程目录。
我只是想远程传输.txt和.csv文件,而不是本地文件夹本身。
下面是我的脚本的传输部分:
# 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."
}
}
}
你能帮我这里只转移所需的文件,并排除子目录?谢谢!
1条答案
按热度按时间ou6hu8tu1#
Session.PutFiles
的localPath
参数文档说明:要上载的本地文件或目录的完整路径。路径中的文件名可以替换为Windows通配符以选择多个文件。要上载目录中的所有文件,请使用掩码
*
。您的代码缺少掩码,掩码应为:
注意
*
,localDir
变量名与该值的用途不符。更直接的方法是使用
Session.PutFilesToDirectory
,它可以处理当前路径值:另请参见https://winscp.net/eng/docs/faq_script_vs_gui#inputs