Page 1 of 1

[SOLVED] Suppress message so it shows as blank.

Posted: 27 Oct 2019 05:54
by PAB
Good morning,

I have a piece of code that works correctly if the data DefaultIPGateway is available. If the data isn't available, it produces the message "No Instance(s) Available.".

Code: Select all

For /f "tokens=2,3 delims={,}" %%a In ('"WMIC NICConfig Where IPEnabled="True" Get DefaultIPGateway /Value | Find "I" "') Do ^
echo    Default IP Gateway : %%~a >> "%OUTPUT%"
What I would like is for it to output Default IP Gateway:, and then leave it blank if the data isn't available.

I have tried the following which does output Default IP Gateway:, but still outputs the "No Instance(s) Available." which I want the message suppressed so it doesn't ouput on the cmd screen.

Code: Select all

For /f "tokens=2,3 delims={,}" %%a In ('"WMIC NICConfig Where IPEnabled="True" Get DefaultIPGateway /Value | Find "I" "') Do Set %_DefaultIPGateway%
echo    Default IP Gateway :%_DefaultIPGateway%
echo    Default IP Gateway :%_DefaultIPGateway% >> "%OUTPUT%"

Code: Select all

For /f "tokens=2,3 delims={,}" %%a In ('"WMIC NICConfig Where IPEnabled="True" Get DefaultIPGateway /Value | Find "I" "') Do Set _DefaultIPGateway=%%a
echo    Default IP Gateway :%_DefaultIPGateway%
echo    Default IP Gateway :%_DefaultIPGateway% >> "%OUTPUT%"
Thanks in advance.

Re: Suppress message so it shows as blank.

Posted: 27 Oct 2019 06:05
by aGerman
This message is coming from WMIC. All you have to do is redirecting its error output in the '...' clause using 2>nul.

Steffen

Re: Suppress message so it shows as blank.

Posted: 27 Oct 2019 06:14
by PAB
Thanks for the reply. I have tried that but I can't seem to get it to work. I tried putting it in different places!

Re: Suppress message so it shows as blank.

Posted: 27 Oct 2019 06:18
by aGerman
What did you try? How should we know?
Give that a go:

Code: Select all

For /f "tokens=2,3 delims={,}" %%a In ('"WMIC NICConfig Where IPEnabled="True" Get DefaultIPGateway /Value 2>nul | Find "I" "') Do ^
Steffen

Re: Suppress message so it shows as blank.

Posted: 27 Oct 2019 07:50
by PAB
Thank you so much for that.

I narrowed down the code to two. I have tested them on two computers, one with data, and the other without, and listed the results below.

Code: Select all

For /f "tokens=2,3 delims={,}" %%a In ('"WMIC NICConfig Where IPEnabled="True" Get DefaultIPGateway /Value 2>nul | Find "I" "') Do ^
echo    Default IP Gateway : %%~a
echo    Default IP Gateway : %%~a >> "%OUTPUT%"
On the computer WITH the data . . .
it shows Default IP Gateway : in the cmd window and the correct address,
and in the text file it shows Default IP Gateway : %~a, which is incorrect.

On the computer WITHOUT the data . . .
it doesn't show in the cmd window, which is incorrect,
and in the text file it shows Default IP Gateway : %~a, which is incorrect.

So probably NOT the one above then!

Code: Select all

For /f "tokens=2,3 delims={,}" %%a In ('"WMIC NICConfig Where IPEnabled="True" Get DefaultIPGateway /Value 2>nul | Find "I" "') Do Set _DefaultIPGateway=%%a
echo    Default IP Gateway :%_DefaultIPGateway%
echo    Default IP Gateway :%_DefaultIPGateway% >> "%OUTPUT%"
On the computer WITH the data . . .
it shows Default IP Gateway : in the cmd window and the correct address but with speech marks around it, which is incorrect because I don't want the speech marks,
and in the text file it shows Default IP Gateway : and the correct address but with speech marks around it, which is incorrect because I don't want the speech marks.

On computer WITHOUT the data . . .
it shows in the cmd window as Default IP Gateway : and no address, which is correct,
and in the text file it shows Default IP Gateway : and no address, which is correct.

Thanks in advance.

Re: Suppress message so it shows as blank.

Posted: 27 Oct 2019 08:07
by aGerman
What about something like that

Code: Select all

if defined _DefaultIPGateway (echo %_DefaultIPGateway%) else (echo not available)
Steffen

Re: Suppress message so it shows as blank.

Posted: 27 Oct 2019 08:31
by PAB
Thanks Steffen, it is appreciated.

OK, so now I have this that works with and without the data . . .

Code: Select all

For /f "tokens=2,3 delims={,}" %%a In ('"WMIC NICConfig Where IPEnabled="True" Get DefaultIPGateway /Value 2>nul | Find "I" "') Do Set _DefaultIPGateway=%%a
If Defined _DefaultIPGateway (echo    Default IP Gateway : %_DefaultIPGateway%) ELSE (echo    Default IP Gateway :)
echo    Default IP Gateway :%_DefaultIPGateway% >> "%OUTPUT%"
The ONLY thing is, how can I produce the results WITHOUT the speech marks please?

Thanks in advance.

Re: Suppress message so it shows as blank.

Posted: 27 Oct 2019 08:42
by aGerman
You know the solution because you already used it :?
... Do Set "_DefaultIPGateway=%%~a"

If you like string manipulation better, you can also remove them later on
... echo %_DefaultIPGateway:~1,-1% ...

Serious question: Do you even understand your own code or is everything just copy/paste from the internet?

Steffen

Re: Suppress message so it shows as blank.

Posted: 27 Oct 2019 09:09
by PAB
Thanks Steffen, the first bit of code sorted it out . . .

Code: Select all

Do Set "_DefaultIPGateway=%%~a"
I will have a try at incorporating your second bit of code at some stage [but the previous bit of code seems easier] . . .

Code: Select all

echo %_DefaultIPGateway:~1,-1%
I am not a programmer or anything like that [too old now approaching the age 60]. I search very hard on the Internet to try and find what I am looking for and then try even harder on my own to amend the code to do what I would like it to do. Most times I can accomplish this on my own, but sometimes I need help, hence why I post here!

I do appreciate your help and the help of others who almost every time help me accomplish what I need.

Thank you everybody, it really is appreciated.

Re: Suppress message so it shows as blank.

Posted: 27 Oct 2019 09:41
by PAB
Hi Steffen,

I just reviewed this thread and it appears that I left out the ~ on all of my code!
When I included the ~, and didn't use the speech marks in . . .

Code: Select all

Do Set "_DefaultIPGateway=%%~a"
. . . it still worked!

Thanks again.

Re: Suppress message so it shows as blank.

Posted: 27 Oct 2019 10:20
by aGerman
Yes, of course the culprit was the missing tilde as a FOR /? in a cmd window would have told you. It explains all of the modifiers used in FOR variable btw.

As to the surrounding quotation marks in my assignment - this is just best practice to avoid side effects.
Try
set myvar=a&b
vs.
set "myvar=a&b"

Re: Suppress message so it shows as blank.

Posted: 07 Nov 2019 09:37
by PAB
Thanks Steffen, it is appreciated.