Whats the difference between #var# and %var% in batch scripting? - windows

While working on my project at work, I came across a startup script for the application which just kind of sets up all the environment variable, paths that sort of thing. I see variables assigned to values like this
set abc=%def%
and also like this:
set xyz=#pqr#
Whats the difference between using #xxxx# against %xxxx% ? I haven't done any batch scripting but looked around could not find an answer to this strangely.

% marks things as variables, which you can set, change, and read, (You can also use ! if delayed expansion is enabled.) while # is just a character, which will print as is and is not used for variables.

Related

There is any character I need to escape in the value of environment variable?

Basically we are thinking about using environment variable to save things like paths, usernames, passwords. Because its a easy way to separate test and production environment and fit well with our RPAs.
So I need to know, there is any character we need to escape to work properly in value when using the command below?
SETX PASSWORD_EXEMPLE "VALUE%2TE*STE~/" /M
Or any special character.

How to hide the resolved value of a variable in ksh?

I am currently stuck with a situation where i need to hide the resolved value of a variable, i.e., the value should not appear when the code runs in the debug mode i.e., ksh -x.
I have seen other threads on similar kind of problem but there, a way has been provided when the value is read from STDIN, with the help of read -s option. But i do not have to read the value from STDIN.
Kindly help me with this.
Thanks,
Amit
When the field to be hidden is unique (something like a strong password), you can make a wrapper that changes the output of the script.
You should only take care for the special characters in the variable such as a slash. Try something like
mycode.sh 2>&1 | sed "s/${myvar}/xxxxxxxx/g"
When you use the variable on a few places only, a good alternative is testing the mode you are running in, and turn off the debug mode before using the variable (and turn on one line later).

String manipulation in Batch Files

I have a batch file question
Set "filename=C:\Documents\Example.doc"
I have a string %FILENAME% and I want to replace the C:\ with C::\, without just redefining it, can anyone help?
I'm not sure what you want to achieve here, and your variant of putting all of the set command in quotes is awkward, albeit valid, but anyway:
SET filename=%filename:C:\=C::\%
Or you just use the %filename:C:\=C::\% expression in places where you want the other value, without actually changing the content of the Filename variable.
For more details see (the output of) SET /?.

Should environment variables that contain a executable-path with spaces also contain the necessary quotes?

When defining an environment variable (on Windows for me, maybe there is a more general guideline)
set MY_TOOL=C:\DevTools\bin\mytool.exe
if the tool is located on a path with spaces
set MY_TOOL=C:\Program Files (x86)\Foobar\bin\mytool.exe
should the environment variable already contain the necessary spaces?
That is, should it read:
set MY_TOOL="C:\Program Files (x86)\Foobar\bin\mytool.exe"
instead of the above version without spaces?
Note: In light of Joeys answer, I really should narrow this question to the examples I gave. That is, environment variables that contain one single (executable / batch) tool to be invoked by a user or by another batch script.
Maybe the spaces should be escaped differently?
I'd say, do it without quotes and use them everywhere you use the variable:
set MY_TOOL=C:\Program Files (x86)\Foobar\bin\mytool.exe
"%MY_TOOL%" -someoption someargument somefile
Especially if you let the user set the value somewhere I guess this is the safest option, since they usually tend not to surround it with quotes rather than do so.
If there are plenty of places where you use the variable you can of course redefine:
set MY_TOOL="%MY_TOOL%"
which makes things more resilient for you. Optionally you could detect whether there are quotes or not and add them if not present to be totally sure.
When your variable represents only a path to a directory and you want to append file names there, then the "no quotes" thing is even more important, otherwise you'd be building paths like
"C:\Program Files (x86)\Foobar\bin"\mytool.exe
or even:
""C:\Program Files (x86)\Foobar\bin"\my tool with spaces.exe"
which I doubt will parse correctly.
The command shell can answer your question: type C:\Pro and hit the tab key.
Autocomplete will leave all spaces as-is and add quotes around the filename. So, this is what is "officially" expected.
(this assumes that autocomplete is turned on, I'm not sure whether the default is on or off, but most people have it on anyway, I guess)

Environment variable for Windows Questions

I'm having a hard time trying to get multiple environment variables configured at the same time. Here are some issues that I would like answered:
If I do have spaces in my PATH environment variable (i.e. C:\Program Files\Java\jdk1.6.0_25), is there any way to keep the same path, but make sure that nothing unexpected happens? I keep hearing that you shouldn't use spaces like in this link here: http://ist.berkeley.edu/as-ag/technology/howto/install-java-sdk-win.html.
Is there a difference in the ordering of the environment variables as listed in the fields?
What will happen if environment variables in the "User variables for user" fields and the "System variable" fields are different? Is it safe to assume that the result will be all the fields combined for that particular variable?
Thanks for any advice!

Resources