Excluding a character in a FOR loop - 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 ?

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 :)

Related

How to find registry folder by match in it`s value

I am writing a batch file to search the registry.
I need to find the folder inside HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Installer\Products which ProductName key equals to "MyProduct".
I need to find this folder and delete it.
This folder I want to delete will look like this: HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Installer\Products\1A0614C849C672CF0A680DCFA3921735
In this example, change MyProduct to your actual ProductName string leaving the closing doublequote untouched, on line 4:
#Echo Off
SetLocal EnableExtensions
Set "App=MyProduct"
Set "Key=HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Installer\Products"
For /F "Delims=" %%G In ('^""%SystemRoot%\System32\reg.exe" Query "%Key%" /S /F
"%App%" /D /E 2^>NUL ^| "%SystemRoot%\System32\findstr.exe" /I /R /X
"%Key:\=\\%\\[^^^\\]*"^"'
) Do Echo="%SystemRoot%\System32\reg.exe" Delete "%%G" /F ^>NUL
Pause
The above will only print the deletion line that you intend to run. Once you are satisfied it is correct, to actually delete it, change the code to the following, (remembering to change your ProductName string again). Please note, that as you're deleting a key from HKEY_LOCAL_MACHINE, you will most likely need to run this script elevated, or as a user having sufficient permissions for doing so.:
#Echo Off
SetLocal EnableExtensions
Set "App=MyProduct"
Set "Key=HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Installer\Products"
For /F "Delims=" %%G In ('^""%SystemRoot%\System32\reg.exe" Query "%Key%" /S /F
"%App%" /D /E 2^>NUL ^| "%SystemRoot%\System32\findstr.exe" /I /R /X
"%Key:\=\\%\\[^^^\\]*"^"'
) Do "%SystemRoot%\System32\reg.exe" Delete "%%G" /F >NUL

Issues when getting a list of user profiles using WMIC

I was using the following batch command to retrieve all local user profiles (including domain users too) :
for /f "delims=" %%I in ('dir /a:d-h /b "%SystemDrive%\Users\*" 2^>nul ^| %SystemRoot%\System32\findstr.exe /i /l /x /v /g:"%bin%\exclude_users.txt"') do (
The problem is that this command has its limits: it doesn't really check if the users in question do actually have an account.
The user Compo provided me a methodology for retrieving the profile names, using WMIC.
So I ended up writing the following command:
#For /F "tokens=* skip=1" %%I In ('%__AppDir__%wbem\WMIC.exe UserAccount Get Name ^|%__AppDir__%findstr.exe /i /l /x /v /g:"%bin%\exclude_users.txt"') do (
The problem is: it ignores my exclusion file (which contains one user per line) and it ends up a profile without any name.
Any idea How I can solve these issues ?
#echo off
setlocal
set "bin=%~dp0"
for /f "tokens=* skip=1" %%I in ('
%__AppDir__%wbem\WMIC.exe UserAccount where Disabled^="FALSE" get Name ^|
%__AppDir__%WindowsPowerShell\v1.0\powershell -noprofile -command "$input.trim()" ^|
%__AppDir__%findstr.exe /i /l /x /v /g:"%bin%\exclude_users.txt"
') do echo "%%~I"
The wmic output is piped to powershell to be trimmed and then piped to findstr.
The wmic command will exclude disabled accounts by use of the where clause.
Change the setting of bin as needed.
If you want a solution which still uses WMIC, and your exclusion list, then the following should do as you require.
#For /F Tokens^=4Delims^=^" %%G In ('%__AppDir__%wbem\WMIC.exe UserAccount Where "LocalAccount='TRUE'" Assoc:List /ResultRole:Name 2^>NUL')Do #Echo %%G|%__AppDir__%findstr.exe /VXLIG:"%~dp0exclude_users.txt"
You can split that over multiple lines for easier reading too:
#For /F Tokens^=4Delims^=^" %%G In ('%__AppDir__%wbem\WMIC.exe UserAccount^
Where "LocalAccount='TRUE'" Assoc:List /ResultRole:Name 2^>NUL'
)Do #Echo %%G|%__AppDir__%findstr.exe /VXLIG:"%~dp0exclude_users.txt"

How to set FULL computer name to a variable in windows batch file

When right clicking >Computer > Properties, my full computer name is 50 characters.
If I set Cname=%COMPUTERNAME% and echo %Cname%, the FULL name is truncated from 50 characters down to just the Host name of 14 chars
In a batch file, how is the FULL computer name extracted?
This example is based purely on the vbscript you stated was retuning the string you require:
#Echo Off
Set "Pre=HKLM\SYSTEM\CurrentControlSet"
For /F "Delims==" %%A In ('Set _ 2^>Nul') Do Set "%%A="
For /F "Tokens=2*" %%A In ('
Reg Query "%Pre%\Control\Computername\ActiveComputerName" /V "ComputerName" 2^>Nul
')Do Set "_ActiveComputerName=%%B"
For /F "Tokens=2*" %%A In ('
Reg Query "%Pre%\Control\Computername\ComputerName" /V "ComputerName" 2^>Nul
')Do Set "_ComputerName=%%B"
For /F "Tokens=2*" %%A In ('
Reg Query "%Pre%\Services\TCPIP\Parameters" /V "HostName" 2^>Nul
')Do Set "_HostName=%%B"
Set _
Simply choose the example which outputs the string you were requiring for your purposes.
Either:
#Echo Off
Set "Pre=HKLM\SYSTEM\CurrentControlSet"
For /F "Tokens=2*" %%A In ('
Reg Query "%Pre%\Control\Computername\ActiveComputerName" /V "ComputerName" 2^>Nul
')Do Set "ActiveComputerName=%%B"
Set ActiveComputerName
Pause
Else:
#Echo Off
Set "Pre=HKLM\SYSTEM\CurrentControlSet"
For /F "Tokens=2*" %%A In ('
Reg Query "%Pre%\Control\Computername\ComputerName" /V "ComputerName" 2^>Nul
')Do Set "Computer-Name=%%B"
Set Computer-Name
Pause
Or:
#Echo Off
Set "Pre=HKLM\SYSTEM\CurrentControlSet"
For /F "Tokens=2*" %%A In ('
Reg Query "%Pre%\Services\TCPIP\Parameters" /V "HostName" 2^>Nul
')Do Set "Host-Name=%%B"
Set Host-Name
Pause
For the reference for Computer Name and Full computer name
https://superuser.com/questions/640046/what-is-the-difference-between-computer-name-and-full-computer-name
Now coming to your question, You can get your computer name with following ways:
%computername%
net config workstation | findstr /C:"Full Computer name"
wmic computersystem get name
Reference :
https://www.windows-commandline.com/find-computer-name-from-command-line/
There is no limitation of DOS console, I have tested as below:

How can I find last row that contains string in txt with cmd command

I want to find two different strings in .txt file. I need two scripts, first script to find last row that contains these strings together and second script to find last row that contains these strings seperately. I tried to write somethings but I go off the project.
This what i have tried so far as code :
#echo off
for /f %%i in ('find /v /c "" ^< deneme.txt') do set /a lines=%%i
echo %lines%
set /a startLine=%lines% - 1
more /e +%startLine% deneme.txt > temp.txt
find "ali" temp.txt|find "veli"
del temp.txt
Thanks for help.
#echo off
set "string1=ali"
set "string2=veli"
set "file=deneme.txt"
for /f "delims=" %%a in ('findstr /i "\<%string1%\>" %file% ^|findstr /i /v "\<%string2%\>" ') do set "out1=%%a"
for /f "delims=" %%a in ('findstr /i "\<%string2%\>" %file% ^|findstr /i /v "\<%string1%\>" ') do set "out2=%%a"
for /f "delims=" %%a in ('findstr /i "\<%string1%\>" %file% ^|findstr /i "\<%string2%\>" ') do set "out3=%%a"
echo last line with %string1%: "%out1%"
echo last line with %string2%: "%out2%"
echo last line with both: "%out3%"
for explanations, see for /? and findstr /?

using piped commands with for /f on windows (with reg query)

I am trying to query the install location of a program in the registry. All I'm interested in is the location output.
This question has a partial solution, but it doesn't quite help.
On Windows 7, the reg command outputs a stupid registry key header along with the value, as shown below:
reg query "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\NSIS Unicode" /v InstallLocation
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\NSIS Unicode
InstallLocation REG_EXPAND_SZ C:\Program Files\NSIS
First, is there a way to turn off the header and simplify the output?
At the command prompt, I can change the above to
reg query "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\NSIS Unicode" /v InstallLocation | findstr InstallLocation
so that it returns me just the second line.
Now, if I am to use a FOR /F to parse this and get only the directory value, the FOR command fails saying | was unexpected at this time.
Here's my batch file:
#for /f "tokens=2* delims= " %%k in ('reg query "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\NSIS Unicode" /v InstallLocation | findstr InstallLocation') do #echo %%k
So where am I going wrong?
You must escape the | character using a caret (^).
#echo off
setlocal
set KEY=HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\NSIS Unicode
set V=InstallLocation
for /f "tokens=2* delims= " %%k in ('reg query "%KEY%" /v %V% ^| findstr "%V%"') do echo %%k
this would return REG_SZ on my machine.
The pipe char is special and has to be escaped with ^.
#for /f "tokens=2* delims= " %%k in ('reg query "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\NSIS Unicode" /v InstallLocation ^| findstr InstallLocation') do #echo %%k

Resources