i have applied this batch script to display all information of my computer here i have getting errors on hdd and sdd like this below
how to correct this script can anyone help me on this
HDD:
3 - Invalid alias verb.
3 - Invalid alias verb.
Missing operand.
Missing operand.
Size: GB
Free space: GB
SSD:
2 - Invalid alias verb.
2 - Invalid alias verb.
Missing operand.
Missing operand.
Size: GB
Free space: GB
this is batch script i have used
#echo off
echo Processor:
wmic cpu get name
echo.
echo RAM:
wmic memorychip get capacity /value
set /a ram=0
for /f "tokens=2 delims==" %%i in ('wmic memorychip get capacity /value ^| find "Capacity"') do set /a ram+=%%i
set /a ramgb=%ram%/1024/1024/1024
echo %ramgb% GB
echo.
echo Motherboard:
wmic baseboard get product,manufacturer
echo.
echo HDD:
for /f "tokens=2 delims==" %%a in ('wmic logicaldisk where drivetype=3 get size /value ^| find "Size"') do set hddsize=%%a
for /f "tokens=2 delims==" %%a in ('wmic logicaldisk where drivetype=3 get freespace /value ^| find "FreeSpace"') do set hddfree=%%a
set /a hddsizegb=%hddsize%/1024/1024/1024
set /a hddfreegb=%hddfree%/1024/1024/1024
echo Size: %hddsizegb% GB
echo Free space: %hddfreegb% GB
echo.
echo SSD:
for /f "tokens=2 delims==" %%a in ('wmic logicaldisk where drivetype=2 get size /value ^| find "Size"') do set ssdsize=%%a
for /f "tokens=2 delims==" %%a in ('wmic logicaldisk where drivetype=2 get freespace /value ^| find "FreeSpace"') do set ssdfree=%%a
set /a ssdsizegb=%ssdsize%/1024/1024/1024
set /a ssdfreegb=%ssdfree%/1024/1024/1024
echo Size: %ssdsizegb% GB
echo Free space: %ssdfreegb% GB
echo.
pause
You don't consider having more than one disk in the system. Your for loops return the last found disk data only. Also, you shouldn't execute wmic for each desired data (wmic is quite slow, so you can improve performance by only executing it once)
Here is an example:
#ECHO off
setlocal enabledelayedexpansion
set i=0
for /f "skip=1 tokens=1-3" %%a in ('"wmic logicaldisk where drivetype=3 get freespace,name,size|findstr /v "^^$" "') do (
set /a i+=1
set "_Freespace[!i!]=%%a"
set "_Name[!i!]=%%b"
set "_Size[!i!]=%%c"
)
set _
Example output (on my system):
_Freespace[1]=21331169280
_Freespace[2]=1547910324224
_Freespace[3]=10803728384
_Freespace[4]=311614398464
_Name[1]=C:
_Name[2]=D:
_Name[3]=E:
_Name[4]=S:
_Size[1]=125791367168
_Size[2]=1973554245632
_Size[3]=26843541504
_Size[4]=960178941952
Also, be aware set /a works with INT32, which limits the range to 2GB. If the numbers for size or freespace are greater than that (very likely nowadays), you'll get an error message. You need another method for translating bytes into kB/MB/GB.
Related
I saw a nice script Here - this code get the Serial Number and Model from list of Remote Computers and export it to csv file.
The only problem is, that the export results have many separated lines instead of ONE line each computer.
I mean, i want to get this:
But i get that:
I also checked Here, but cant get it to work as described.
Here the Batch Code:
#echo off
del list.csv 2>nul
for /f %%i in (pclist.txt) do (
for /f "tokens=2 delims==" %%M in ('wmic /node:"%%i" csproduct get name /value') do for %%m in (%%M) do (
for /f "tokens=2 delims==" %%S in ('wmic /node:"%%i" bios get serialnumber /value') do for %%s in (%%S) do (
>>list.csv echo %%i, %%s, %%m
)
)
)
type list.csv
pause
How do i make the results to export to one line (display SN and model) to each computer?
You can have a go at this.
#echo off
break>list.csv
for /f "delims=" %%i in (pclist.txt) do call :method %%~i
type list.csv
goto :eof
:method
set "host_name=%~1"
for /f "tokens=2* delims==" %%N in ('wmic /node:"%~1" csproduct get name /value') do set "name=%%N"
for /f "tokens=2* delims==" %%S in ('wmic /node:"%~1" bios get serialnumber /value') do set "serial=%%S"
(echo %host_name%,%serial%,%name%)>>list.csv
We just get each of the values once, seeing as we do not incorporate for loops in for loops. Though we could have done this without setting variables, in this instance it makes it a little more readable.
So in this code !msp! in the echo outputs 0.
I am doing a check to see what that number is and then echo out the type. But for some reason its saying none of the if statements are true?
set msp=
for /f "tokens=2 delims==" %%f in ('wmic memorychip get memorytype /value ^| find "="') do (set msp=%%f)
echo !msp!
if !msp!=="0" (
echo Unknown s
)
if !msp!==0 (
echo Unknown n
)
The s and n is just me trying to see if " " matter.
"0" is not equal to 0. Quote both sides of the equation. But that alone does not solve your problem. Wmic returns an ugly line ending of CRCRLF instead of just CRLF. The additional CR gets part of the variable (try echo a!msp!b with your code). One way (actually the most reliable) to solve that, is with another for:
setlocal enabledelayedexpansion
set "msp="
for /f "tokens=2 delims==" %%f in ('wmic memorychip get memorytype /value ^| findstr "="') do for %%g in (%%f) do set "msp=%%g"
echo !msp!
if "!msp!" == "0" (
echo Unknown s
)
if !msp! == 0 (
echo Unknown n
)
In this special case (output is a number), also set /a works:
for /f "tokens=2 delims==" %%f in ('wmic memorychip get memorytype /value ^| findstr "="') do set /a msp=%%f
I want to get the computer name, IP address and version (windows 10) by using a batch file that scans the network.
I have the following:
Set compInfo= "wmic /NODE:192.168.1.%%s OS GET Version,Caption"
Set ipAdd= "wmic /NODE:192.168.1.%%s NICCONFIG WHERE IPEnabled=true GET
IPAddress,DNSHostName"
(FOR /L %%s IN (1,1,254) DO (echo %compInfo%,%ipAdd% >>
C:\Users\xxxx\Desktop\NsLooKup\TestEcho.txt))
I'm trying to have the results in the same Line and possibly have it in CSV format with /Format:
It's not working for me, is it because the variables are outside the for loop?
EDIT:
Note: run command inside for loop
(FOR /L %%s IN (1,1,254) DO wmic /node:192.168.1.%%s OS GET Version,Caption /format:htable & wmic /node:192.168.1.%%s NICCONFIG WHERE IPEnabled=true GET IPAddress,DNSHostName /format:htable) > results.html
When I do this I get the TWO lines as a result instead of ONE. if i can make this as one line for every ip address it would work great :)
#echo off
setlocal enabledelayedexpansion
(
FOR /L %%s IN (1,1,254) DO (
set "compInfo=N/A"
set "IpAdd=N/A"
for /f "delims=" %%a in ('wmic /NODE:192.168.1.%%s os get version^,caption /all^|find "."') do set compInfo=%%a
for /f "delims=" %%a in ('wmic /NODE:192.168.1.%%s NICCONFIG WHERE "IPEnabled=true" GET IPAddress^,DNSHostName /all^|find "."') do set IpAdd=%%a
echo !compInfo:~0,-1!,!IpAdd:~0,-1!
)
)>"C:\Users\xxxx\Desktop\NsLooKup\TestEcho.txt"
I have executed the below command in command prompt.
Input:
C:\>#for /f "skip=1" %p in ('wmic cpu get loadpercentage') do #echo %p%
8%
%
I have written the code to get only 8% in batch script like below,
call :Harddisk
call :CPU
Echo ^</TABLE^> ^</BODY^> ^</HTML^> >> %opfile%
pause
exit /b
:CPU
set wmih=#for /f skip=1 %p in ('wmic cpu get loadpercentage') do #echo %p% |Find /c /v ":"
ECHO wmih
for /f "tokens=1,2,3,4,5" %%a in ('"%wmih%"') do (
Echo ^<TR^> ^<TD^> >> %opfile%
Echo CPU Utilization: %%a >> %opfile%
Echo ^</TD^> ^</TR^> >> %opfile%
)
exit /b
I'm not getting proper output. Please help me to sort it out.
Thanks!!!
for /f "tokens=2 delims==" %%i in ('wmic cpu get loadpercentage /value ^|find "="') do echo set p=%%i
set p=%p:~0,-1%
echo %p%
Just to keep your structure
:CPU
set "wmih=#for /f "usebackq skip^^^=2 tokens^^^=2 delims^^^=^^^," %%p in (`wmic cpu get loadpercentage^^^,version /format^^^:csv`) do #echo(%%p%%"
ECHO %wmih%
for /f "tokens=1,2,3,4,5" %%a in ('"%wmih%"') do (
>>%opfile% Echo ^<TR^> ^<TD^>
>>%opfile% Echo CPU Utilization: %%a
>>%opfile% Echo ^</TD^> ^</TR^>
)
To be able to store the command inside a variable and then pass it to the for command, that will spawn another cmd instance to execute the command, you need a lot of scape characters just to handle the command execution.
It can be made easier
:CPU
for /f "skip=2 tokens=2 delims=," %%a in (
'wmic cpu get loadpercentage^,version /format^:csv'
) do (
>>%opfile% Echo ^<TR^> ^<TD^>
>>%opfile% Echo CPU Utilization: %%a%%
>>%opfile% Echo ^</TD^> ^</TR^>
)
In both cases, to remove the aditional CR character at the end of the retrieved value, the query request the output in csv format with an aditional field, so, the needed data is not at the end of the line and the value will not include the ending CR
I'm using the below code to output the current free space on the C: Drive. How can I convert the output from bytes to GB using batch?
#echo off
for /f "usebackq delims== tokens=2" %%x in (`wmic logicaldisk where "DeviceID='C:'" get FreeSpace /format:value`) do set FreeSpace=%%x
echo %FreeSpace%
Batch does not support float point arithmetic. This would be a nice workaround:
#setlocal enableextensions enabledelayedexpansion
#echo off
for /f "usebackq delims== tokens=2" %%x in (`wmic logicaldisk where "DeviceID='C:'" get FreeSpace /format:value`) do set FreeSpace=%%x
echo !FreeSpace:~0,-10!,!FreeSpace:~2,-8!GB
It only works if you run the .bat as administrator. It just inserts a dot after the 9. digits from the right, and trims the last 7. This is not exactly matching the value from windows, because 1k is here 1000 and not 1024
A better but more complex solution would be to use VBScript, described in the following article: Article
Here is a solution that gives GB in a whole number. May not be what you wanted, but it was easy to do, and may do the trick for what you need. I couldn't really get it to work for me using wmic, but wmic is probably better than dir.
#setlocal enableextensions enabledelayedexpansion
#echo off
for /f "tokens=3" %%a in ('dir c:\') do (
set bytesfree=%%a
)
set bytesfree=%bytesfree:,=%
endlocal && set bytesfree=%bytesfree%
rem truncating end. loses precision
set /a kb=%bytesfree:~0,-3%
set /a mb = kb/1024
set /a gb = mb/1024
echo %gb%
Eh, well, here is the same thing using wmic.
#setlocal enableextensions enabledelayedexpansion
#echo off
for /f "usebackq delims== tokens=2" %%x in (`wmic logicaldisk where "DeviceID='C:'" get FreeSpace /format:value`) do set FreeSpace=%%x
rem truncating end. losing precision
set /a kb=%FreeSpace:~0,-4%
set /a mb = kb/1024
set /a gb = mb/1024
echo %gb%
Must you use batch commands? Can you not use PowerShell?
[System.IO.DriveInfo]::GetDrives() | Where {$_.Name -eq 'C:\'} |
Select {$_.AvailableFreeSpace/1GB}
REM ECHO Disk Storage
for /f "tokens=1" %%d in (
'wmic logicaldisk where drivetype^=3 get deviceid ^| find ":"') do (
for /f "usebackq delims== tokens=2" %%x in (`wmic logicaldisk where "DeviceID='%%d'" get Size /value`) do set Size=%%x
echo VolumeSize on %%d Partition = !Size:~0,-10!,!Size:~2,-8! GB >output.txt
for /f "usebackq delims== tokens=2" %%x in (`wmic logicaldisk where "DeviceID='%%d'" get FreeSpace /value`) do set FreeSpace=%%x
echo Freespace on %%d Partition = !FreeSpace:~0,-10!,!FreeSpace:~2,-8! GB >> output.txt
echo.
)
)
Could use the StrFormatByteSize64() API function to convert a long int to human readable size. This results in a more accurate value than truncating to meet the cmd environment's 32-bit limit. This API function supports values in the ranges from bytes and kilobytes to petabytes and exabytes.
(This script is a hybrid Batch + PowerShell script. Save it with a .bat extension.)
<# : batch portion
#echo off & setlocal
for /f "tokens=2 delims==" %%x in (
'wmic logicaldisk where "DeviceID='C:'" get FreeSpace /value'
) do call :int2size FreeSpace %%x
echo %FreeSpace% free on C:
rem // end main runtime
exit /b
rem // batch int2size function
:int2size <return_varname> <int>
setlocal
set "num=%~2"
for /f "delims=" %%I in (
'powershell -noprofile "iex (${%~f0} | out-string)"'
) do endlocal & set "%~1=%%I" & goto :EOF
: end batch / begin PowerShell #>
Add-Type #'
using System.Runtime.InteropServices;
namespace shlwapi {
public static class dll {
[DllImport("shlwapi.dll")]
public static extern long StrFormatByteSize64(ulong fileSize,
System.Text.StringBuilder buffer, int bufferSize);
}
}
'#
$sb = new-object Text.StringBuilder 16
[void][shlwapi.dll]::StrFormatByteSize64($env:num, $sb, 16)
$sb.ToString()