Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to setx without expanding variables?

Tags:

batch-file

I'm trying to set a variable to a value without expanding that variable. E.g.,

setx var1 "%nested_var%\dir"

The result is that var1 gets assigned the expanded string, while I'd rather it didn't contain expanded variables. I've tried a number of approaches without results.

Note that this is done on the command line, not in a batch file.

like image 813
user93200 Avatar asked Oct 28 '25 13:10

user93200


2 Answers

If you are running the command from command prompt, then you can use escape character ^, but you have to remove the quotations, otherwise it will include escape character to variable.

The command would be setx var1 ^%nested_var^%\dir

From the batch you leave the quotation marks and you can escape variable expansion by doubling percentage character.

In the batch use setx var1 %%nested_var%%\dir or setx var1 "%%nested_var%%\dir"

Hope this helps

like image 71
R1cky Avatar answered Oct 30 '25 03:10

R1cky


N.B. If one is using setx command from command prompt, the inner environment variables must not be expanded and environment variable has spaces, then use syntax like this:

setx var1 ^"^%nested_var^%\some dir^"

like image 36
Mosc Avatar answered Oct 30 '25 05:10

Mosc



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!