Newbie stuck on some variables, substrings etc

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Charij
Posts: 3
Joined: 11 May 2011 08:16

Newbie stuck on some variables, substrings etc

#1 Post by Charij » 11 May 2011 08:23

Hi guys,

I'm trying to write a batch script for windows XP. It needs to call ipconfig and build an IP address from the (192.168.XXX.30) XXX part of the address and supplanting it into a template. Then runs some fun stuff from a network shared folder.

Code: Select all

SET _var=ipconfig |FIND "192.168"
SET _var=%_var:~25,-4%

net use z: \\192.168.%_var%.30\test_folder

... Do stuff

net use z: \DELETE

ECHO "Tasks completed"
PAUSE


At the moment I can't seem to get the result from ipconfig in to my variable, would be even better if I could get the substring from it in 1 line. When I try to echo the contents it just prints out _var:~25,-4

Any ideas? Thanks in advance

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

Re: Newbie stuck on some variables, substrings etc

#2 Post by aGerman » 11 May 2011 08:31

Try:

Code: Select all

for /f "tokens=3 delims=." %%a in ('ipconfig ^|find "192.168"') do set "var=%%a"

Regards
aGerman

Charij
Posts: 3
Joined: 11 May 2011 08:16

Re: Newbie stuck on some variables, substrings etc

#3 Post by Charij » 11 May 2011 08:40

Thanks for the reply,

I plugged in what you linked, but it's not finding any string.

Code: Select all

for /f "tokens=3 delims=." %%a in ('ipconfig ^|find "192.168"') do set "_var=%%a"
SET _var=%_var:~25,-4%

net use z: \\192.168.%_var%.30\test_folder

PAUSE

... Do stuff

net use z: \DELETE

ECHO "Tasks completed"
PAUSE



Is there anyway to get the output of

Code: Select all

ipconfig |FIND "192.168" |FIND "IP"

Directly into a variable? I don't see why I need to create a loop for it when the output will always be a single line in my case.

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

Re: Newbie stuck on some variables, substrings etc

#4 Post by aGerman » 11 May 2011 09:36

You need the FOR loop to get the sub string to the variable and it's already the 3rd octett (no need for %_var:~25,-4%).

Code: Select all

@echo off &setlocal
for /f "tokens=3 delims=." %%a in ('ipconfig ^|find "192.168"') do set "_var=%%a"
echo %_var%
pause

Regards
aGerman

Charij
Posts: 3
Joined: 11 May 2011 08:16

Re: Newbie stuck on some variables, substrings etc

#5 Post by Charij » 11 May 2011 09:48

Ah I see works perfectly now and I can see how it works!

Thanks a lot for the help! :)

Post Reply