Download the latest file from FTP using PowerShell

I am working on a PowerShell script that pulls files from an FTP site. Files are uploaded to the FTP site every hour, so I need to upload the latest. The code I downloaded now downloads all files from today, not just one file. How can I upload only the most recent file?

Here is the code I'm currently using

$ftpPath = 'ftp://***.***.*.*' $ftpUser = '******' $ftpPass = '******' $localPath = 'C:\Temp' $Date = get-date -Format "ddMMyyyy" $Files = 'File1', 'File2' function Get-FtpDir ($url, $credentials) { $request = [Net.FtpWebRequest]::Create($url) if ($credentials) { $request.Credentials = $credentials } $request.Method = [System.Net.WebRequestMethods+FTP]::ListDirectory (New-Object IO.StreamReader $request.GetResponse().GetResponseStream()) -split "`r`n" } $webclient = New-Object System.Net.WebClient $webclient.Credentials = New-Object System.Net.NetworkCredential($ftpUser,$ftpPass) $webclient.BaseAddress = $ftpPath Foreach ( $item in $Files ) { Get-FTPDir $ftpPath $webclient.Credentials | ? { $_ -Like $item+$Date+'*' } | % { $webClient.DownloadFile($_, (Join-Path $localPath $_)) } } 
+5
source share
1 answer

This is not easy with FtpWebRequest . For your task you need to know the timestamps of the file.

Unfortunately, there is no reliable and efficient way to get timestamps using the functions offered by the FtpWebRequest /.NET framework / PowerShell, because they do not support the FTP MLSD . The MLSD team provides a list of remote directories in a standard machine-readable format. Command and format are standardized by RFC 3659 .

Alternatives you can use that are supported by the .NET platform:

  • ListDirectoryDetails method (FTP LIST command) to get information about all the files in the directory, and then you are dealing with a specific FTP server data format (the * nix format, similar to the ls * nix ls , is the most common drawback is that the format can change over time, because the format "May 8 17:48" is used for older files, and the format "October 18, 2009" is used for older files).
  • GetDateTimestamp method (FTP MDTM ) for individually obtaining timestamps for each file. The advantage is that the response is standardized by RFC 3659 to YYYYMMDDHHMMSS[.sss] . The downside is that you need to send a separate request for each file, which can be quite inefficient.

Some links:


Alternatively, use a third-party FTP library that supports the MLSD command and / or supports listing listing format analysis.

For example, WinSCP.NET assembly supports both.

Code example:

 # Load WinSCP .NET assembly Add-Type -Path "WinSCPnet.dll" # Setup session options $sessionOptions = New-Object WinSCP.SessionOptions -Property @{ Protocol = [WinSCP.Protocol]::Ftp HostName = "example.com" UserName = "user" Password = "mypassword" } $session = New-Object WinSCP.Session # Connect $session.Open($sessionOptions) # Get list of files in the directory $directoryInfo = $session.ListDirectory($remotePath) # Select the most recent file $latest = $directoryInfo.Files | Where-Object { -Not $_.IsDirectory } | Sort-Object LastWriteTime -Descending | Select-Object -First 1 # Any file at all? if ($latest -eq $Null) { Write-Host "No file found" exit 1 } # Download the selected file $sourcePath = [WinSCP.RemotePath]::EscapeFileMask($remotePath + $latest.Name) $session.GetFiles($sourcePath, $localPath).Check() 

For complete code, see Download the latest file (PowerShell) .

(I am the author of WinSCP)

+8
source

Source: https://habr.com/ru/post/1260457/


All Articles