Search code examples
for-loopbatch-filecmdfindstrregedit

Excluding a character in a FOR loop


I would like to display the information regarding the ProfileImagePath value of one windows user who don't have the "_" character in his username :

@echo off
cls
for /f %%I in ('dir /a:d-h /b C:\Users\ ^| %SystemRoot%\System32\findstr.exe /b /l /v "_"') do (
        FOR /F "delims=" %%k IN ('reg query "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList"^|findstr.exe /R "S-1-5-21-[0-9]*-[0-9]*-[0-9]*-[0-9]*$" 2^>nul') do (
        reg query "%%k" /v "ProfileImagePath"|findstr /i /e /c:"%%~I"
        )   
)

But the findstr command also takes into account users with the "_" character, while in the first FOR command, I have excluded this character :

ProfileImagePath    REG_EXPAND_SZ    C:\Users\user1
ProfileImagePath    REG_EXPAND_SZ    C:\Users\_user1

How is it possible ? How to take this into account in the 2nd FOR command ?


Solution

  • findstr /i /e /c:"user1" searches for strings ending with user1. However _user1 also ends with user1 :(

    Changing it to findstr /i /e /c:"\%%~I" is one way to solve your problem :)