如何在 Powershell 中以递归模式查找包含字符和扩展名的文件?

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

我试图找到所有包含 $var 并以“.log”结尾的文件,我需要它是递归的

我试过了

 $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
            }  

但是这个命令 return 每个单独的文件,无论 $var 和 Recurse 似乎都被禁用 :/

文件看起来像这样:foo_test.log

另一个文件:\user\foo_1.log

(PS : $var 我的意思是它取决于 $iniContent["Search"]["FileWith"] 值)

经过多次尝试,我这样做了并且成功了:

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
            }
        }  

如果有更好的方法,也欢迎post ;)

您可以通过此命令找到所有文件:

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

这是文件夹中 return 个名称包含 $var 并以 ".log"

结尾的所有文件