2017-08-30 23 views
0

我僅限於PuTTY和WinSCP。使用WinSCP從SFTP服務器下載比X天更新的文件,跳過不包含任何匹配文件的文件夾

我想下載包含日誌文件的日誌目錄。例如,我想抓取6天或更新的所有log_fileslog_dir2log_dir3包括符合條件的文件夾,而log_dir1及其文件沒有。

DIR/log_dir1/log_files % older than 6 days 
DIR/log_dir2/log_files % meets criteria 
DIR/log_dir3/log_files % meets criteria 

我的問題是,而log_dir1log_files不被下載,語法我目前使用的下載文件夾log_dir1。通常情況下,這並不是什麼大問題,但我們正在談論數百個log_dir文件夾(全部爲空,因爲文件大於6天)。由於我無法控制的原因,我無法將這些舊的日誌目錄與其日誌文件進行移動或歸檔。

我的問題很簡單,我該如何更改語法以忽略超過6天的文件夾以及文件。

get -filemask="*>6D" /DIR/* C:\temp 

我已經試過的參數幾個不同的組合,我看了一下目錄口罩和麪具的路徑支持頁面。我不能讓他們中的任何人工作(版本問題?)。任何人都可以解釋他們的語法比help page更好。我將在明天使用當前版本的WinSCP進行更新。

+0

在WinSCP賦予文件屏蔽時間限制,不能用於目錄 - 你究竟想要什麼?你真的想根據他們的時間戳選擇文件夾嗎?這可靠嗎?或者你想跳過不包含任何新文件的文件夾? –

+0

嗯。至少我想跳過不包含比6天更新的文件的文件夾。上面的語法給我正確的文件,但也是所有的文件夾(也是空的)。 – James

+0

是的。我提出了你的答案,但由於新的帳戶,它不反映馬丁。謝謝! – James

回答

0

Time constraint in WinSCP file mask不能用於目錄。


但是你可以在PowerShell script with a use of WinSCP .NET assembly輕鬆地實現這個自定義邏輯:

# Load WinSCP .NET assembly 
Add-Type -Path "WinSCPnet.dll" 

# Set up session options 
$sessionOptions = New-Object WinSCP.SessionOptions -Property @{ 
    Protocol = [WinSCP.Protocol]::Sftp 
    HostName = "example.com" 
    UserName = "username" 
    Password = "password" 
    SshHostKeyFingerprint = "..." 
} 

$remotePath = "/remote/path" 
$localPath = "C:\local\path" 
$limit = (Get-Date).AddDays(-6) 

$session = New-Object WinSCP.Session 

# Connect 
$session.Open($sessionOptions) 

# Enumerate files to download 
$fileInfos = 
    $session.EnumerateRemoteFiles(
     $remotePath, $Null, [WinSCP.EnumerationOptions]::AllDirectories) | 
    Where-Object { $_.LastWriteTime -gt $limit } 

foreach ($fileInfo in $fileInfos) 
{ 
    $localFilePath = 
     [WinSCP.RemotePath]::TranslateRemotePathToLocal(
      $fileInfo.FullName, $remotePath, $localPath) 

    # If the corresponding local folder does not exist yet, create it 
    $localFileDir = Split-Path -Parent $localFilePath 
    if (!(Test-Path -Path $localFileDir)) 
    { 
     Write-Host "Creating local directory $localFileDir..." 
     New-Item $localFileDir -ItemType directory | Out-Null 
    } 

    Write-Host "Downloading file $($fileInfo.FullName)..." 

    # Download file 
    $sourcePath = [WinSCP.RemotePath]::EscapeFileMask($fileInfo.FullName) 
    $transferResult = $session.GetFiles($sourcePath, $localFilePath) 

    # Did the download succeeded? 
    if (!$transferResult.IsSuccess) 
    { 
     # Print error (but continue with other files) 
     Write-Host ("Error downloading file ${remoteFilePath}: " + 
      $transferResult.Failures[0].Message) 
    } 
} 

$session.Dispose() 

Write-Host "Done." 

運行腳本(download.ps1),如:

powershell.exe -ExecutionPolicy Unrestricted -File download.ps1 
相關問題