Comparing strings in batch files

winprofessional 41 Reputation points
2020-04-17T15:30:53.06+00:00

I am not sure of this is teh right forum to ask this question. If not, kindly direct me to the correct forum.

I am trying to call a WMI command from a batch file like below:

for /f "tokens=2 delims==" %%f in ('wmic /namespace:\some command /value ^| find "="') do set "val=%%f"

IF "%val%"==""( echo Null. Exiting...
  Exit /B
)

The issue is if the WMI command didn't find the query result, then I expect the query to return an empty string. The value I am looking for is a string. So the above empty string comparison does not work so I am not getting the "Exiting...." statement. What is wrong here? What more is to done in the comparison? .The value I am looking for doesn't exist and the script says "No instances found" in command prompt. But I need to inform the user in a nice way. How do one does string or empty comparisons in batch files?

Not Monitored
Not Monitored
Tag not monitored by Microsoft.
35,929 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Dave Patrick 426.1K Reputation points MVP
    2020-04-17T15:40:35.777+00:00

    QnA currently supports the products listed in right-hand pane (more to be added) Better to reach out to subject matter experts in dedicated forums over here.

    https://social.technet.microsoft.com/Forums/scriptcenter/en-US/home?forum=ITCG

    (please don't forget to mark helpful replies as answer)

    0 comments No comments