Solved

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

Posted on 2011-09-15
5
351 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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

820 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