Get files starting with a specific name

Advertisement

jonathanf
Guest

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?

Reply with quote

Advertisement

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

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.

Reply with quote

jonathanf
Joined:
Posts:
2
Location:
North Carolina

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.

Reply with quote

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

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

Reply with quote

Advertisement

You can post new topics in this forum