Search code examples
powershellpowershell-4.0

Searching specific character in file in powershell not working


I have list of files in my folder. I need to only extract the files which has ~ sign. So, I tried:

'dore_20220319121423~'.contains('~')

Output is coming as 'True'.

But when I scanned whole folder and tried to find such files then the filter is not working.All the files inside the folder are coming in output, since I needed only the files having ~ sign.

cls
$folerPath = 'Z:\Splite\RMQ\2022-03-20'
$files = Get-ChildItem $folerPath
foreach ($f in $files){
$outfile = $f.Name.Contains('~')
if ($outfile){
echo $files.Name
}
}

I am seeing all the files in output, even though I applied filter.


Solution

  • You are not checking the ~ character but are only passing it to $outfile

    Try doing:

    foreach ($f in $files){
       if ($f.Name.Contains('~')){
           echo $f.Name
       }
    }