[SOLVED] Suppress message so it shows as blank.

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
PAB
Posts: 139
Joined: 12 Aug 2019 13:57

[SOLVED] Suppress message so it shows as blank.

#1 Post by PAB » 27 Oct 2019 05:54

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.
Last edited by PAB on 07 Nov 2019 09:38, edited 1 time in total.

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Suppress message so it shows as blank.

#2 Post by aGerman » 27 Oct 2019 06:05

This message is coming from WMIC. All you have to do is redirecting its error output in the '...' clause using 2>nul.

Steffen

PAB
Posts: 139
Joined: 12 Aug 2019 13:57

Re: Suppress message so it shows as blank.

#3 Post by PAB » 27 Oct 2019 06:14

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!

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Suppress message so it shows as blank.

#4 Post by aGerman » 27 Oct 2019 06:18

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

PAB
Posts: 139
Joined: 12 Aug 2019 13:57

Re: Suppress message so it shows as blank.

#5 Post by PAB » 27 Oct 2019 07:50

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.

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Suppress message so it shows as blank.

#6 Post by aGerman » 27 Oct 2019 08:07

What about something like that

Code: Select all

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

PAB
Posts: 139
Joined: 12 Aug 2019 13:57

Re: Suppress message so it shows as blank.

#7 Post by PAB » 27 Oct 2019 08:31

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.

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Suppress message so it shows as blank.

#8 Post by aGerman » 27 Oct 2019 08:42

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

PAB
Posts: 139
Joined: 12 Aug 2019 13:57

Re: Suppress message so it shows as blank.

#9 Post by PAB » 27 Oct 2019 09:09

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.

PAB
Posts: 139
Joined: 12 Aug 2019 13:57

Re: Suppress message so it shows as blank.

#10 Post by PAB » 27 Oct 2019 09:41

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.

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Suppress message so it shows as blank.

#11 Post by aGerman » 27 Oct 2019 10:20

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"

PAB
Posts: 139
Joined: 12 Aug 2019 13:57

Re: Suppress message so it shows as blank.

#12 Post by PAB » 07 Nov 2019 09:37

Thanks Steffen, it is appreciated.

Post Reply