Create a batch sript that uses findstr and variables to map drive

966 views Asked by At

So I am trying to create batch script that does a findstr of two variables. I am reading from a specific file where I need the variables from.

The variables I need are

NetPath=
NetPathmed=

And my goal is to use the two variables to do a network map drive. In the first findstr I need everything after the NetPath= and in the second I need just a specific amount of data from the string. A normal NetPathMed looks like this

NetPathmed=\\**IPaddress**:C:\folder\file.

I just need the IP address

For example the batch I created looks like this

cd: C:\folder\
set var1="findstr 'NetPath=' file.ini"
for /f "tokens=1,2 delims== " %%a in ("%var1%") do set net=%%a&set path=%%b

Set var2= "findstr 'NetPathMed' opsetup.ini"
for /f "tokens=1,2,3 delims==/ " %%c in ("%var2%") do set net=%%c&set path=%%d &set net=%%e

net use var1(driveletter) \var2(IPaddress)\network folder /persistent:yes
1

There are 1 answers

0
Magoo On
cd C:\folder\
set var1='findstr "NetPath=" file.ini'
for /f "tokens=1,* delims==" %%a in (%var1%) do set var1=%%b

Set var2='findstr "NetPathMed" opsetup.ini'
for /f "tokens=2,3 delims=\:" %%c in (%var2%) do ECHO elements %var1% %%c %%d: /persistent:yes

Well - fixed a bit. Very difficult to tell where the elements you are extracting from your files actually are and which you wish to assemble into your command.

Made worse by your using 'NetPathMedin your code butNetPathmedin the data line you have given. If you don't use the/iswitch,findstr` is case-sensitive.

Other errors:

cd: should not have a :.

The command within the parentheses of the for /f should be enclosed in SINGLE quotes. FINDSTR likes "double quotes"

I couldn't tell whether you wanted the data to be placed in var1 and var2 or what - an example of the relevant contents of the two files and the required command to be generated would have been helpful.

I chose to assign all of the text after the = in the selected line from the first file into var1 (because that's what you said)

The first part in %%a is token1 = NetPath (not used) and the second token (*=the rest of the line) appears in the next metavariable (%%b)

Similarly, parsing the EXACT DATA that you posted, you are only using the second and third tokens

token1 is NetPathmed=
token2 is **IPaddress**
token3 is C
token 4 is \folder\file. I just need the IP address

Note that you specified / not \. Different animal. Chalk and cheese.

So - all you'd need to do is assemble the various elements %var1% %%c and %%d together with the fixed text you require, replace the echo with net use after testing and you should be off...