Unreliable results using quotes with variables in windows batch for loop - windows

I'm running a for loop to go through a list of services to stop, but I'm having trouble with the quotes, and rather unusual results as well.
for /f "tokens=* delims= " %%a in ('wmic service where ^(displayname like "%%tsm%%"^) get name ^| findstr "TSM"') do echo "%%a"
My results are with this:
"Service Name
"Service Name
If I switch to:
for /f "tokens=* delims= " %%a in ('wmic service where ^(displayname like "%%tsm%%"^) get name ^| findstr "TSM"') do echo ""%%a""
The results are:
""Service Name
""Service Name
And, if I switch to:
for /f "tokens=* delims= " %%a in ('wmic service where ^(displayname like "%%tsm%%"^) get name ^| findstr "TSM"') do echo ^"%%a^"
The results:
"Service Name
"Service Name
Obviously something is happening that I don't understand, because I simply cannot get it to encapsulate my results in quotes so I can run commands such as net stop or sc delete.

try this:
for /f "tokens=* delims= " %%a in ('wmic service where ^(displayname like "%%tsm%%"^) get name ^| findstr "TSM"') do (
for /f "delims=" %%i in ("%%a") do echo "%%i"
)
this is due to the "end of line" sign of wmic (CRLFCR).

Related

store part of outcome command to variable

Hello dear people and others,
Today i wanted to create a simple script, thought it would be easy to store the outcome to var of the following command:
wmic bios get serialnumber | findstr /N /V SerialNumber
Outcome:
2:H3GK4S1
3:
The problem is when i try to get the serial with wmic, it returns the string as expected but also an empty string/line. When i try to store the serial to a variable it stores it and then directly overwrites it with the empty string. This is the function i nearly got working now:
FOR /F "tokens=*" %g IN ('Wmic Bios Get SerialNumber ^| FINDSTR /N /V SerialNumber') DO (SET serial=%g & ECHO %g)
And this gives the following output:
FOR /F "tokens=*" %g IN ('Wmic Bios Get SerialNumber ^| FINDSTR /N /V SerialNumber') DO (SET serial=%g & ECHO %g)
2:H3GK4S1
3:
As can be seen above, the loop overwrites the serial var, if someone can help me towards the right directon to get this working, would be mad.
At the Command Prompt:
For /F "Tokens=1* Delims==" %g In ('WMIC BIOS Get SerialNumber /Value') Do #For /F "Tokens=*" %i In ("%h") Do #Set "serial=%i" & Echo %i
Or in a batch file:
#For /F "Tokens=1* Delims==" %%g In ('WMIC BIOS Get SerialNumber /Value'
) Do #For /F "Tokens=*" %%i In ("%%h") Do #Set "serial=%%i" & Echo %%i
#Pause
EditIf you're happy to use a labelled section in your batch file:
#Echo Off
Set "serial="
For /F "Skip=1 Delims=" %%A In ('WMIC BIOS Get SerialNumber') Do If Not Defined serial Call :Sub %%A
Set serial 2>Nul
Pause
GoTo :EOF
:Sub
Set "serial=%*"
GoTo :EOF
Try like this:
FOR /F "tokens=*" %g IN ('Wmic Bios Get SerialNumber /format:value') DO for /f "tokens=* delims=" %# in ("%g") do set "serial=%#"
echo %serial%
Mind that's a command that should be executed in the command prompt directly.For a batch file you'll need to double the % in the for loop tokens.
In a batch file, you can also use a goto to end the loop after the first iteration :
#echo off
for /f "tokens=2 delims=:" %%a in ('wmic bios get serialnumber ^| findstr /N /V SerialNumber') do (
set "$var=%%a"
goto:next
)
exit/b
:next
echo Result=^> [%$var: =%]

Collect each service's display name and status

I want to get list of services with their display name and their status.
This is what I have tried:
for /f "tokens=2" %s in ('SC query state^= all ^| find "DISPLAY_NAME"') do #(for /f "tokens=4" %t in ('SC query %s ^| find "STATE"') do #echo %s is %t)
But this returns only limited services such as disk, etc.
This is a perfect task for the built-in WMI command line executable, WMIC.exe.
From the cmd.exe prompt:
For /F "Skip=1 Delims=" %A In ('"WMIC Service Get DisplayName, Name, State"') Do #For /F "Delims=" %B In ("%A") Do #Echo(%B
From a batch file:
#For /F "Skip=1 Delims=" %%A In ('"WMIC Service Get DisplayName, Name, State"'
) Do #For /F "Delims=" %%B In ("%%A") Do #Echo(%%B
#Pause
Try getting help from powershell directly in your batch file, like this for example (save as .bat and run it). In the example i type it to the screen and give you ALL services, RUNNING ONLY, and STOPPED ONLY but you could do basically what you want with the content of that txt file (search it with a loop, save a part to a variable, etc.).
#echo off&cls
pushd %~dp0
echo.
echo List ALL services
pause
PowerShell -NoProfile -ExecutionPolicy Bypass -Command "powershell Get-Service | Out-File services.txt"
type services.txt
echo.
echo List services that are RUNNING only
pause
find "Running" services.txt
echo.
echo List services that are STOPPED only
pause
find "Stopped" services.txt
pause
:cleanup
del /f services.txt

