Home > Back-end >  Searching specific character in file in powershell not working
Searching specific character in file in powershell not working

Time:03-23

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.

CodePudding user response:

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