从WinSCP输出中隐藏服务器名称等信息

时间:2018-03-13 15:13:01

标签: vba ftp winscp

我想在我的应用程序中使用WinSCP命令行。我想内联所有命令,而不是脚本文件。

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

我需要显示命令窗口才能看到下载文件的进度。

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)

1 个答案:

答案 0 :(得分:1)

您想要的是可行的,但它涉及许多高级代码,用于重定向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]::CombinePaths($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]::CombinePaths($remotePath, $latest.Name)); " & _
    "  $session.GetFiles($sourcePath, $localPath + '\*').Check(); " & _
    "  echo ' Done'; " & _
    "} catch { Write-Host $_.Exception.Message; }")

enter image description here