Post a reply

Options
Add an Attachment

If you do not want to add an Attachment to your Post, please leave the Fields blank.

(maximum 10 MB; please compress large files; only common media, archive, text and programming file formats are allowed)

Options

Topic review

martin

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

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

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
}