Topic "Get files starting with a specific name"

Author Message
jonathanf

Guest


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?
Advertisements
martin
[View user's profile]
Site Admin
Joined: 2002-12-10
Posts: 24991
Location: Prague, Czechia
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
[View user's profile]

Joined: 2016-10-05
Posts: 2
Location: North Carolina
Code:
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
[View user's profile]
Site Admin
Joined: 2002-12-10
Posts: 24991
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
jonathanf
[View user's profile]

Joined: 2016-10-05
Posts: 2
Location: North Carolina
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.
Advertisements

You can post new topics in this forum






Search Site

What is WinSCP?

It is award-winning SFTP client, SCP client, FTPS client and FTP client integrated into one software program for file transfer to FTP server or secure SFTP server. [More]

And it's free!

Donate

About donations

$9   $19   $49   $99

About donations

Recommend

WinSCP Privacy Policy

WinSCP License