从 FTP 服务器下载名称包含特定字符串的文件

Download files whose name contains a specific string from an FTP server(从 FTP 服务器下载名称包含特定字符串的文件)
本文介绍了从 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: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 服务器下载名称包含特定字符串的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

Custom Error Queue Name when using EasyNetQ for RabbitMQ?(使用 EasyNetQ for RabbitMQ 时自定义错误队列名称?)
Rabbitmq Ack or Nack, leaving messages on the queue(Rabbitmq Ack 或 Nack,将消息留在队列中)
Wait for a single RabbitMQ message with a timeout(等待一条带有超时的 RabbitMQ 消息)
Setup RabbitMQ consumer in ASP.NET Core application(在 ASP.NET Core 应用程序中设置 RabbitMQ 消费者)
How do I set a number of retry attempts in RabbitMQ?(如何在 RabbitMQ 中设置重试次数?)
WebClient set headers(WebClient 设置标头)