Listing files matching wildcard

Advertisement

Guest

Listing files matching wildcard

# Get list of matching files in the directory
$files =
$session.EnumerateRemoteFiles(
$remotePath, ($wildcard = Get-Content "test.txt"), [WinSCP.EnumerationOptions]::None)

# Any file matched?
if ($files.Count -gt 0)
{
foreach ($fileInfo in $files)
{
Write-Host $($fileInfo.Name) with size $($fileInfo.Length)

}
}
else
{
Write-Host "No files matching $wildcard found"
}


Is the bolded section not a valid option? I have 5 different lists that i need to us to download files from an FTP server. i have an option that is doing what i need but i'm not able to log the file it downloads like i can with this code i copied from your site.

When using my list like this it reads the two lines i have in the test file but says acts like there are no matches. if i remove the second line in the file it finds the matches of the single line item.

Reply with quote

Advertisement

martin
Site Admin
martin avatar
Joined:
Posts:
29,791
Location:
Prague, Czechia

Re: Listing files matching wildcard

The mask parameter of Session.EnumerateRemoteFiles does not support multiple masks.

If you need multiple masks, do not use the mask parameter.
Instead, let Session.EnumerateRemoteFiles return all files and filter them programmatically, as you need.

Reply with quote

trae*
Joined:
Posts:
4
Location:
United States

Re: Listing files matching wildcard

martin wrote:

The mask parameter of Session.EnumerateRemoteFiles does not support multiple masks.

If you need multiple masks, do not use the mask parameter.
Instead, let Session.EnumerateRemoteFiles return all files and filter them programmatically, as you need.


Could you give me an example of that ?

Would it be removing the $wildcard and then using a foreach with a where over and over for each file i need?

might help to add a little more detail on what the end result is.
i have a list of files that i need to download. filex*.dar ,fileb*.dar,filec*.dar etc and i would like each filename that is downloaded recorded. i have been able to get all the files downloaded but cant seem to do it to where it lists the actual filename being downloaded. only the wildcard name.

Reply with quote

trae*
Joined:
Posts:
4
Location:
United States

wanted to add a little more detail to see if you can help me out .

so this is the code i am using that downloads the files to where i need them to go but wont list the individual file name downloaded only the wildcard. what can i do to change that?


try
{


$remotePath3 = "C:\folder\"
$lines2 = Get-Content "list.txt"

ForEach ($line in $lines2)
{
Write-Host "Downloading $line to C:\folder\ ..."
$session.GetFiles($line, $remotePath3).Check()
$dt = Get-Date
Add-Content $log "$dt : Downloading $($line) to C:\folder.."
}


i am using this over and over for each list that i need to sort through. sorry if none of this makes sense i can try to clarify more if needed.

Reply with quote

martin
Site Admin
martin avatar
Joined:
Posts:
29,791
Location:
Prague, Czechia

Re: Listing files matching wildcard

trae* wrote:

i have a list of files that i need to download. filex*.dar ,fileb*.dar,filec*.dar etc and i would like each filename that is downloaded recorded. i have been able to get all the files downloaded but cant seem to do it to where it lists the actual filename being downloaded. only the wildcard name.
Then it does not look like you need EnumerateRemoteFiles at all.
Use GetFiles, which supports multiple masks via TransferOptions.FileMask.
https://winscp.net/eng/docs/library_transferoptions#filemask

To log the downloaded files, iterate TransferOperationResult.Transfers.
See https://winscp.net/eng/docs/library_session_getfiles

If you want to log the files, as they are downloaded already, handle Session.FileTransferred event:
https://winscp.net/eng/docs/library_session_filetransferred

Reply with quote

Advertisement

trae*
Joined:
Posts:
4
Location:
United States

Re: Listing files matching wildcard

martin wrote:

trae* wrote:

i have a list of files that i need to download. filex*.dar ,fileb*.dar,filec*.dar etc and i would like each filename that is downloaded recorded. i have been able to get all the files downloaded but cant seem to do it to where it lists the actual filename being downloaded. only the wildcard name.
Then it does not look like you need EnumerateRemoteFiles at all.
Use GetFiles, which supports multiple masks via TransferOptions.FileMask.
https://winscp.net/eng/docs/library_transferoptions#filemask

To log the downloaded files, iterate TransferOperationResult.Transfers.
See https://winscp.net/eng/docs/library_session_getfiles

If you want to log the files, as they are downloaded already, handle Session.FileTransferred event:
https://winscp.net/eng/docs/library_session_filetransferred


thanks i will see if i can get it to work with those .

Reply with quote

Advertisement

You can post new topics in this forum