Avatar of fileinster
fileinsterFlag for United Kingdom of Great Britain and Northern Ireland

asked on 

Making Batch files at on output of commands

Hi all,
I am trying to write a windows bactch file and I want to type a command, and have to batch file differently depending on the output of the command.

I know thi can be done as I've done it before with the for command, but I plagiarised someone else's work and I can remember how it was done. The line was rather complicated.
What I did before was set certain variables on the computer based on the default gateway of the host. I did an IPCONFIG and had the for command look for what was set a the gateway using the tokens variable and feed that back to the line.


Can anyone help me with this? Much appreciated.
Windows Batch

Avatar of undefined
Last Comment
Steve Knight
Avatar of Steve Knight
Steve Knight
Flag of United Kingdom of Great Britain and Northern Ireland image

Well the program output and what you want to check for would be helpfu but for now.
@echo off
for /f  "tokens=*" %%a in ('yourcommand.exe') do echo %%a, %%b, %%c
 
would run down each line of output of "yourcommand.exe" which you add in single quotes in the middle of the for line and and split the lines after every space and then assign first "field" to %%a, second to %%b etc. so if yourcommand.exe output
abc def ghi
then it would show
abc, def, ghi
Steve
Avatar of fileinster
fileinster
Flag of United Kingdom of Great Britain and Northern Ireland image

ASKER

It was something like

for /F "tokens=14" %s IN ('ipconfig') do echo %s

But there was a way to filter the line containing the word "gateway" which I think was done in the command section using a carat, maybe? Also now I'm on a vista host the token seem to have changed. I can't seem to display the gateway using any tokens now!
Avatar of Steve Knight
Steve Knight
Flag of United Kingdom of Great Britain and Northern Ireland image

Best bet first is to find the command which only gives you the line you want if you want only one, e.g. if you want only this line from ipconfig then you could do
ipconfig | find "ateway"    (in case it is gateway or Gateway in caps)
When you use a | or < or > character in the for command you use a caret,a s you say, i.e.
for /f "tokens=14" %%a in ('ipconfig ^| find "ateway"') do echo %%a
Also if doing htis from command line then you use one % not two.  In batch files you use two...
My vista machine is at the office so can't look at the moment to see the difference.  if you want to post the output of the ipconfig machine (amend the ip's if you like but don't change the layout) then will check why.
Steve
Avatar of fileinster
fileinster
Flag of United Kingdom of Great Britain and Northern Ireland image

ASKER

I think that's exactly what I used to do, but as Vista's output has changed to include IPv6 now all I get from that output is:

>ipconfig | find "ateway"
   Default Gateway . . . . . . . . . :
   Default Gateway . . . . . . . . . : fe80::2%9
   Default Gateway . . . . . . . . . :
   Default Gateway . . . . . . . . . :
   Default Gateway . . . . . . . . . :
   Default Gateway . . . . . . . . . :


The IPv4 gateways are on separate lines underneath that. any ideas where to go from here?

Avatar of Steve Knight
Steve Knight
Flag of United Kingdom of Great Britain and Northern Ireland image


OK, what does it show for the IPV4 entries? Do you actually need IPV6, 9999999:1 you don't in which you could unbind IPV6 from the LAN card.

If it is on a line on it's own then you are going to need to be a bit more creative. You might be better off using a VB Script:

On Error Resume Next
Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
Set colItems = objWMIService.ExecQuery ("Select * from Win32_NetworkAdapterConfiguration where IPEnabled=TRUE")
For Each obj in colItems
if obj.DefaultIPGateway<>"" then Wscript.Echo obj.DefaultIPGateway
next

save it as getgateway.vbs and call that from batch with

@echo off
for /f "tokens=*" %%a in ('cscript //nologo getgateway.vbs') do if not "%%a"=="" set gateway=%%a
echo The gateway is %gateway%

Not sure how well that works with DHCP, let us know.

Steve
Avatar of fileinster
fileinster
Flag of United Kingdom of Great Britain and Northern Ireland image

ASKER

The reason why I wanted to do it at the cmd line is because I don't know VB script. But thanks for the script. However, it complains when I run it, at the last then command:

getgateway.vbs(5, 27) Microsoft VBScript compilation error: Expected 'Then'

I think If we can get this working then problem solved, and more eloquent than my solution!
Avatar of Steve Knight
Steve Knight
Flag of United Kingdom of Great Britain and Northern Ireland image

