简体   繁体   English

仅使用 PowerShell 将新文件从 FTP 目录下载到本地文件夹

[英]Only download new files from FTP directory to local folder using PowerShell

I have a PowerShell script which downloads all files from an FTP directory to a local folder (Thanks to Martin Prikryl).我有一个 PowerShell 脚本,它将所有文件从 FTP 目录下载到本地文件夹(感谢 Martin Prikryl)。

The FTP directory is updated with multiple new files at various times throughout the day. FTP 目录在一天中的不同时间使用多个新文件进行更新。

I will be running my script every 30 minutes via Task Scheduler to download files from the FTP directory.我将每 30 分钟通过任务计划程序运行我的脚本以从 FTP 目录下载文件。

I would like the script to check and only download new files from the FTP directory to local folder.我希望脚本检查并仅将新文件从 FTP 目录下载到本地文件夹。

Note: The check must be done on the FTP directory as previously downloaded files may be removed from Local folder.注意:必须在 FTP 目录上进行检查,因为之前下载的文件可能会从本地文件夹中删除。

Please see below for my current script;请参阅下面的我当前的脚本;

#FTP Server Information - SET VARIABLES
$user = 'user' 
$pass = 'password'
$target = "C:\Users\Jaz\Desktop\FTPMultiDownload"
#SET FOLDER PATH
$folderPath = "ftp://ftp3.example.com/Jaz/Backup/"

#SET CREDENTIALS
$credentials = new-object System.Net.NetworkCredential($user, $pass)

function Get-FtpDir ($url,$credentials) {
    $request = [Net.WebRequest]::Create($url)
    $request.Method = [System.Net.WebRequestMethods+FTP]::ListDirectory
    if ($credentials) { $request.Credentials = $credentials }
    $response = $request.GetResponse()
    $reader = New-Object IO.StreamReader $response.GetResponseStream() 
    $reader.ReadToEnd()
    $reader.Close()
    $response.Close()
}

$Allfiles=Get-FTPDir -url $folderPath -credentials $credentials
$files = ($Allfiles -split "`n")

$files 

$webclient = New-Object System.Net.WebClient 
$webclient.Credentials = New-Object System.Net.NetworkCredential($user,$pass)
$counter = 0
foreach ($file in ($files | where {$_ -like "*.*"})){
    $source=$folderPath + $file
    $destination = Join-Path $target $file
    $webclient.DownloadFile($source, $destination)

    #PRINT FILE NAME AND COUNTER
    $counter++
    $counter
    $source
}

Thanks in advance for you help!在此先感谢您的帮助!

In your loop, check if the local file exists, before you actually download it:在您的循环中,在实际下载之前检查本地文件是否存在:

$localFilePath = $target + $file
if (-not (Test-Path $localFilePath))
{
    $webclient.DownloadFile($source, $localFilePath)
}    

As you actually do not keep the local files, you have to remember what files were already downloaded in some kind of a log:由于您实际上不保留本地文件,因此您必须记住在某种日志中已经下载了哪些文件:

# Load log
$logPath = "C:\log\path\log.txt"

if (Test-Path $logPath)
{
    $log = Get-Content $logPath
}
else
{
    $log = @()
}

# ... Then later in the loop:

foreach ($file in ($files | where {$_ -like "*.*"})){
    # Do not download if the file is already in the log.
    if ($log -contains $file) { continue }

    # If this is a new file, add it to a log and ...
    $log += $file

    # ... download it using your code
}

# Save the log    
Set-Content -Path $logPath -Value $log

Note that your code to split listing to lines is not very reliable.请注意,将列表拆分为行的代码不是很可靠。 For a better solution, see my answer to PowerShell FTP download files and subfolders .如需更好的解决方案,请参阅我对PowerShell FTP 下载文件和子文件夹的回答。


Related question:相关问题:
Download most recent file from FTP using PowerShell使用 PowerShell 从 FTP 下载最新文件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将文件从 C# 中的 FTP 服务器下载到修改日期大于指定的本地文件夹 - Download files from FTP server in C# to local folder with modification date greater than specified 从 FTP 服务器下载新的和修改过的文件 - Download new and modified files from an FTP server 在 PowerShell 中使用 WinSCP 仅下载新文件 - Download only new files with WinSCP in PowerShell 如何使用 PowerShell 的 FtpWebRequest 类从 FTP 服务器下载文件名中包含井号/哈希符号“#”的文件 - How to download files that contain a pound/hash sign '#' in the filename from an FTP server using PowerShell's FtpWebRequest class 如何从特定文件夹的ftp​​下载文件 - How to download files from ftp from particular folder 使用 WinSCP .NET 程序集从 FTP 服务器下载除特定文件夹之外的所有文件 - Download all files except a specific folder from FTP server using WinSCP .NET assembly 从FTP下载文件(如果给定的本地文件列表中不存在) - Download file from FTP if it does not exist in a given list of local files 仅当文件存在时,才在 PowerShell 中使用 WinSCP 从 SFTP 服务器从阵列下载文件 - Download files from an array from SFTP server using WinSCP in PowerShell only when the files exist 从FTP下载文件和文件夹 - Download files and folders from FTP 使用 PowerShell 从 SFTP 服务器下载文件 - Download files from SFTP server using PowerShell
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM