if statement not working when the variable does have a value - batch-file

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

Related

Batch file to collect UUID and Serial number

I have been using this batch file to collect the Serial number and UUID number and output to a CSV and now it no longer works.
#echo off
set outputfile="Y:\HP\UUDI.csv"
for /f "delims== tokens=2" %%i in ('wmic csproduct Get "UUID" /value') do SET CSPRODUCT=%%i
for /f "delims== tokens=2" %%i in ('wmic bios get serialnumber /value') do SET SERIAL=%%i
echo UUID,Serial,>>%outputfile%
echo %CSPRODUCT%,%SERIAL%,>>%outputfile%
If someone can look at this file and help me understand what went wrong I would appreciate it
I don't understand what did you mean by "No Longer Works" ? Please be more explicit when you ask a question !
here is a test and tell me if this works or not on your side and i will edit this aswer according to your response !
#echo off
set "outputfile=%~dp0UUDI.csv"
#for /f %%i in (
'wmic csproduct Get "UUID" /value ^& wmic bios get serialnumber /value'
) do (
#for /f %%j in ("%%i") do set "%%j" & echo "%%j"
)
echo UUID,SerialNumber>"%outputfile%"
echo %UUID%,%SERIALNumber%>>"%outputfile%"
If exist "%outputfile%" Start "" "%outputfile%" & Exit
The only reason I can see for your provided code to change its behavior, is that which was commented already by Mofi. That is, you've somehow caused the location of WMIC.exe to have been removed from the %Path% environment.
I have decided to provide an alternative method of achieving your goal using your chosen command utility WMIC.exe, and using its full path, to prevent such a reliance in future.
The WMIC command is traditionally one of the slower ones, so this method invokes it only once. All you should need to do is Echo your commands, currently on lines 12and 14, each separated as in line 13. If any of your commands requires to Get more than one property, you should separate those with caret escaped commas, e.g. Get Property1^,Property2. The results, (subject to line/environment length limitations), will then be saved to variables, %Title%, and %Record%, which can later be output to a file outside of the loop. Note: all commands should use /Value, or the more correct, /Format:List.
Example, (don't forget to adjust your output file path on line 4 as needed):
#Echo Off
SetLocal EnableExtensions DisableDelayedExpansion
Set "outputfile=Y:\HP\UUDI.csv"
Set "WMIC=%SystemRoot%\System32\wbem\WMIC.exe"
Set "FIND=%SystemRoot%\System32\find.exe"
Set "Title="
Set "Record="
For /F "Tokens=1,* Delims==" %%G In ('
(
Echo CSProduct Get UUID /Value
^&
Echo BIOS Get SerialNumber /Value
^)
^| %WMIC% ^| %FIND% "="
') Do (If Not Defined Title (Set "Title=%%G") Else (
SetLocal EnableDelayedExpansion
For /F "Tokens=*" %%I In ("!Title!") Do (EndLocal
Set "Title=%%I,%%G"))
If Not Defined Record (Set "Record=%%H") Else (
SetLocal EnableDelayedExpansion
For /F "Tokens=*" %%I In ("!Record!") Do (EndLocal
Set "Record=%%I,%%H")))
If Defined Title ( Echo %Title%
Echo %Record%) 1>"%outputfile%"

| was unexpected at this time in Windows 7

Here is my batch file
#echo off
set /a IPCOUNT=1
set IP_FILTER=^IPV4
for /f "tokens=1,2,3 delims=:" %%i in ('2^>nul ipconfig | findstr /i "!IP_FILTER!" | findstr /v "%"') do (
if "%%j" NEQ " 127.0.0.1" (
echo !IPCOUNT! %%j
set IP_CHECK=%%j
echo !IP_CHECK!>>"%REGULAR_IP_FILE%"
set /a IPCOUNT+=1
)
)
When I run this code, I am getting the following error:
| was unexpected at this time.
How can I solve this?
The fixed code is as follows:
#echo off
setlocal EnableDelayedExpansion
set "IPcount=1"
set "IP_filter=IPV4"
for /F "tokens=2 delims=:" %%A in ('2^>nul ipconfig ^| findstr /IRC:"%IP_filter%"') do (
for /F "tokens=*" %%B IN ("%%A") do (
if not "%%B" == "127.0.0.1" (
echo !IPcount! %%B
(echo %%B)>>"%REGULAR_IP_FILE%"
set /a "IPcount+=1"
)
)
)
I have enabled delayed expansion. It seems you use it in your file, but you haven't enabled it.
It seems you want to find your IPv4 Address. I have modified your script to add a better way. Note that IPv4 (case-insensitive) is not in the start of the line. A space is in the start of the line, so I removed ^, but kept regex enabled.
Quote set variables and values: set /a "var=value".

Missinng Operand For Loop Batch

Below is a piece of code that is "supposed" to be used to, ping all IP's on my network and return the Computer name to an .txt file on my desktop. Every time I run it gives me "missing operand" error. Any help would be nice, gracias!
#Echo Off
Title Getting all Computer Names from Network. . .
Color A
SETLOCAL EnableDelayedExpansion
REM Convert Current IPv4 Address to Variable.
For /F "skip=1 delims={}, " %%A in ('wmic nicconfig get ipaddress') do For /F "tokens=1" %%B in ("%%~A") do set "IP=%%~B"
SET "offsets=0.0.0.0"
For /F "tokens=1-4 delims=. " %%A in ("%IP%") do (
For /F "tokens=1-4 delims=." %%I in ("%offsets%") do (
set /A octetA=%%A+%%I, octetB=%%b+%%j, octetC=%%c+%%k, octetD=%%d+%%l
)
)
REM Do a Ping Sweep To Get Computer Name.
For /L %%B in (1,1,254) do For /F "Tokens=2 Delims== " %%A in ('wmic /node:"%octetA%.!octetB!.!octetC!.%%B" computersystem get name /value') do set "Host=%%A"
REM Output Computer Name In Text File
Echo %Host% >> "C:\Users\%username%\Desktop\ComputerNames.txt"
pause
set /A octetA=%%A+%%I, octetB=%%b+%%j, octetC=%%c+%%k, octetD=%%d+%%l
metavariables are case-sensitive.
set /A octetA=%%A+%%I, octetB=%%B+%%J, octetC=%%C+%%K, octetD=%%C+%%L

Get Windows version ,and edition, pass it to variable to use with findstr after