Looks like the "not equal to" consisting of less than and more than symbols has been translated into the web browser codes of < and >   Replace those and give it a go.

Steve
On Error Resume Next
Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
Set colItems = objWMIService.ExecQuery ("Select * from Win32_NetworkAdapterConfiguration where IPEnabled=TRUE")
For Each obj in colItems
if obj.DefaultIPGateway <>"" then Wscript.Echo obj.DefaultIPGateway
next 
 
save it as getgateway.vbs and call that from batch with
 
@echo off
for /f "tokens=*" %%a in ('cscript //nologo getgateway.vbs') do if not "%%a"=="" set gateway=%%a
echo The gateway is %gateway%
 
Not sure how well that works with DHCP, let us know.

Open in new window

Avatar of fileinster
fileinster
Flag of United Kingdom of Great Britain and Northern Ireland image

ASKER

OK, it now exists without any error message, but it doesn't seem to get the gateway. The gateway variable is blank
Avatar of Steve Knight
Steve Knight
Flag of United Kingdom of Great Britain and Northern Ireland image

Hmm, I get the same.  Looks like it doesn't work with DHCP.... Thanks Microsoft!   Will look what else I can find.
Avatar of Steve Knight
Steve Knight
Flag of United Kingdom of Great Britain and Northern Ireland image

Actually my fault, the gateway entry is an array (i.e. potentially multiple entries) so to show them use this:


On Error Resume Next
Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
Set colItems = objWMIService.ExecQuery ("Select * from Win32_NetworkAdapterConfiguration where IPEnabled=TRUE")
For Each obj in colItems
  for each gw in obj.DefaultIPGateway
     wscript.echo gw
  next
next

Open in new window

Avatar of Steve Knight
Steve Knight
Flag of United Kingdom of Great Britain and Northern Ireland image

You could also of course move your logic currently in the batch file into the VB Script file too using the better string handling etc. Post your script if you want help adjusting.
Avatar of fileinster
fileinster
Flag of United Kingdom of Great Britain and Northern Ireland image

ASKER

See, this is why I didn't want to do it in VBS; I don't know it well enough. However, that works like a charm. The only problem I have left is I have three gateways: one IPv4 and two IPv6. I can't switch off IPv6 as you previously suggested as I am using it, and in a production environment most Vista PCs would have it enabled any way.

So, if I have more than one output can you offer any more suggestions for filtering or setting each gateway as a different variable, such as %GW1 %GW2 %GW3 etc?
Avatar of Steve Knight
Steve Knight
Flag of United Kingdom of Great Britain and Northern Ireland image

Wow, someone actually using IPv6!  You could do it with gw gw2 gw3 etc. as something like this.  Tend to agree about VBS, I'm much more comfortable with batch or full VB / VBA but needs must...

@echo off
setlocal enabledelayedexpansion
set count=1
for /f "tokens=*" %%a in ('cscript //nologo gateway.vbs') do call :getgateway %%a

REM Show all the gateways
echo There are %count% gateways:
set gw

goto :eof
:getgateway
set gw%count%=%*
set /a count=count+1
goto :eof

If you were going to do it all in VB Script it would probably end up being easier, though as you say comfort factor says use batch.
ASKER CERTIFIED SOLUTION
Avatar of Steve Knight
Steve Knight
Flag of United Kingdom of Great Britain and Northern Ireland image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of fileinster
fileinster
Flag of United Kingdom of Great Britain and Northern Ireland image

ASKER

Superb! That's the job. VB was something I wanted to learn but never got around to. Out of interest, have you any experience with Powershell; is it better than VBS?
Avatar of fileinster
fileinster
Flag of United Kingdom of Great Britain and Northern Ireland image

ASKER

Cheers, mate! You stuck it out 'til the end!
Avatar of Steve Knight
Steve Knight
Flag of United Kingdom of Great Britain and Northern Ireland image

On the To Do list... .along with about 15000 other things ...  scheduled in for 2034 at the moment.  Thanks for the points etc.

Steve
Windows Batch
Windows Batch

Batch files are text files containing a script of commands that are executed by the command interpreter on DOS, OS/2 and Windows systems. Most commonly, they are used to perform a series of functions that are repeated -- copying a set of files created daily with one step, for example.

13K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo