5

Apologies if I overlooked this problem in another thread, but I was unable to find it (here or really anywhere on the internet).

In Windows (XP), I have the following global environment variable which I set by right-clicking My Computer, then choosing Properties > Advanced > Environment Variables:

CUSTOM_HOME = c:\some\folder\path

How do I interpolate this into an execution within a batch file? I need to "append" subfolder\program.exe to this path so that I get c:\some\folder\path\subfolder\program.exe in the batch file. I try something like

%CUSTOM_HOME%\subfolder\program.exe

but when I execute the batch file, the output just shows

> \subfolder\program.exe
> The system cannot find the path specified.

What's the correct syntax so the full path to the .exe will be correct?

istrasci
  • 1,331
  • 1
  • 18
  • 40

3 Answers3

6

That is the correct syntax, but you will need to add quotes around the whole thing in case the custom_home path contains spaces.

If it isn't being found, then that means the global env variable is either misspelled or not available. You can test this at the command line with SET CUSTOM_HOME.

competent_tech
  • 44,465
  • 11
  • 90
  • 113
  • Sorry about that :). Sort of. – competent_tech Nov 28 '11 at 02:36
  • I can do `echo %CUSTOM_HOME%` and `set CUSTOM_HOME` from the command line and they both work. But adding quotes doesn't help the batch file. It's like the environment variables aren't being loaded in the script or something. I don't need to reboot or something to make them "take effect", do I? – istrasci Nov 28 '11 at 02:39
  • Are you trying to execute it interactively or through the task scheduler? If it is a service that is performing the execution, they may not have picked up the change to the global env settings, thus necessitating a reboot. – competent_tech Nov 28 '11 at 02:41
  • Yeah, rebooting seemed to work. Isn't there anyway to globally "reload" environment variables when they've been changed without having to reboot? That seems stupid. – istrasci Nov 28 '11 at 02:48
  • Sadly, I have yet to find one. Sometimes, if it is a service that needs the new value, restarting the service will work, but most of the time I just reboot. – competent_tech Nov 28 '11 at 02:49
4

The problem here are the spaces besides the equal sign. In Batch SET command the variable name is the complete string before the equal sign (including spaces) and the variable value is the complete string after the equal sign (including equal signs).

This command:

SET CUSTOM_HOME = c:\some\folder\path

assign to "CUSTOM_HOME " variable the value " c:\some\folder\path". You may test it this way:

ECHO %CUSTOM_HOME %

Just eliminate spaces besides the equal sign...

Aacini
  • 65,180
  • 12
  • 72
  • 108
  • A good comment for others who may view this thread, but that wasn't the problem. I wrote `CUSTOM_HOME = c:\some\folder\path` to mean conceptually equal. I didn't set the value in the script. I set it by right-clicking **My Computer**, then doing **Properties > Advanced > Environment Variables** (or whatever the sequence is; I'm in OSX right now). I'll edit the OP. – istrasci Nov 28 '11 at 15:55
0

Your syntax is correct. I think you have opened Command Prompt and set the environmental variables. Now you might be executing batch file on the same Command Prompt.

Due to that it may not be working.

Try by closing the existing Command Prompt and Run it.

Siva Charan
  • 17,940
  • 9
  • 60
  • 95