I'm noob it's my first question, but after searching tons of answers here I still can't quite get what I want. So the problem: need to get windows version ,and edition and pass it to variable to run findstr on it afterwards I get the variable with the edition however findstr can't use it to find string in txt file.
what I have done: I'm using this code to get the ver and save it as var
#echo off
setlocal EnableDelayedExpansion
for /f "tokens=2 delims==" %%G in ('wmic os get Caption /value') do (
set winEdition=%%G
)
echo !winEdition!
endlocal
goto :eof
Output: Microsoft Windows 7 Enterprise
however if I run IF statement or findstr with !winEdition! var I get no result with IF and "string not found" with findstr also if I echo the var to a txt file I get "牣獯景⁴楗摮睯⁳‷湅整灲楲敳†਍" in the txt file so I think it's encoding problem, but I can't find a way to fix it.
More details: the full code suppose to take the var from the code above, search for the string in txt file and return the next line full code:
#echo OFF
setlocal EnableDelayedExpansion
set "winEdition="
for /f "tokens=2 delims==" %%G in ('wmic os get Caption /value') do (
set winEdition=%%G
)
set numbers=
for /F "delims=:" %%a in ('findstr /I /N /C:"!winEdition!" serials.txt') do (
set /A after=%%a+1
set "numbers=!numbers!!after!: "
)
rem Search for the lines
for /F "tokens=1* delims=:" %%a in ('findstr /N "^" serials.txt ^| findstr /B "%numbers%"') do (
echo %%b
)
endlocal
goto :eof
The second part of the code works and tested with string and with var I want to automate it further. Thank you for help I am also open to alternative ways to get the result!
wmic prints some additional "empty" lines (1), which overwrites your value (watch with echo on)
#echo off
setlocal
set "winEdition="
for /f "tokens=2 delims==" %%G in ('wmic os get Caption /value') do (
if not defined winEdition set winEdition=%%G
)
echo %winEdition%
(1) on screen they appear to be empty, but technically they aren't. They contain a crippeled line break, which causes a lot of headache...
Hi again I found the answer!!! So I just used a different command to find the win version, and edition thanks to #LotPings, it's a bit slower than wmic but it works consistently every time.
Code:
#echo off
setlocal EnableDelayedExpansion
for /f "tokens=4-6" %%a in ('"systeminfo | find /i "OS Name""') do (
set "ver=%%a %%b %%c"
)
set "numbers="
for /F "delims=:" %%a in ('findstr /I /N /C:"!ver!" serials.txt') do (
set /A after=%%a+1
set "numbers=!numbers!!after!: "
)
for /F "tokens=1* delims=:" %%a in ('findstr /N "^" serials.txt ^| findstr /B "%numbers%"') do (
echo %%b
)
if "%ver%" equ "Windows 10 Pro" echo YES
endlocal
goto :eof
Thank you all for help, and suggestions!
ps. the serials.txt is just a normal text file created with notepad encoding UTF-8 and the content is just:
Windows 10 Pro
XXXXX-XXXXX-XXXXX-XXXXX-XXXXX
Windows 7 Pro...
and IF is just another check that it works in original code the problem is not with the second part nor with serials.txt because I couldn't compare the !winEdition! with IF statement.
BTW found the problem with my original solution thx to the one I found above
the problem was wrong token= solution:
#echo off
setlocal EnableDelayedExpansion
for /f "tokens=3-5 delims== " %%A in ('wmic os get Caption /value ^| find "Windows" ') do (
set "winVer=%%A %%B %%C"
)
echo !winVer!
set "numbers="
for /F "delims=:" %%a in ('findstr /I /N /C:"!winVer!" serials.txt') do (
set /A after=%%a+1
set "numbers=!numbers!!after!: "
)
for /F "tokens=1* delims=:" %%a in ('findstr /N "^" serials.txt ^| findstr /B "%numbers%"') do (
echo %%b
)
endlocal
goto :eof

Get the result of a WMIC command and store it in a variable

I have seen some batch scripts working that way, including all around stackoverflow.
My question is simple: Why the MEM part isn't working?
#echo OFF
SET CPU="$CPU"
echo CPU: %NUMBER_OF_PROCESSORS%
FOR /F "delims=" %%i IN ('wmic computersystem get TotalPhysicalMemory') DO set MEM=%%i
echo MEM: %MEM%
You can do something like that with formatting the output of WMIC with For / Do loop like that :
#echo off
Call :GetTotalPhysicalMemory
echo TotalPhysicalMemory = %MEM% & pause
exit
::***********************************************
:GetTotalPhysicalMemory
for /f "tokens=2 delims==" %%a in ('
wmic computersystem get TotalPhysicalMemory /value
') do for /f "delims=" %%b in ("%%a") do (
Set "MEM=%%b"
)
exit /b
::***********************************************
That's simple. wmic computersystem get TotalPhysicalMemory outputs three lines of text:
TotalPhysicalMemory
12867309568
<blank line>
So your for-loop does three iteration. In the first one MEM is set to TotalPhysicalMemory, in the second one it's set to 12867309568 and finally it becomes . So your output is empty.
This is quite ugly but will solve your problem:
#echo OFF
setlocal enabledelayedexpansion
SET CPU="$CPU"
echo CPU: %NUMBER_OF_PROCESSORS%
FOR /F "delims= skip=1" %%i IN ('wmic computersystem get TotalPhysicalMemory') DO (
set MEM=%%i
goto STOP
)
:STOP
echo MEM: !MEM!
skip=1 will ignore TotalPhysicalMemory and goto STOP will break the loop after the first iteration.

Resources