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 =
Related
I'm getting the list of Windows users and their local path, through wmic (thanks Compo).
I would like some user names to be excluded from the output in this wmic command :
#For /F "Skip=1Tokens=1,2" %%G In ('%__AppDir__%wbem\WMIC.exe UserAccount Where^
"LocalAccount='True' And Not Name Like '[_]%%'" Get Name^,SID 2^>Nul'
)Do #For /F %%I In ("%%H")Do #For /F "Tokens=2Delims==" %%J In ('
%__AppDir__%wbem\WMIC.exe Path Win32_UserProfile Where^
"SID='%%I' And Special!='True'" Get LocalPath /Value 2^>Nul'
)Do #For /F "Tokens=*" %%K In ("%%J")Do #Echo User name:"%%G",Profile path:"%%K"
I'm not sure how I can add this exclusion file :
%__AppDir__%findstr.exe /V /X / L/ /I G:"usernames.txt"
Can you help me please ?
Thank you
If your intention is to exclude names from the output, the general rule for efficiency, is to filter your commands as soon as possible in your code.
For this reason, the most efficient method would be to make the individual exclusions within the Where clause. I provided an example of how to do that in my comment, e.g. change the current exclusion, of names beginning with an underscore, (And Not Name Like '[_]%%'), to And Name!='Dimitri' And Name!='Kalinda' And Name!='Peter'.
If you have a list of exclusions one per line in a file, and there are too many to transfer into the Where clause, then you should perform that filtering in the Do portion of that initial For loop. You could at that point use findstr.exe with the options you chose, (just fixed).
As the code you chose from my original answer was not the robust one, which caters for user names with spaces/problematic characters, I'd suggest you change to that too.
For that reason, this would be my suggested answer, (excluding names within usernames.txt using findstr.exe):
#For /F Tokens^=4Delims^=^" %%G In ('%SystemRoot%\System32\wbem\WMIC.exe
UserAccount Where "LocalAccount='TRUE'" Assoc:List /ResultRole:SID 2^>NUL'
)Do #Set /P "=%%G"<NUL|%SystemRoot%\System32\findstr.exe /XVLIG:"usernames.txt"^
>NUL&&(For /F "Tokens=1*Delims==" %%H In ('%SystemRoot%\System32\wbem\WMIC.exe
UserAccount Where "Name='%%G'" Get SID /Value 2^>NUL
^|%SystemRoot%\System32\find.exe "="')Do #For %%J In (%%I
)Do #For /F "Tokens=1*Delims==" %%K In ('%SystemRoot%\System32\wbem\WMIC.exe
Path Win32_UserProfile Where (SID^="%%J" And Special!^="TRUE" And LocalPath
Is Not Null^) Get LocalPath /Value 2^>NUL^|%SystemRoot%\System32\find.exe
"="')Do #For /F Tokens^=* %%M In ("%%L"
)Do #Echo UserName:"%%G", UserProfile:"%%M")
I am currently using this quick batch file to get info that I need from a variety of computers. My problem is that it creates the CSV file, but it is all one column. I am planning to use python to manipulate the data, but it is difficult to do so if it is all one column.
#echo off
(systeminfo | findstr /c:"Host Name"
systeminfo | findstr /c:"OS Name"
systeminfo | findstr /c:"System Model"
systeminfo | findstr /c:"System type"
systeminfo | findstr /c:"Total Physical Memory"
ipconfig | findstr IPv4
wmic diskdrive get size
wmic bios get serialnumber
wmic cpu get name)>>in.csv
All output, single line, comma delimited, and sub-delimits on both IP address and disk drive, since there are multiple of those. Sub-delimit is a semi-colon.
Output:
MyBox,36527083,Packard Bell,3-2,Microsoft Windows 10,x64-based PC,17087881216,192.168.121.1;192.168.3.1;10.11.18.112,Intel(R) Core(TM) i7-4710HQ CPU # 2.50GHz,2000396321280;63992678400;4000784417280;512105932800;1500299297280
Cmd:
#echo off
setlocal EnableDelayedExpansion
set "zManufacturer="
set "zHostName="
set "zOsName="
set "zSystemModel="
set "zSystemType="
set "zTotalPhysicalMemory="
set "zIpAddresses="
set "zHddSizes="
set "zCpuName="
set "zBiosSerial="
for /f "skip=1 tokens=1-6 delims=," %%A in ('wmic computersystem get Manufacturer^,Model^,TotalPhysicalMemory^,SystemType^,Workgroup /format:csv ^| findstr ","') do (
set "zHostName=%%A"
set "zManufacturer=%%B"
set "zSystemModel=%%C"
set "zSystemType=%%D"
set "zTotalPhysicalMemory=%%E"
)
for /f "tokens=2 delims=:" %%A in ('ipconfig ^| findstr /i ipv4') do set "zIpAddresses=!zIpAddresses!%%A"
set "zIpAddresses=!zIpAddresses:~1!"
set "zIpAddresses=!zIpAddresses: =;!"
for /f "tokens=1 delims=|" %%A in ('wmic os get name ^|findstr "|"') do set "zOsName=%%A"
for /f "tokens=2 delims=," %%A in ('wmic bios get serialnumber^,version /format:csv') do set "zBiosSerial=%%A"
for /f "tokens=2 delims=," %%A in ('wmic cpu get name^,version /format:csv') do set "zCpuName=%%A"
for /f "skip=1 tokens=2 delims=," %%A in ('wmic diskdrive get size^,TracksPerCylinder /format:csv ^|findstr /v /r "^$"') do set "zHddSizes=!zHddSizes!;%%A"
set "zHddSizes=!zHddSizes:~1!"
echo !zHostName!,!zBiosSerial!,!zManufacturer!,!zSystemModel!,!zOsName!,!zSystemType!,!zTotalPhysicalMemory!,!zIpAddresses!,!zCpuName!,!zHddSizes!
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: =%]
Hello having my first go with a BATCH script, I'm getting the size of the HDD as follow:
wmic diskdrive get size
Which works fine but I'd like to store this value into a variable for later use, such as using ECHO to display the value.
I'm not sure how I set the output of the above command to a variable. I went with:
SET hddbytes=wmic diskdrive get size
But this just sets the variable to the above text string and not the output.
For usage in batch file. From command line, replace %% with %
for /f "tokens=*" %%f in ('wmic diskdrive get size /value ^| find "="') do set "%%f"
echo %size%
Or, if you want to use you prefered variable
for /f "tokens=2 delims==" %%f in ('wmic diskdrive get size /value ^| find "="') do set "myVar=%%f"
echo %myVar%
You want:
for /f %%a in ('wmic diskdrive get size^|findstr [0-9]') do echo %%a
Updated:
WMIC's output could get a trailing Carriage Return in some environment:
Size <CR>
<CR><LF>
256052966400<CR>
<CR><LF>
500105249280<CR>
<CR><LF>
15496427520 <CR>
<CR><LF>
use csv format, and use FOR loop to truncate the wanted value from the wmic output:
For /F "tokens=2,3 delims=," %%a in ('"wmic diskdrive get size,Status
/format:csv |findstr "[0-9]" "') do (
echo "%%a"
)
Another Batch Script Example:
setlocal ENABLEDELAYEDEXPANSION
:: You can change active code page as well::
#chcp 936 >NUL
:: [example] Remove all network printers:
for /F "tokens=2,3 delims=," %%a in ('"wmic printer where 'local=FALSE' get Name,PrinterStatus /format:csv |findstr "." "') do (
echo "%%a"
rundll32 printui.dll,PrintUIEntry /n "%%a" /dn /q
)
for /f "delims=" %%w in ('wmic diskdrive get size /format:Textvaluelist.xsl') do for /f "usebackq delims=" %%a in ('%%w') do set %%a
echo %size%
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).