如何从FTP服务器上的通配符文件夹中自动下载特定文件类型 [英] How to automatically download a specific file type from within a wildcard folder on an FTP server

查看:74
本文介绍了如何从FTP服务器上的通配符文件夹中自动下载特定文件类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试建立一种自动将所有.mkv文件下载到不知道其全名的文件夹中的ftp服务器上的方法.我所知道的最多是The.Walking.Dead.*,其中star代表我不知道文件夹名称的内容.目前,我正在使用WinSCP,从这里获得的最接近的代码是

I'm trying to set up a way to automatically download all .mkv files on an ftp server within a folder of which I won't know the whole name. The most I will know is The.Walking.Dead.* Where star represents what I don't know of the folder name. Currently I'm using WinSCP and the closest code I've gotten from here is

@echo off

"C:\Program Files (x86)\WinSCP\WinSCP.com" ^
  /log="D:\Documents\WinSCP Log\WinSCP.log" /ini=nul ^
  /command ^
    "open "ftp://ivay.myseedbox.site/downloads/manual/" ^
    get "/The.Walking.Dead.*/*.mkv" "E:\Torrents\TV Shows\The Walking Dead\Season 8\" ^
    PAUSE
    exit

set WINSCP_RESULT=%ERRORLEVEL%
if %WINSCP_RESULT% equ 0 (
  echo Success
) else (
  echo Error
)

exit /b %WINSCP_RESULT%

但这返回错误

< 2017-11-06 12:47:02.172 Script: No file matching 'The.Walking.Dead.*' found.
. 2017-11-06 12:47:02.172 Listing file "E:\Torrents\TV".
. 2017-11-06 12:47:02.172 Retrieving file information...
> 2017-11-06 12:47:02.172 PWD
< 2017-11-06 12:47:02.308 257 "/"
> 2017-11-06 12:47:02.308 CWD /downloads/manual/E:\Torrents\TV
< 2017-11-06 12:47:02.433 550 Failed to change directory.
> 2017-11-06 12:47:02.433 TYPE I
< 2017-11-06 12:47:02.557 200 Switching to Binary mode.
> 2017-11-06 12:47:02.557 SIZE /downloads/manual/E:\Torrents\TV
< 2017-11-06 12:47:02.681 550 Could not get file size.
. 2017-11-06 12:47:02.681 Could not retrieve file information
< 2017-11-06 12:47:02.681 Script: Can't get attributes of file 'E:\Torrents\TV'.
< 2017-11-06 12:47:02.681 Could not retrieve file information

< 2017-11-06 12:47:02.681 Could not get file size.
. 2017-11-06 12:47:02.681 Script: Failed

所以我看到的是它正在尝试获取文件夹目录并将其用作文件路径,并且还忽略本地目录中的空间,并将远程目录和本地目录压缩在一起.因此,我们将不胜感激,因为我几乎不知道这里发生了什么.

So what I can see is it's trying to take my folder directory and use it as a file path and also ignoring the space in my local directory and squishing the remote and local directories together. So any help would be appreciated as I have almost no idea what's going on here.

推荐答案

选择要传输的文件时,文件掩码只能用于路径的最后一部分.

When selecting files to transfer, file mask can be used for the last component of the path only.

您可以做的一件事是:

get -filemask=*.mkv /The.Walking.Dead.* "E:\Torrents\TV Shows\The Walking Dead\Season 8\"

但这将重新创建匹配的文件夹( The.Walking.Dead.* )作为目标本地文件夹( Season 8 )的子文件夹.

But this will re-create the matching folder (The.Walking.Dead.*) as a subfolder of the target local folder (Season 8).

如果要直接将文件( *.mkv )下载到目标本地文件夹( Season 8 ),则可以使WinSCP重命名"源文件夹转到第8季:

If you want to download the files (*.mkv) directly to the target local folder (Season 8), you can make WinSCP "rename" the source folder to Season 8:

get -filemask=*.mkv /The.Walking.Dead.* "E:\Torrents\TV Shows\The Walking Dead\Season 8"

请注意目标路径中没有尾随反斜杠.它使WinSCP将匹配的源文件夹( The.Walking.Dead.* )下载到目标本地文件夹( The Walking Dead ,而不是 Season 8 >!),名称为 Season 8 .由于 Season 8 已经存在,它不会执行任何操作,并将直接继续下载包含的文件.

Note the absence of the trailing backslash in the target path. It makes WinSCP download the matching source folder (The.Walking.Dead.*) to the target local folder (The Walking Dead, not Season 8!) under a name Season 8. As Season 8 already exists, it won't do anything with it and will directly continue with downloading the contained files.

前面的内容适用于您的特定情况.在更复杂的情况下,您需要在下载之前找出文件夹的名称.虽然使用普通的批处理文件来实现它不是不可能的,但是这将非常复杂.

The previous works for your specific case. In more complex cases, you would need to find out the name of the folder before the download. While it is not impossible to implement this using a plain batch file, it would be very complicated.

在这种情况下,我建议使用 WinSCP .NET程序集使用PowerShell.

In such case, I would suggest using PowerShell with use of WinSCP .NET assembly.

有了它,脚本(例如 download.ps1 )将类似于:

With it, the script (e.g. download.ps1) would be like:

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

Write-Host "Connecting..."
$session = New-Object WinSCP.Session
$session.SessionLogPath = "session.log"
$session.Open($sessionOptions)

$remotePath = "/"
$localPath = "C:\local\path"
$pattern = "The.Walking.Dead.*"
$twdFolder =
    $session.ListDirectory($remotePath).Files |
    Where-Object { $_.IsDirectory -and ($_.Name -like $pattern) } |
    Select-Object -First 1

if ($twdFolder -eq $Null)
{
    Write-Host "No folder matching '$pattern' found."
}
else
{
    Write-Host "Found folder '$($twdFolder.Name)', downloading..."
    $sourcePath = [WinSCP.RemotePath]::Combine($remotePath, $twdFolder.Name)
    $sourcePath = [WinSCP.RemotePath]::Combine($sourcePath, "*")
    $destPath = Join-Path $localPath "*"
    $transferResult = $session.GetFiles($sourcePath, $destPath).Check()
}

Write-Host "Done"

提取WinSCP自动化程序包以及脚本和 查看全文

登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