Windows Batch Files - Interpolate environment variables into executable call

空扰寡人 提交于 2019-12-10 20:32:08

问题


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?


回答1:


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.




回答2:


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...




回答3:


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.



来源:https://stackoverflow.com/questions/8290895/windows-batch-files-interpolate-environment-variables-into-executable-call

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!