从FTP服务器下载名称包含特定字符串的文件 [英] Download files whose name contains a specific string from an FTP server

查看:75
本文介绍了从FTP服务器下载名称包含特定字符串的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我没有使用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:\Users\Desktop\PowerShell\$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:\Users\Desktop\PowerShell"
$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:\Users\Desktop\PowerShell"
$date = "20201009"
$session.GetFilesToDirectory("/remote/path", $localpath, "*$date*").Check()

$session.Dispose()

(我是WinSCP的作者)

这篇关于从FTP服务器下载名称包含特定字符串的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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