How to set and recall variables in a BAT file

Hello,

I have a batch file that I would like to set a variable based on which file exists on the disk.  This part I have figured out.  I am stuck on an easy part as to how to set a variable based on that file and how to recall that variable later.  I am basically trying to dynamically map a drive based on where a computer is located.  The batch I have is as follows:

@ECHO OFF

REM "Get correct TeraStation IP Address"
if exist "c:\location1.dat" (
  SET TeraStation = 123.246.2.14
) else (
  if exist "c:\location2.dat" (
    SET TeraStation = 63.224.65.2
  ) else (
    SET TeraStation = 123.246.2.14
  )
)

REM "Map Network Drives and Set Time
  net use * /del /y
  net use s: \\%TeraStation%\SmartLaunch babadook /USER:SL_Client
 
  if "%TeraStation%" = "123.246.2.14" (
     net use n: \\123.246.2.14\World of Warcraft
)

CLS
Exit


As you can see from the batch, I am mapping 1 drive based on a variable name.  I am also checking that variable later and if it equals a certain number, I am mapping a second drive.  For some reason, when it gets to the mapping part, the %TeraStation% shows up as blank, even though I have an "ELSE" statement setting it in case neither file exists.  Please help!

ED7
electricd7Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

sirbountyCommented:
Change this...assuming that babadook is the password?  If not enclose the whole thing in quotes..

REM "Map Network Drives and Set Time
  net use * /del /y
  net use s: "\\%TeraStation%\SmartLaunch" babadook /USER:SL_Client
 
  if "%TeraStation%" = "123.246.2.14" (
     net use n: "\\123.246.2.14\World of Warcraft"
)



As for your initial logic...try this instead:

SET TeraStation = 123.246.2.14
if exist "c:\location1.dat" (
  SET TeraStation = 123.246.2.14
) else (
  if exist "c:\location2.dat" (
    SET TeraStation = 63.224.65.2
   )
)
0
Lee W, MVPTechnology and Business Process AdvisorCommented:
Instead of nesting things (and using spaces) try this:

@ECHO OFF

REM "Get correct TeraStation IP Address"
REM Default Terastation value
SET TeraStation = 123.246.2.14
REM If the location information is not 1 then set the value differently
if exist "c:\location1.dat" SET TeraStation=63.224.65.2

REM "Map Network Drives and Set Time
  net use * /del /y
  net use s: "\\%TeraStation%\SmartLaunch" babadook /USER:SL_Client
 
if "%TeraStation%" = "123.246.2.14" net use n: "\\%terastation%\World of Warcraft"


CLS
Exit



Also, enclose paths in " - those with spaces don't usually work well unless you've defined them through being enclosed in quotes.
0
Shift-3Commented:
leew caught the space problems I was going to mention.  That just leaves the problem with the IF command.  Use == or EQU, not =.
0
Exploring SQL Server 2016: Fundamentals

Learn the fundamentals of Microsoft SQL Server, a relational database management system that stores and retrieves data when requested by other software applications.

electricd7Author Commented:
Ok that will work, but I will soon have 3-4 locations.  How do I modify this to work with more than 2 options?  Thanks.
0
sirbountyCommented:
How about a loop?

for /f %%a in ('dir c:\location*.dat') do goto %%a
goto :eof

:location1.dat
Set TeraStation = 123.246.2.14
goto map
:location2.dat
Set TeraStation = 63.224.65.2
goto map
:location3.dat
Set TeraStation = 123.246.2.14
goto map

:map
REM "Map Network Drives and Set Time
  net use * /del /y
  net use s: "\\%TeraStation%\SmartLaunch" babadook /USER:SL_Client
 
  if "%TeraStation%" = "123.246.2.14" (
     net use n: "\\123.246.2.14\World of Warcraft"
)

Of course, you can continue this model for as many as needed...this assumes only one location#.dat file will exist in the root...
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Lee W, MVPTechnology and Business Process AdvisorCommented:

@echo off
If exist "c:\location1.dat" Goto Location1
If exist "c:\location2.dat" Goto Location2
If exist "c:\location3.dat" Goto Location3
If exist "c:\location4.dat" Goto Location4
Goto LocationError

:Location1
Set TeraStation=63.224.65.2
Goto DoneWithLocations

:Location2
Set TeraStation=123.246.2.14
Goto DoneWithLocations

:Location3
Set TeraStation=a.b.c.d
Goto DoneWithLocations

:Location4
Set TeraStation=w.x.y.z
Goto DoneWithLocations

:LocationError
echo Your location could not be determined.  Contact your administrator.  You are NOT connected to the TeraStation.
Pause
:DoneWithLocations

REM "Map Network Drives and Set Time
  net use * /del /y
  net use s: "\\%TeraStation%\SmartLaunch" babadook /USER:SL_Client
 
if "%TeraStation%" == "123.246.2.14" net use n: "\\%terastation%\World of Warcraft"


CLS
Exit
0
sirbountyCommented:
Correction to the first line...
for /f %%a in ('dir c:\location*.dat') do goto %%a
should read

for /f %%a in ('dir c:\location*.dat /b') do goto %%a
0
electricd7Author Commented:
Thanks all.  I am trying leew's suggestion now.  If it works correctly i will award him the points.  I assume yours will work too sirbounty, but I simply like the method he is using a little better.... stand by.
0
Lee W, MVPTechnology and Business Process AdvisorCommented:
Actually, I like sirbounty's suggestion - hadn't thought of it.  If you like both our suggestions and both work, I would encourage a points split.
0
electricd7Author Commented:
Ok, just ran Leews suggestion and still getting a blank variable for TeraStation here is what I have:

C:\Documents and Settings\Batch>net use * /del /y
There are no entries in the list.


C:\Documents and Settings\Batch>net use s: "\\\SmartLaunch"
System error 53 has occurred.

The network path was not found.


C:\Documents and Settings\Batch>if "" == "63.224.65.2" net use
 n: \\\World of Warcraft
0
sirbountyCommented:
Thanx leew! :^)

Under your :DoneWithLocations
place a

:DoneWithLocations
echo TeraStation is now set to %terastation%
pause
0
electricd7Author Commented:
Yea I like both, but leew's gives me the option to change the "location1.dat" file to a more meaningful name.  But both answer exactly what I asked so long as I can get them not to show up blank variable names in the drive mappings.
0
Lee W, MVPTechnology and Business Process AdvisorCommented:
What version of Windows are you using?  (I just tested this on XP Pro and had no problem whatsoever.
0
electricd7Author Commented:
XP is what I am using....
0
electricd7Author Commented:
let me try again....
0
sirbountyCommented:
Just curious - how are you generating the location#.dat file?
Perhaps you could just have one generic name: location.dat and drop the IP address inside it?
That way you merely read the file to get the associative IP address...
0
Lee W, MVPTechnology and Business Process AdvisorCommented:
Another excellent idea.  I'm unfortunately late for an appointment - I'll be back later but for now, electricd7, you're in good hands with sirbounty.
0
electricd7Author Commented:
You are right....it is working now.  Not sure what I was smoking??  Well I will be using leew's suggestion, but shall I split points if leew thinks SirBounty's is better (it works too, i tried it!)
0
sirbountyCommented:
Very glad to assist.
Thanx leew for the nod.  

Have a great day gents.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Windows Batch

From novice to tech pro — start learning today.