Windows Batch file split string %%i was unexpected at this time

I'm trying to set few variables from systeminfo command in Windows. I read all kinds of similar threads and tried various things, but always keep getting the same error:
%%G was unexpected at this time.
or
%%i was unexpected at this time.
I've been trying to get this done all day long. Can someone point me in the right direction please? This is what I have tried so far:
for /f "tokens=:" %%i in ('systeminfo ^| grep "OS Name"') do set OSname=%%i
for /f "usebackq delims=: tokens=2" %%i in ('systeminfo ^| grep "OS Name"') do set OSname=%%i
FOR /F "usebackq delims= tokens=2" %%i IN ('systeminfo ^| grep "OS Name"') DO set vers=%%i
for /f "tokens=* delims= " %%a in ('systeminfo ^|grep "OS Name"') do (set OS_Name=%%a)
systeminfo | find "OS Name" > osname.txt
for /f "usebackq delims=: tokens=2" %%i in (osname.txt) do set osname=%%i
FOR /F %%G IN (FINDSTR /L /C:"OS Name" systeminfo.txt) DO ECHO %%G
UPDATE: I got it to work like this:
FOR /F "usebackq delims=: tokens=2" %i IN (`FINDSTR /L /C:"OS Name" systeminfo.txt`) DO set osname=%i
Any way to remove the leading space in front of the string? Thanks.
This is all you need to get the OS name.
for /f "tokens=2* delims=: " %%i in ('systeminfo ^| find "OS Name"') do set OSname=%%j

How do i get a specific line from an output using a batch file?

I have the code below that returns the hostname and IP address of a given server. How can i only have the hostname as the output?
FOR /F %%i in (servers.txt) do FOR /F "skip=3 delims=: tokens=2 usebackq" %%j in (`nslookup %%i`) do #echo %%j >> Devices_With_IP.txt
Assuming your machine uses English:
FOR /F %%i in (servers.txt) do FOR /F "delims=: tokens=2" %%j in (
'nslookup %%i ^| find "Name:"'
) do #echo %%j >> Devices_With_IP.txt
Try using the findstr command on the output of your nslookup to get only the line containing "Name".
FOR /F %%i in (servers.txt) do FOR /F "tokens=2 usebackq delims=: " %%j in (`nslookup %%i ^| findstr Name`) do #echo %%j >> Devices_With_IP.txt
Note that I also rearranged the /F conditions in the second loop in order to include space as a deliminator, this removes the leading spaces before the output.
Using the find command instead of findstr -
FOR /F %%i in (servers.txt) do FOR /F "tokens=2 usebackq delims=: " %%j in (`nslookup %%i ^| find "Name"`) do #echo %%j >> Devices_With_IP.txt
Just realized that using find instead of findstr makes this (almost) exactly the same as dbenham's answer.

How can I remove empty lines from wmic output?

This is my string to obtain the position of TeamViewer (any version) service executable:
for /f "skip=1 delims=" %A in ('wmic path win32_service where "name like 'TeamViewer%'" get pathname') do set POSITION=%A
The problem is caused by wmic because it includes an empty line at the end of result (on Windows 7 command) and this is the output:
C:\Users\giovanni>for /f "skip=1 delims=" %A in ('wmic path win32_service where "name like 'TeamViewer%
'" get pathname') do set POSITION=%A
:\Users\giovanni>set POSITION="C:\Program Files\TeamViewer\Version8\TeamViewer_Service.exe"
:\Users\giovanni>set POSITION=
C:\Users\giovanni>echo %position%
ECHO enabled.
How I can get only the second line of the output with the correct position of the executable? (or skip the latest line, of course).
Thanks all in advance and have a nice day.
Giovanni.
This is checktv.bat:
for /f "skip=1 delims=" %%A in ('wmic path win32_service where "name like 'TeamViewer%'" get pathname ^| findstr /r /v "^$"') do set POSITION=%%A
echo %POSITION%
Like this:
for /f "skip=1 delims=" %A in (
'wmic path win32_service where "name like 'TeamViewer%'" get pathname ^| findstr /r /v "^$"'
) do set POSITION=%A
The findstr /r /v "^$" removes empty lines from the output.
wmic blah /value | find "=" >> wherever
output will be
field=value
no extra lines
tokenize from there, delim =

Resources