PowerShell – Find Files Method

Advertisement

wonderboom
Joined:
Posts:
4
Location:
England

PowerShell – Find Files Method

Hi,
in the UI I can use the "Find Files" function to recursively search subdirectories on a remote site for a file type using this file mask.
/*/dir1/dir2/*.csv>=7D
which all sits under
/blah
And from the resultant search results I can transfer all the files it finds in a single process, I've searched the forums and Googled this and can't find any method of doing this in PowerShell, does anyone have any pointers? Is this even possible?

I only want the files it finds, not the directory structures.

Thanks.

Reply with quote

Advertisement

wonderboom
Joined:
Posts:
4
Location:
England

Tried the below PS

Hi,

Just to update on this I've tried the code below.
$RemotePath = "/blah/"
$TransferOptions = New-Object WinSCP.TransferOptions
$TransferOptions.FileMask = "*/dir1/dir2/*>=7D"
$Wildcard = "*.csv"
 
$FileInfos = 
    $Session.EnumerateRemoteFiles(
        $RemotePath, $Wildcard, [WinSCP.EnumerationOptions]::AllDirectories)
 
ForEach($File in $FileInfos){
    $FilePath = [WinSCP.RemotePath]::EscapeFileMask($File.FullName)
 
    $Session.GetFiles($FilePath, $LocalPath, $False, $TransferOptions) | Out-Null
}
But the GetFiles always returns False.

Any thoughts appreciated.

Reply with quote

martin
Site Admin
martin avatar
Joined:
Posts:
36,712
Location:
Prague, Czechia

Re: Tried the below PS

What do you mean by "GetFiles always returns False"? GetFiles returns an object. It cannot return False.

Please attach a full session log file showing the problem (using the latest version of WinSCP).

To generate the session log file, set Session.SessionLogPath. Submit the log with your post as an attachment. Note that passwords and passphrases not stored in the log. You may want to remove other data you consider sensitive though, such as host names, IP addresses, account names or file names (unless they are relevant to the problem). If you do not want to post the log publicly, you can mark the attachment as private.

Reply with quote

wonderboom
Joined:
Posts:
4
Location:
England

Find Files

Martin,

Thanks for taking the time to reply, I've fixed the issue, it appears I had missed a \ in the local path which was leading to the issue with retrieval.

The code I'd pasted previously works for my requirement however I do have one question, can I apply the 7D filter to this section.
$FileInfos = 
    $Session.EnumerateRemoteFiles(
        $RemotePath, $Wildcard, [WinSCP.EnumerationOptions]::AllDirectories)
instead of just the *.csv wildcard, can I also filter this on days so I can then just retrieve all the files in $FileInfos rather than applying the FileMask and having to run through all the files discovered in the EnumerateRemoteFiles section?
Cheers.

Reply with quote

martin
Site Admin
martin avatar
Joined:
Posts:
36,712
Location:
Prague, Czechia

Re: Find Files

No, as documented, the Session.EnumerateRemoteFiles supports only simple Windows wildcards.
https://winscp.net/eng/docs/library_session_enumerateremotefiles#mask

If your concern is performance, more efficient solution than calling Session.GetFiles with file mask is to query the RemoteFileInto.LastWriteTime.
https://winscp.net/eng/docs/library_remotefileinfo#lastwritetime
For an example, see:
Download files newer than X days from SFTP server with WinSCP, skipping folders that do not contain any matching files

Reply with quote

Advertisement

Advertisement

You can post new topics in this forum