Post a reply

Before posting, please read how to report bug or request support effectively.

Bug reports without an attached log file are usually useless.

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

jonathanf

Well, it's only going to be one file, so that's a non-issue. I do want it renamed when downloaded anyway.

Will try again and post log.
martin

You have to enable the logging by setting Session.SessionLogPath.

Though one obvious issue is that you download all files to a single file download.xls, so all downloaded files overwrite each other.

To download the files using their own name, use:
$transferResult = $session.GetFiles("/Reports/Report_rptTotalMilesAndRanks_201609*.xls", "c:\users\jonathanf\Desktop\", $False, $transferOptions)

See https://winscp.net/eng/docs/library_session_getfiles#localpath
jonathanf

try

{
    # Load WinSCP .NET assembly
    Add-Type -Path "C:\Program Files (x86)\WinSCP\WinSCPnet.dll"
 
    # Setup session options
    $sessionOptions = New-Object WinSCP.SessionOptions -Property @{
        Protocol = [WinSCP.Protocol]::Sftp
        HostName = <redacted>
        UserName = <redacted>
        Password = <redacted>
        SshHostKeyFingerprint = <redacted>
    }
 
    $session = New-Object WinSCP.Session
 
    try
    {
        # Connect
        $session.Open($sessionOptions)
 
        # Download files
        $transferOptions = New-Object WinSCP.TransferOptions
        $transferOptions.TransferMode = [WinSCP.TransferMode]::Binary
 
        $transferResult = $session.GetFiles("/Reports/Report_rptTotalMilesAndRanks_201609*.xls", "c:\users\jonathanf\Desktop\download.xls", $False, $transferOptions)
 
        # Throw on any error
        $transferResult.Check()
 
        # Print results
        foreach ($transfer in $transferResult.Transfers)
        {
            Write-Host ("Download of {0} succeeded" -f $transfer.FileName)
        }
    }
    finally
    {
        # Disconnect, clean up
        $session.Dispose()
    }
 
    exit 0
}
catch [Exception]
{
    Write-Host ("Error: {0}" -f $_.Exception.Message)
    exit 1
}


No logs are generated here.

It should be noted here that when I remove the wildcard and specify an actual filename, the script runs perfectly.
martin

Re: Get files starting with a specific name

Your syntax is correct: Report_rptTotalMilesAndRanks_201610*Final.csv

So I do not understand, what is your question about.

If you have problem, show us your code and a session log file.
jonathanf

Get files starting with a specific name

I've searched all over and can find pieces of what I need, but not the whole thing.

I'm using PowerShell to script checking whether a file exists and then download it. Every month, two new files come in that I need to pull. They begin with the same phrase but end based on the month of the year and then have a timestamp before the file extension.

So right now, I have the following file on the server:
Report_rptTotalMilesAndRanks_20160916111841_Final.csv

That one has been processed. I need to get the new file when it comes online, which will have the following portion of the filename stable:
Report_rptTotalMilesAndRanks_201610*Final.csv
Where the * is in the filename would be the date, year, and time the file was generated.

How on earth do I tell WinSCP to look for a file beginning with "Report_rptTotalMilesAndRanks_201610" and ending in "Final.csv" with a wildcard in between?