2017-11-06 23 views
1

잘 모를 폴더 내의 FTP 서버에서 모든 .mkv 파일을 자동으로 다운로드하는 방법을 설정하려고합니다. 전체 이름. 내가 알게 될 가장 큰 것은 The.Walking.Dead입니다. * 여기서 별은 내가 폴더 이름을 모르는 것을 나타냅니다. 현재 내 폴더를 취하려고 그래서 내가 무엇을 볼 수있다 나는 WinSCP에를 사용하고 있는데 여기에서 왔 가장 가까운 코드는FTP 서버의 와일드 카드 폴더에서 특정 파일 형식을 자동으로 다운로드하는 방법

@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 

의 오류가 반환 디렉토리에 저장하고 파일 경로로 사용하고 내 로컬 디렉토리의 공백을 무시하고 원격 및 로컬 디렉토리를 함께 스 큐잉합니다. 그래서 나는 여기에서 무슨 일이 일어나는 지 거의 알지 못하기 때문에 어떤 도움을 주셔서 감사하겠습니다.

+0

와일드 카드를 폴더 이름으로 사용할 수 있다고 생각하지 않습니다. 파일 이름에 와일드 카드를 사용할 수 있지만'mget' 명령을 사용해야합니다. – Squashman

+0

실제로 모든 ftp 명령을 스크립트 파일에 넣어야합니다. 로그 파일에서 모든 종류의 오류가 발생했음을 알 수 있어야합니다. 배치 파일 코드를 ftp 스크립트 코드와 함께 사용할 수 없습니다. – Squashman

답변

1

전송할 파일을 선택할 때 file mask은 경로의 마지막 구성 요소로만 사용할 수 있습니다.

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

하지만이 대상 로컬 폴더 (Season 8)의 하위 폴더와 일치하는 폴더 (The.Walking.Dead.*)를 다시 만들 것입니다 : 당신이 할 수있는


한 가지입니다.


직접 대상 로컬 폴더 (Season 8), 당신은 WinSCP에 만들 수있는 소스 폴더 Season 8에 "이름 바꾸기"로 파일 (*.mkv)을 다운로드하려면 다음

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

참고가없는 경우 대상 경로에 후행 백 슬래시가 표시됩니다. WinSCP는 일치하는 소스 폴더 (The.Walking.Dead.*)를 Season 8이라는 이름으로 대상 로컬 폴더 (The Walking Dead, 아니 Season 8!)에 다운로드합니다. Season 8이 이미 존재하기 때문에 아무 것도하지 않으며 포함 된 파일을 계속 다운로드합니다.


이전 사례는 특정 사례에 해당합니다. 보다 복잡한 경우에는 다운로드하기 전에 폴더 이름을 찾아야합니다. 일반 배치 파일을 사용하여 구현하는 것은 불가능하지 않지만 매우 복잡합니다.

그런 경우에는 WinSCP .NET assembly을 사용하여 PowerShell을 사용하는 것이 좋습니다. 나는 완전히 내가 여기에 게시 결국 잊어

powershell.exe -ExecutionPolicy Unrestricted -File download.ps1 
0

: 같은

# 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]::CombinePaths($remotePath, $twdFolder.Name) 
    $sourcePath = [WinSCP.RemotePath]::CombinePaths($sourcePath, "*") 
    $destPath = Join-Path $localPath "*" 
    $transferResult = $session.GetFiles($sourcePath, $destPath).Check() 
} 

Write-Host "Done" 

스크립트와 함께 Extract WinSCP automation packagerun the script : 그와

, 스크립트 (예 : download.ps1)은 같은 것 어떻게하는지 직접 알아 냈어. 아무에게도 회신하지 않으니 유감입니다. 당신이 저를 도우려고했던 시간에 정말 감사합니다. 가장 깨끗한 방법이 아닐지 모르지만 그것은 저를 위해 일합니다. 다운로드 진행률 표시 줄을 추가하고 싶습니다.하지만 정말 복잡 할 것 같습니다.이것은 내가 결국 사용하는 것입니다 :

param (
    $localPath = "E:\Torrents\TV Shows\The Walking Dead\Season 8\", 
    $remotePath = "/downloads/manual/The.Walking.Dead.*", 
    $fileName = "*" 
) 

    # Deletes new episode folder just in case one exists 
    Remove-Item "E:\Torrents\TV Shows\The Walking Dead\Season 8\New Episode" -recurse -EA SilentlyContinue 

    #Tells the user that the files are being downloaded 
    cls 
    "Downloading The Walking Dead. This can take a while." 
    "Please do not close this window." 

try 
{ 
# Load WinSCP .NET assembly 
Add-Type -Path "C:\Program Files (x86)\WinSCP\WinSCPnet.dll" 

# Set up session options 
$sessionOptions = New-Object WinSCP.SessionOptions -Property @{ 
    Protocol = [WinSCP.Protocol]::Ftp 
    HostName = "removed" 
    PortNumber = 21 
    UserName = "removed" 
    Password = "removed" 
} 

$session = New-Object WinSCP.Session 

try 
{ 
    # Connect 
    $session.Open($sessionOptions) 

    # Download the file and throw on any error and deletes the files from the server 
    $session.GetFiles(
     ($remotePath + $fileName), 
     ($localPath + $fileName)) 
    $session.RemoveFiles("/downloads/manual/The.Walking.Dead.*") 
} 

finally 
{ 
    $session.Dispose() 
} 

    # Renames the downloaded folder 
    Rename-Item "E:\Torrents\TV Shows\The Walking Dead\Season 8\The.Walking.Dead.*" "New Episode" -EA SilentlyContinue 

    # Deletes unnecesary files 
    Remove-Item "E:\Torrents\TV Shows\The Walking Dead\Season 8\New Episode\*" -exclude *.mkv -EA SilentlyContinue 

    # Moves video file to proper directory 
    Move-Item "E:\Torrents\TV Shows\The Walking Dead\Season 8\New Episode\*.mkv" "E:\Torrents\TV Shows\The Walking Dead\Season 8" -EA SilentlyContinue 

    # Deletes new episode folder to clean things up 
    Remove-Item "E:\Torrents\TV Shows\The Walking Dead\Season 8\New Episode" -recurse -EA SilentlyContinue 

    exit 0 
} 
catch [Exception] 
{ 
    Write-Host "Error: $($_.Exception.Message)" 
    exit 1 
}