从 WinSCP 输出中隐藏一些信息,例如服务器名称

Hide some information like server name from WinSCP output

我想在我的应用程序中使用 WinSCP 命令行。我想内联所有命令并且没有脚本文件。

是否可以隐藏服务器名称? 我不想让用户知道服务器名称。

我需要命令 window 可见才能看到下载文件的进度。

Call Shell("c:\program files (x86)\winscp\winscp.com /ini=nul /command ""open ftp://user:password@servername/ "" ""get -latest /public_ftp/incoming/* c:\local\"" ""exit""", vbNormalFocus)

你想要的是可行的,但它涉及很多高级代码,用于重定向 winscp.com 的输出,并过滤掉你不想显示的信息。


使用 WinSCP .NET assembly from PowerShell 更简单。这使您可以完全控制输出。

只需执行如下操作:

PowerShell -ExecutionPolicy bypass -Command "$sessionUrl = 'ftp://user:password@servername/' ; $remotePath = '/public_ftp/incoming' ; $localPath = 'c:\local' ; try { Add-Type -Path 'C:\Program Files (x86)\WinSCP\WinSCPnet.dll' ; $sessionOptions = New-Object WinSCP.SessionOptions ; $sessionOptions.ParseUrl($sessionUrl) ; echo 'Opening connection'; $session = New-Object WinSCP.Session ; $session.add_FileTransferProgress( { Write-Host -NoNewline ([char]13 + '{0} ({1:P0})' -f $_.FileName, $_.FileProgress) } ); $session.Open($sessionOptions) ; echo 'Finding latest file'; $directoryInfo = $session.ListDirectory($remotePath) ; $latest = $directoryInfo.Files | Where-Object { -Not $_.IsDirectory } | Sort-Object LastWriteTime -Descending | Select-Object -First 1 ; if ($latest -eq $Null) { Write-Host 'No file found' ; exit; }; echo 'Downloading file'; $session.GetFiles([WinSCP.RemotePath]::EscapeFileMask([WinSCP.RemotePath]::Combine($remotePath, $latest.Name)), $localPath + '\*').Check(); echo ' Done'; } catch { Write-Host $_.Exception.Message; }"

PowerShell代码基本上等同于WinSCP article on Downloading the most recent file.

它只是合并成一个命令,所以你可以从 VBA Shell 函数中执行它(在正确地加倍双引号之后)。

Call Shell ( _
  "PowerShell -ExecutionPolicy bypass -Command """ & _
    "$sessionUrl = 'ftp://user:password@servername/';" & _
    "$remotePath = '/public_ftp/incoming';" & _
    "$localPath = 'c:\local';" & _
    "try {" & _
    "  Add-Type -Path 'C:\Program Files (x86)\WinSCP\WinSCPnet.dll'; " & _
    "  $sessionOptions = New-Object WinSCP.SessionOptions; " & _
    "  $sessionOptions.ParseUrl($sessionUrl); " & _
    "  echo 'Opening connection'; " & _
    "  $session = New-Object WinSCP.Session; " & _
    "  $session.add_FileTransferProgress( { Write-Host -NoNewline ([char]13 + '{0} ({1:P0})' -f $_.FileName, $_.FileProgress) } ); " & _
    "  $session.Open($sessionOptions); " & _
    "  echo 'Finding latest file'; " & _
    "  $directoryInfo = $session.ListDirectory($remotePath); " & _
    "  $latest = $directoryInfo.Files | Where-Object { -Not $_.IsDirectory } | " & _
    "    Sort-Object LastWriteTime -Descending | Select-Object -First 1; " & _
    "  if ($latest -eq $Null) { Write-Host 'No file found' ; exit; }; " & _
    "  echo 'Downloading file'; " & _
    "  $sourcePath = [WinSCP.RemotePath]::EscapeFileMask([WinSCP.RemotePath]::Combine($remotePath, $latest.Name)); " & _
    "  $session.GetFiles($sourcePath, $localPath + '\*').Check(); " & _
    "  echo ' Done'; " & _
    "} catch { Write-Host $_.Exception.Message; }")