【问题标题】:Count number of uploaded file using PowerShell and WinSCP .NET assembly使用 PowerShell 和 WinSCP .NET 程序集计算上传文件的数量
【发布时间】:2021-02-11 18:29:51
【问题描述】:

我有将文件从我的计算机上传到 FTP 的脚本。但我想计算我的脚本上传了多少文件。我正在使用 WinSCP 的脚本。

Add-Type -Path "WinSCPnet.dll"

$session = New-Object WinSCP.Session

try
{
    # Connect
    $session.DebugLogPath = "C:\forKeppLog\transfer_data_script.log"
    $session.Open($sessionOptions)
    
    #Message
    Write-Host -ForegroundColor Cyan "`r`nUploading files to $servername..."
    Write-Host -ForegroundColor White "Do not close this window!"
    $path = "/MyuploadfilesTOFtp/"


    # Transfer files
    $session.PutFiles("C:\Myfileslocation\*.*", $path+"/*").Check()
}
finally
{
    #Inform user
    Write-Host -ForegroundColor Cyan "Files have been uploaded to $servername."

    #Dispose of session
    $session.Dispose()
}

我可以在我的日志文件中看到上传了哪些文件,但使用起来并不友好。我想为上传的计数文件数获取一些变量。

我想做这样的事情

Write-Host -ForegroundColor Cyan "Files have been uploaded to $servername. Uploaded file's number is $numberofiles."

【问题讨论】:

    标签: .net powershell ftp winscp winscp-net


    【解决方案1】:

    Session.PutFiles 返回TransferOperationResult。它具有 Transfers 属性和转移集合。

    $results = $session.PutFiles("C:\Myfileslocation\*.*", $path+"/*")
    $results.Check() # Throws if any transfer failed
    
    $count = $results.Transfers.Count
    

    【讨论】:

      猜你喜欢
      • 2020-12-26
      • 1970-01-01
      • 2021-12-29
      • 2017-03-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-25
      • 2014-07-12
      相关资源
      最近更新 更多