PierreDuv
PierreDuv

Reputation: 124

How to find a file which contains characters and extension in recurse mode in Powershell?

I try to find all the file which contains $var and end with ".log" and I need it to be Recursive

I tried this

 $logDirectoryPath = $Directory + "\*"       
Get-ChildItem -Path $logDirectoryPath -Include *$iniContent["Search"]["FileWith"]*, *.log -Recurse -File -Name | ForEach-Object {
                $lbox_files.Items.Add($_)  # we add file to a list
            }  

But this command return every single files regardless of $var and Recurse seems disable :/

the file look like this : foo_test.log

another file : \user\foo_1.log

(PS : by $var I mean it depends of $iniContent["Search"]["FileWith"] value)

Upvotes: 1

Views: 223

Answers (2)

Vad
Vad

Reputation: 743

You can find all files by this comand:

Get-ChildItem -Path "c:\" -Recurse -File -Filter "*$var*.log"

It's return all files in folder that contain in name $var and ends with ".log"

Upvotes: 2

PierreDuv
PierreDuv

Reputation: 124

After many tries, I did this and it works:

Get-ChildItem -Path $Directory  -Filter *.log -Recurse -File -Name | ForEach-Object {
            $fileWith = $iniContent["Search"]["FileWith"]
            if ($_ -like '*{0}*' -f $fileWith) {
                $lbox_files.Items.Add($_)  # we add the file name to a list
            }
        }  

If there is a better way to do it, feel free to post it too ;)

Upvotes: 0

Related Questions