Need to download ALL files from the current date date

Advertisement

lvance18
Joined:
Posts:
2
Location:
Tampa

Need to download ALL files from the current date date

I am having a problem downloading ALL files in a directory with the current date. I have the script where it will only download the latest file. I need it to download ALL files from the current date. There is 199 small files uploaded daily.

Here is my script:
param (
    $localPath = "C:\HPHC TEST\*",
    $remotePath = "/users/"
)
 
try
{
    # Load WinSCP .NET assembly
    Add-Type -Path "WinSCPnet.dll"
 
    # Setup session options
    $sessionOptions = New-Object WinSCP.SessionOptions -Property @{
        Protocol = [WinSCP.Protocol]::Sftp
        HostName = "test.com"
        UserName = "test"
        Password = "testing"
        SshHostKeyFingerprint = "ssh-rsa 1024 test"
    }
    $session = New-Object WinSCP.Session
 
    try
    {
        # 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
        $session.GetFiles(
            [WinSCP.RemotePath]::EscapeFileMask($latest.FullName), $localPath).Check()
    }
    finally
    {
        # Disconnect, clean up
        $session.Dispose()
    }
 
    exit 0
}
catch
{
    Write-Host "Error: $($_.Exception.Message)"
    exit 1
}
I also have this one which I tried to modify for use with multiple files.
# Load WinSCP .NET assembly
Add-Type -Path "WinSCPnet.dll"
 
# Set up session options
$sessionOptions = New-Object WinSCP.SessionOptions -Property @{
    Protocol = [WinSCP.Protocol]::Sftp
    HostName = "test.com"
    UserName = "test"
    Password = "testing"
    SshHostKeyFingerprint = "ssh-rsa 1024 test"
}
 
$sessionOptions.AddRawSettings("SendBuf", "0")
$sessionOptions.AddRawSettings("SshSimple", "0")
 
$session = New-Object WinSCP.Session
 
try
{
    # Connect
    $session.Open($sessionOptions)
 
    # Transfer files
    $session.GetFiles("/users/%TIMESTAMP#ddmmyyyy%.txt", "C:\HPHC TEST\*").Check()
}
finally
{
    $session.Dispose()
}

Reply with quote

Advertisement

martin
Site Admin
martin avatar
Joined:
Posts:
41,518
Location:
Prague, Czechia

Re: Need to Download ALL files from the current date date

Assuming you are referring to file timestamp (what your first code uses), not to some timestamp in a filename (what your second code uses), then this should do:
$transferOptions = New-Object WinSCP.TransferOptions
$transferOptions.FileMask = "*>=1D"
$session.GetFiles(
    "/users/*.txt", "C:\HPHC TEST\*", $False, $transferOptions).Check()
If you do not need PowerShell, you can do this even in a plain scripting:
get -filemask=*>=1D /users/*.txt "C:\HPHC TEST\*"
See https://winscp.net/eng/docs/script_download_most_recent_file#alternatives

Reply with quote

Advertisement

You can post new topics in this forum