Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
793 views
in Technique[技术] by (71.8m points)

.net - Download files whose name contains a specific string from an FTP server

I am not experienced with PowerShell and I need help to download several images with similar names from an FTP server. I found a lot in this forum, and I only managed to download one picture. For this I had to enter the name of the file.

I would like to select a date and then download all the pictures with this date and save them in a local folder. I would also like to save the names of the downloaded images in a .txt file

So how can I download the pictures based on the date?

The string 20201009 is the date and the numbers after it are sequential.

I hope you understand what I mean, because it is my first time that I write something in the forum

FTP-Server

$UserName = "abc"
$Password = "abc"

$RemoteFileName =  "DatenTestKam3_schlecht_20201009_085248_00848.jpg"
$LocalFilePath = "C:UsersDesktopPowerShell$RemoteFileName"

$ServerName = "10.196.195.167/22_test"

$webclient = New-Object System.Net.WebClient

$webclient.Credentials = New-Object System.Net.NetworkCredential($UserName, $Password)

$uri = New-Object System.Uri(“ftp://$ServerName/$RemoteFileName”)

$webclient.DownloadFile($uri, $LocalFilePath)
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If I understand your question correctly, you want to download all files whose name contains "20201009" string, right?

$url = "ftp://ftp.example.com/remote/path/"
$credentials = New-Object System.Net.NetworkCredential("username", "password")
$request = [System.Net.WebRequest]::Create($url)
$request.Credentials = $credentials
$request.Method = [System.Net.WebRequestMethods+Ftp]::ListDirectory
$response = $request.GetResponse()
$reader = New-Object IO.StreamReader $response.GetResponseStream() 
$listing = $reader.ReadToEnd()
$reader.Close()
$response.Close()

$localpath = "C:UsersDesktopPowerShell"
$date = "20201009"
$files =
    ($listing -split "`r`n") |
    Where-Object {$_ -like "*$date*"}

$webclient = New-Object System.Net.WebClient 
$webclient.Credentials = $credentials

foreach ($file in $files)
{
    $localfilepath = (Join-Path $localPath $file)
    Write-Host ($file + " => " + $localfilepath)

    $webclient.DownloadFile(($url + $file), $localfilepath)
}

It's even easier, if you use WinSCP .NET assembly:

Add-Type -Path "WinSCPnet.dll"

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

$session = New-Object WinSCP.Session

$session.Open($sessionOptions)

$localpath = "C:UsersDesktopPowerShell"
$date = "20201009"
$session.GetFilesToDirectory("/remote/path", $localpath, "*$date*").Check()

$session.Dispose()

(I'm the author of WinSCP)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...