【发布时间】:2017-02-21 19:22:19
【问题描述】:
与我合作的供应商将 zip 文件上传到 FTP。我需要下载那里上传的任何内容并根据需要进行处理。
使用 Powershell,如何从 FTP 文件夹下载*.*?
# Config
$Username = "user"
$Password = "password"
$LocalFile = "C:\tools\file.zip"
$RemoteFile = "ftp://myftpserver:22/Folder1/Folder/file.csv"
# Create a FTPWebRequest
$FTPRequest = [System.Net.FtpWebRequest]::Create($RemoteFile)
$FTPRequest.Credentials = New-Object System.Net.NetworkCredential($Username,$Password)
$FTPRequest.Method = [System.Net.WebRequestMethods+Ftp]::DownloadFile
$FTPRequest.UseBinary = $true
$FTPRequest.KeepAlive = $false
$ftpRequest.EnableSsl = $true
# Send the ftp request
$FTPResponse = $FTPRequest.GetResponse()
# Get a download stream from the server response
$ResponseStream = $FTPResponse.GetResponseStream()
# Create the target file on the local system and the download buffer
$LocalFileFile = New-Object IO.FileStream ($LocalFile,[IO.FileMode]::Create)
[byte[]]$ReadBuffer = New-Object byte[] 1024
# Loop through the download
do {
$ReadLength = $ResponseStream.Read($ReadBuffer,0,1024)
$LocalFileFile.Write($ReadBuffer,0,$ReadLength)
}
while ($ReadLength -ne 0)
有什么方法可以使 $RemoteFile 类似于ftp://myftpserver:22/Folder1/Folder/*.zip 或ftp://myftpserver:22/Folder1/Folder/*.*
如果有相同的帖子,我深表歉意。我看到了一些类似的,但不够接近来回答这个问题。
【问题讨论】:
标签: powershell ftp