Page 1 of 1

Newbie stuck on some variables, substrings etc

Posted: 11 May 2011 08:23
by Charij
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

Re: Newbie stuck on some variables, substrings etc

Posted: 11 May 2011 08:31
by aGerman
Try:

Code: Select all

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

Regards
aGerman

Re: Newbie stuck on some variables, substrings etc

Posted: 11 May 2011 08:40
by Charij
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.

Re: Newbie stuck on some variables, substrings etc

Posted: 11 May 2011 09:36
by aGerman
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

Re: Newbie stuck on some variables, substrings etc

Posted: 11 May 2011 09:48
by Charij
Ah I see works perfectly now and I can see how it works!

Thanks a lot for the help! :)