2015-11-20 49 views
1

我有一個調用WinSCP .NET程序集的腳本。該腳本從FTP目錄下載最新的文件,並根據其文件擴展名+ .txt2245.xml - >xml.txt)對其進行命名。我需要修改我的WinSCP腳本以僅下載特定文件擴展名的文件

我需要創建一個篩選器,只下載名爲tn*nc1的文件擴展名。任何人都可以點我在正確的方向:

$session = New-Object WinSCP.Session 

# Connect 
$session.Open($sessionOptions) 

# Get list of files in the directory 
$directoryInfo = $session.ListDirectory($remotePath) 

# Select the most recent file 
$latest = $directoryInfo.Files | 
    Where-Object { -Not $_.IsDirectory} | 
    Group-Object { [System.IO.Path]::GetExtension($_.Name) } | 
    ForEach-Object{ 
     $_.Group | Sort-Object LastWriteTime -Descending | Select -First 1 
    } 

$extension = [System.IO.Path]::GetExtension($latest.Name) 
"GetExtension('{0}') returns '{1}'" -f $fileName, $extension 

if ($latest -eq $Null) 
{ 
    Write-Host "No file found" 
    exit 1 
} 

# Download 

$latest | ForEach-Object { 
    $extension = ([System.IO.Path]::GetExtension($_.Name)).Trim(".") 
    $session.GetFiles($session.EscapeFileMask($remotePath + $_.Name), "$localPath\$extension.txt").Check() 
} 

我試着在分揀目錄中添加過濾器,但沒有奏效:

Where-Object { -Not $_.IsDirectory -or [System.IO.Path]::GetExtension($_.Name) -like "tn*" -or [System.IO.Path]::GetExtension($_.Name) -eq "nc1"} | 

謝謝!

回答

1

你的代碼幾乎是正確的。只需要:

  • -and「非目錄」條件的擴展條件。或者使用兩個單獨的Where-Object子句,如下所示。
  • GetExtension結果包括點。
$latest = $directoryInfo.Files | 
    Where-Object { -Not $_.IsDirectory} | 
    Where-Object { 
     [System.IO.Path]::GetExtension($_.Name) -eq ".nc1" -or 
     [System.IO.Path]::GetExtension($_.Name) -like ".tn*" 
    } | 
    Group-Object { [System.IO.Path]::GetExtension($_.Name) } | 
    ForEach-Object { 
     $_.Group | Sort-Object LastWriteTime -Descending | Select -First 1 
    } 
+0

感謝您的幫助,馬丁 – Kyle

相關問題