[Bat] Use the environment variable name including "%" when operating the registry with the REG ADD command.

Windows

Hello!This time, I will introduce how to use commands that include environment variables when operating the registry.

Environment variables are variables that have batch-specific behavior.
The variable name is enclosed in%, and "%Variable name%It is written as.
When executing a command, the syntax is parsed first, and if there is an environment variable, it is automatically replaced with the value inside it, and then passed to the command.

Therefore, if there is a name that uses the same character string as the environment variable, it will be replaced without permission and cannot be specified.

When such a situation occurs, before replacing it properly%Variable name%I would like to introduce how to execute the command as it is.

Event example

If an environment variable is specified in the name of the registry key, you may encounter an error such as the registry key not being found if you try to read it without thinking.

For example, try to read the FontSize key as a command prompt setting.Then, I think that the command will be as follows.

reg query HKEY_CURRENT_USER \ Console \% SystemRoot% _system32_cmd.exe / v FontSize

However, even if I execute this command, it fails as shown below.

This time I would like to avoid this.

Cause

The cause of this event is that "% SystemRoot%" is the value of that variable at runtime.C: \ Windows \This is because it has been replaced with.

HKEY_CURRENT_USER \ Console \C: \ Windows \_system32_cmd.exe

There is no such registry as above.You have to pass the command as it is.To avoid this, it is necessary to escape (do not replace)%, but it does not work even if you try to overlap it with %% or enclose it with "".

Environment variables are more straightforward, straightforward, and inflexible than in other languages.

Solution

To escape environment variables^"Use the.

"%^If you write "SystemRoot%" after the first%, you can pass it to the command as a character string that properly contains%.

If there is an environment variable in the registry key name

As shown in the above example, if the registry key name contains an environment variable, you can read it with the Reg Query command by writing it like this.

reg query HKEY_CURRENT_USER \ Console \%^SystemRoot% _system32_cmd.exe / v FontSize
Reg Query the registry key containing environment variables

If you want to set an environment variable for the registry key value

Even here, even if you type the following command without thinking about anything, the environment variables will be written in the expanded state.

reg add HKLM \ Software \ test / v test / t REG_SZ / d% SystemRoot%

Even in this case, "^If you use "", you can write correctly including the character string of the environment variable.

reg add HKLM \ Software \ test / v test / t REG_SZ / d% ^ SystemRoot%

Now you can use the REG command freely even in the registry that contains environment variables!

Afterword

This time, I wrote about the command when the registry key contains environment variables.

Of course, there are many cases where you want to execute a command including environment variables in various scenes, not just the registry key.

It is troublesome to output even a simple "echo" with the variable name of the environment variable.
Even if I type "echo% SystemRoot%", the replaced value is still output.

It doesn't matter much if you usually operate only on the console, but it can be troublesome when you want to output the variable name to a text file.

At that time^I hope you remember that adding the will work fine.

I hope it helps you.

Comment

Translate »
I copied the title and URL