I have the following batch file, however, it freaks out if you have more than one NIC enabled and spits out output of Speed for the first NIC it reports, but for any more, it reports
Speed = Missing Operand
How can I fix this?
#echo off
for /f "tokens=2 delims==" %%a in ('wmic nic where NetEnabled^=true get speed /value ^| find /i "speed"') do set /a speed=%%a
set /a speed=((%speed%/1024)/1024)
echo Speed in megabytes: %speed% Mbps
pause
In addition, why doesn't it work in batch file execution without the '^' before the '=' and '|'
#ECHO OFF &SETLOCAL
for /f "tokens=2 delims==" %%a in ('wmic nic where NetEnabled^=true get speed /value ^| find /i "speed"') do set "speed=%%a"
set /a speed=speed/1048576 2>nul
if %speed% neq 0 (echo Speed in megabytes: %speed% Mbps) else echo No speed available.
You need to escape = and | in the for-loop, no matter if batch or cmd window. It's because it's in the for-loop.
Related
WMIC CPU Get NumberOfCores,NumberOfLogicalProcessors gets me most of what I want, but how do I store the combined output into a variable?
for /f "delims=" %%a in ('"WMIC CPU Get NumberOfCores,NumberOfLogicalProcessors /value"') do set /a "_%%a"
set _
Works for single socket machines only. The WMIC command returns cpu info on separate lines, i.e.
>WMIC CPU Get NumberOfCores,NumberOfLogicalProcessors /value
NumberOfCores=24
NumberOfLogicalProcessors=48
NumberOfCores=24
NumberOfLogicalProcessors=48
#echo off
setlocal
set /A "_NumberOfCores=_NumberOfLogicalProcessors=0"
for /F "tokens=1,2 delims==" %%a in ('WMIC CPU Get NumberOfCores^,NumberOfLogicalProcessors /value') do if "%%b" neq "" set /A "_%%a+=%%b"
set _
Note that wmic command output lines terminated in CR+CR+LF ASCII characters, even the empty lines, so it is necessary to check if the %%b part exists to avoid to process empty lines.
This will do it:
for /f "tokens=2,3 delims=," %%a in ('WMIC CPU Get NumberOfCores^,NumberOfLogicalProcessors /value /format:csv') do set "both=Cores=%%a Processors=%%b"
/format:csv changes up the format some, making it easier for us to manipulate, which I did in the above command.
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: =%]
I have tried for like 3 hours now, with multiple codes similar to this:
wmic cpu get loadpercentage > Load.txt
findstr "%random:~,1%" Load.txt > Load1.txt
set load=<Load1.txt
if %load%==" 2 7 " echo yes
pause
But they all run in to a similar problem, the output of wmic cpu get loadpercentage:
LoadPercentage
56
The format just doesn't allow it to be put into a variable, so I can't check it for anything. Perferably, I would like it to be done in Windows CMD and/or Powershell.
Thanks for the help!
EDIT:
Thanks to #lit for the code, here's my final code that works perfectly:
:: To find the "GUID" or the codes for each power plan, run the command in CMD "powercfg -list".
set HighPerformanceMode=8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c
set PowerSaverMode=a1841308-3541-4fab-bc81-f71556f20b4a
:loop
SETLOCAL ENABLEDELAYEDEXPANSION
SET "load="
FOR /F "usebackq skip=1 tokens=*" %%f IN (`wmic cpu get loadpercentage`) DO (
IF "!load!" EQU "" (
set "load=%%~f"
)
)
if "%load%" geq "65" (
ping localhost -n 2 >nul
if "%load%" geq "65" (
%systemroot%\System32\powercfg.exe /setactive %HighPerformanceMode%
)
) else (
if "%load%" lss "25" (
ping localhost -n 2 >nul
if "%load%" lss "25" (
%systemroot%\System32\powercfg.exe /setactive %PowerSaverMode%
)
)
)
endlocal
ping localhost -n 3 > nul
goto loop
Make sure you change the HighPerformanceMode and PowerSaverMode to have your computer specific power plans. You can find the codes by doing powercfg -list in cmd.
I then made a separate short script that just has "C:\Load Batch\Load Batch.bat" in it, but you have to change it to wherever the main script is. Then I used a program called "BAT to EXE converter" and put it in Ghost Mode, and put the newly made .exe program into my startup folder.
EDIT 2:
I don't believe that my question is a duplicate, the linked question is about getting CPU and RAM usage for what appears to be just to view it, while my question is about getting the load percentage as a pure text form to be used in a script. I am aware of it only testing for one CPU core, but as one goes up it is very likely that the others have similar loads. I had searched this site for code that would separate the "LoadPercentage" text when wmic cpu get loadpercentage is ran, because I couldn't set it into a variable that way.
It is likely that the problem is that wmic output is in Unicode. How about going with PowerShell?
Get-CimInstance -ClassName CIM_Processor | select LoadPercentage
I am not sure from the question about what needs to run.
The typical fallback of cmd shell programmers is usually something like:
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET "X="
FOR /F "usebackq skip=1 tokens=*" %%f IN (`wmic cpu get loadpercentage`) DO (
IF "!X!" EQU "" (
set "X=%%~f"
)
)
ECHO X is %X%
EDIT:
Actually, there will be a LoadPercentage emitted for each core. You probably want the arithmetic mean (average) of them.
Get-CimInstance -ClassName CIM_Processor |
Measure-Object -Property LoadPercentage -Average |
Select Average
Doing this in a cmd script would involve summing the LoadPercentage values and dividing by the count of them.
SET /A TOTAL=0
SET /A CORE_COUNT=0
FOR /F "usebackq skip=1" %%t IN (`type NUL ^| wmic /node:"%SERVER_NAME%" cpu get loadpercentage ^| findstr .`) DO (
IF "%%t" NEQ "" (
SET /A TOTAL=!TOTAL! + %%t
)
SET /A CORE_COUNT=!CORE_COUNT! + 1
)
SET /A AVG_UTILIZATION=%TOTAL% / %CORE_COUNT%
ECHO Number of cores: %CORE_COUNT%
ECHO Total CPU utilisation: %TOTAL%
ECHO Average CPU utilisation: %AVG_UTILIZATION%%%
Although writing to a (temporary) file and reading it back with set /p is a possible and valid way, the usual way of getting the output of a command into a variable in cmd is a for /f loop:
for /f "" %%a in ('wmic cpu get loadpercentage /value ^|find "="') do set /a "%%a"
echo %loadpercentage%
As in lits answer, a find (or findstr) command is used to convert the Unicode output of wmic to a "cmd compatible" format.
I use the /value parameter to get an outputformat that's easier to parse and set /a to get a numeric value (no need to mess with spaces).
The benefit of using /value is, you can easily get more than one parameter from the same wmic command:
for /f "delims=" %%a in ('"wmic cpu get Caption,CurrentClockSpeed,ExtClock,L2CacheSize /value |findstr = "') do set "_%%a"
set _
Enclosing the complete command in quotes removes the need of escaping special chars. Of course this works only, if you don't need quotes in the commandstring itself.
(without quoting:
for /f "delims=" %%a in ('wmic cpu get Caption^,CurrentClockSpeed^,ExtClock^,L2CacheSize /value ^|findstr = ') do set "_%%a"
)
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 want to use
wmic NIC where NetEnabled=true get Name, Speed
to get the NIC speed.
However, I want to do some Math on the Speed to get a more sensible figure, i.e. 1000Mbps or 100Mbps. How can you do such Math in Batch?
You can do simple math in batch-files.
You can use set /a var=1+1 and set /a var=%var%+1 etc.
In your example it would be (for just the speed):
#echo off
for /f "tokens=2 delims==" %%a in ('wmic nic where NetEnabled^=true get speed /value ^| find /i "speed"') do set /a speed=%%a
echo Speed in bytes: %speed% Bps
set /a speed=%speed%/1024
echo Speed in kilobytes: %speed% Kbs
set /a speed=%speed%/1024
echo Speed in megabytes: %speed% Mbs