Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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
   }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading