Solved

How to identify the path in batch file - %Variable%

Posted on 2011-09-15
5
348 Views
Last Modified: 2012-06-21
I'm working on a 2003 Windows Server, and one of the batch files that previously worked but no longer does after an upgrade, is a variable that I cannot figure out.

The batch file reads as such:
call "%SoftwareDir%\Utils\change.bat" "%1" "%2"
but I'll receive the following, "The system cannot find the path specified."
I know the path of the directories it is supposed to be pulling from and if I edit the batch file with the exact path such as:
call "D:\Software\Utils\change.bat" "%1" "%2"
the script will run without error.

What I'm trying to figure out is what the %SoftwareDir% refers to, and where it is set.  I thought it would be listed under the Set command, but after seeing the output from Set, I don't see anything referencing "SoftwareDir."  Anybody know where I can find what path this variable is referring to?  Thanks.

Eric
0
Comment
Question by:Nevada6372
  • 3
5 Comments
 
LVL 6

Assisted Solution

by:tmwsiy
tmwsiy earned 83 total points
ID: 36545493
This is just an environment variable like any other.

For some reason this variable was previously defined somewhere and now is not getting set in shell it is being run in.

You have to remember that environment variables set within a script will go away when the cmd.exe that it was invoked with dies (IE when control is returned to you and the shell waits for another command).

Make sure you have echo on and place something like this in the batch script with the call
echo %SoftwareDir%
call "%SoftwareDir%\Utils\change.bat" "%1" "%2"

IF you know the path that SoftwareDir should be you can either just paste it in with the actual path or set it

ie
SET SoftwareDir=c:\pathtosoftware

hope this helps!

0
 
LVL 33

Accepted Solution

by:
knightEknight earned 84 total points
ID: 36545509
It was probably set on the previous system, but not on this one.  Two options - you can set this variable at the system level so it is available to the entire system, or you can just set it in the batch near the top:

@echo off
setlocal
set SoftwareDir=D:\Software

:the rest of your script here and below...
0
 
LVL 33

Expert Comment

by:knightEknight
ID: 36545528
To set this at the system level, right-click on My Computer / Properties / Advanced system settings.  Then click on the Environment Variables button and create a new System variable called SoftwareDir with the appropriate path value.
0
 
LVL 33

Expert Comment

by:knightEknight
ID: 36545533
... after that you will need to close and re-open your command prompt.  Then when you run the SET command you should see it, and it will work in the script.
0
 
LVL 11

Assisted Solution

by:paultomasi
paultomasi earned 83 total points
ID: 36545724
As knightEknight suggests, include the following line in your batch file prior to the CALL statement:

   set SoftwareDir=D:\Software


The following line should then work okay:

   call "%SoftwareDir%\Utils\change.bat" "%1" "%2"


Furthermore, you should consider using %~1 and %~2 instead of just %1 and %2 when including double-quotes. Like this:

   call "%SoftwareDir%\Utils\change.bat" "%~1" "%~2"

The reason for this is that should %1 or %2 already include double-quotes (possibly from the command line, user or a previous CALL), you'll need to strip these away before adding another pair otherwise you may end up with TWO double-quotes at both ends of %1 and %2.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Welcome to my series of short tips on migrations. Whilst based on Microsoft migrations the same principles can be applied to any type of migration. My first tip is around source server preparation. No migration is an easy migration, there is a…
Know what services you can and cannot, should and should not combine on your server.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now