A powershell version of for /f (|findstr) for Unicode folders/files

Here is a small script I wrote that recursively scan a directory without some parent-subdirectories and extracts some attributes of the files within it.

@echo off
echo Path,Name,Extension,Size > filelist.txt
for /f "delims=" %%i in ('dir D:\שער /A:-d /s /b ^| findstr /l /i /v ^/c:"קקק" ^/c:"ttt"')
do echo %%~dpi,%%~ni,%%~xi,%%~zi >> filelist.txt

The problem is that findstr doesn't support Unicode chars (hebrew in this case, for /f does if you change the console font).

What is the PowerShell version of this script (assuming that PS loop does support unicode chars) ?

Thank you

2

2 Answers

Assuming that your findstr command is used to search the files content for the קקק text, here is the PowerShell code equivalent:

Set-Content -Path 'filelist.txt' -Value 'Path,Name,Extension,Size' -Encoding UTF8
foreach( $file in (Get-ChildItem -File -Path 'C:\Temp\שער' -Recurse) )
{ $nameCount = Get-Content -Path $file.FullName -Encoding UTF8 | Select-String -Pattern 'קקק' | Measure-Object | Select-Object -ExpandProperty Count if( $nameCount -gt 0 ) { $line = $file.DirectoryName + ',' + $file.BaseName + ',' + $file.Extension + ',' + $file.Length Add-Content -Path 'filelist.txt' -Value $line -Encoding UTF8 }
}

I had a similar problem with findstr, and solved it by using Select-String instead of findstr.

cat .\log*.txt | findstr -I Error was buggy, but cat .\log*.txt | Select-String -Pattern 'Error' was working fine.

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like