3

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

Roey Peretz
  • 183
  • 2
  • 8
  • 1
    Just a nitpick: from the title it looks like UTF is about the *conetnts* of the files, since UTF is a file encoding/format. IMHO it would be better to say "Unicode paths" instead of "UTF folders/files". – Nathan.Eilisha Shiraini Jul 05 '17 at 07:16
  • There are no "UTF" chars. UTF-8/16/32 are encoding schemes for Unicode and Windows uses UTF-16. [`findstr` doesn't support Unicode](https://superuser.com/q/306619/241386) [but `find` does](https://blogs.msdn.microsoft.com/oldnewthing/20121128-00/?p=5963/) – phuclv Jul 05 '17 at 07:35

2 Answers2

0

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
    }
}
Chris
  • 1,395
  • 13
  • 28
0

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.

Pascal V.
  • 1
  • 1