問題描述
我沒有使用 PowerShell 的經驗,我需要幫助才能從 FTP 服務器下載多個名稱相似的圖像.我在這個論壇找到了很多,我只設法下載了一張圖片.為此,我必須輸入文件名.
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.
我想選擇一個日期,然后下載該日期的所有圖片并將它們保存在本地文件夾中.我還想將下載圖像的名稱保存在 .txt 文件中
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?
字符串20201009
是日期,后面的數字是連續的.
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
$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)
推薦答案
如果我理解你的問題,你想下載所有名稱包含20201009"的文件.字符串,對吧?
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)
}
如果您使用 WinSCP .NET 程序集,那就更簡單了:
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()
(我是 WinSCP 的作者)
這篇關于從 FTP 服務器下載名稱包含特定字符串的文件的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!