How to upload files to FTP by latest date?

Advertisement

guard100
Guest

How to upload files to FTP by latest date?

I try to create upload multiple files which create which apply on same date but it only uploaded on latest files instead of all files created on same date.



try
{
# Load WinSCP .NET assembly
Add-Type -Path "WinSCPnet.dll"
Move-Item -Path \\unc\d\xxx\xxx\*.csv -Destination \\unc\d\xxx\xxx\uploaded -Force
# Setup session options
$sessionOptions = New-Object WinSCP.SessionOptions -Property @{
Protocol = [WinSCP.Protocol]::ftp
HostName = "xxxxx"
UserName = "xxxxx"
Password = "xxxxx"
}
$session = New-Object WinSCP.Session
try
{
# Connect
$session.Open($sessionOptions)
$localPath = "\\unc\d\xxx\xxx\uploaded"
$remotePath = "/Inbound"

# Select the most recent file.
# The !$_.PsIsContainer test excludes subdirectories.
# With PowerShell 3.0, you can replace this with -File switch of Get-ChildItem.
$latest =
Get-ChildItem -Path $localPath |
Where-Object {!$_.PsIsContainer} |
Sort-Object LastWriteTime -Descending |
Select-Object -First 1
# Any file at all?
if ($latest -eq $Null)
{
Write-Host "No file found"
}
# Upload the selected file
$sourcePath = Join-Path $localPath $latest.Name
$session.PutFiles(
[WinSCP.RemotePath]::EscapeFileMask($sourcePath),
[WinSCP.RemotePath]::Combine($remotePath, "*")).Check()
}
finally
{
# Disconnect, clean up
$session.Dispose()
}
}
catch
{
Write-Host "Error: $($_.Exception.Message)"
exit 0
}

Reply with quote

Advertisement

martin
Site Admin
martin avatar
Joined:
Posts:
32,052
Location:
Prague, Czechia

Re: How to upload files to FTP by latest date?

What some date?
Do you want to use PowerShell? Or simple WinSCP scripting?

Reply with quote

Advertisement

You can post new topics in this forum