Subscribe to Windows IT Pro
May 22, 2003 12:00 AM

How can I pass a percent sign (%) value to reg.exe?

Windows IT Pro
InstantDoc ID #39047
Rating: (5)

A. Just as you can use a slash (\) as an escape character to pass a double quote ("), you can use a carat (^) as an escape character to pass a percent sign. For example,

G:\>reg add HKLM\Software\sav /v test /t REG_SZ /d "\"%userprofile^%\""

maintains the user-profile value's percent sign. Without the carat, the reg.exe tool will evaluate the value between the percent signs; with the carat, reg.exe will leave the value unchanged.

Related Content:

ARTICLE TOOLS

Comments
  • Moore
    2 years ago
    Aug 04, 2010

    There are occasions when you want to add a value but you don't want to add the enclosing quotes. However, when the value needs its own quotes owing to a space character in the string you'll find that it doesn't parse the caret properly. I discovered that it works if you store the string as a variable first, e.g.:
    set MyDocsRedirTemp="%HOMEDRIVE^%\\My Documents"
    reg add "HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\User Shell Folders" /v Personal /t REG_EXPAND_SZ /d %MYDOCSREDIRTEMP% /f
    set MyDocsRedirTemp=

  • Jarom
    5 years ago
    Jun 05, 2007

    One problem I have had is to have a variable in a .cmd file include a percent sign literal in a variable, and then to use that variable correctly. It seems to make it work correctly, I needed to use more than two percent signs.

    Check this out, executed from a command file:

    rem Let's see how a percent sign is interpreted
    echo 1
    echo %
    set x=%
    echo %x%
    echo 2
    echo %%
    set x=%%
    echo %x%
    echo 3
    echo %%%
    set x=%%%
    echo %x%
    echo 4
    echo %%%%
    set x=%%%%
    echo %x%
    echo 5
    echo %%%%%
    set x=%%%%%
    echo %x%
    echo 6
    echo %%%%%%
    set x=%%%%%%
    echo %x%
    echo 7
    echo %%%%%%%
    set x=%%%%%%%
    echo %x%
    echo 8
    echo %%%%%%%%
    set x=%%%%%%%%
    echo %x%
    echo 9
    echo %%%%%%%%%
    set x=%%%%%%%%%
    echo %x%
    echo 10
    echo %%%%%%%%%%
    set x=%%%%%%%%%%
    echo %x%

    rem Let's see what happens if we try to put a percent
    rem literal in a generated file
    set file=x.cmd
    echo echo off > %file%
    echo echo 1 >>%file%
    echo echo % >>%file%
    echo set x=% >>%file%
    echo echo %%x%% >>%file%
    echo echo 2 >>%file%
    echo echo %% >>%file%
    echo set x=%% >>%file%
    echo echo %%x%% >>%file%
    echo echo 3 >>%file%
    echo echo %%% >>%file%
    echo set x=%%% >>%file%
    echo echo %%x%% >>%file%
    echo echo 4 >>%file%
    echo echo %%%% >>%file%
    echo set x=%%%% >>%file%
    echo echo %%x%% >>%file%
    echo echo 5 >>%file%
    echo echo %%%%% >>%file%
    echo set x=%%%%% >>%file%
    echo echo %%x%% >>%file%
    echo echo 6 >>%file%
    echo echo %%%%%% >>%file%
    echo set x=%%%%%% >>%file%
    echo echo %%x%% >>%file%
    echo echo 7 >>%file%
    echo echo %%%%%%% >>%file%
    echo set x=%%%%%%% >>%file%
    echo echo %%x%% >>%file%
    echo echo 8 >>%file%
    echo echo %%%%%%%% >>%file%
    echo set x=%%%%%%%% >>%file%
    echo echo %%x%% >>%file%
    echo echo 9 >>%file%
    echo echo %%%%%%%%% >>%file%
    echo set x=%%%%%%%%% >>%file%
    echo echo %%x%% >>%file%
    echo echo 10 >>%file%
    echo echo %%%%%%%%%% >>%file%
    echo set x=%%%%%%%%%% >>%file%
    echo echo %%x%% >>%file%
    echo pause >>%file%
    type x.cmd
    x.cmd

  • regaddpercent
    6 years ago
    Dec 14, 2006

    Was just having this problem and here's what:
    If the example line is put in the command prompt it will work.
    But, if that line is put in a .cmd or .bat file it will not!
    Here's a .cmd/.bat compatible line (double up on percent signs):

    reg add HKLM\\Software\\sav /v test /t REG_SZ /d "\\"%%userprofile%%\\""

  • Chris
    7 years ago
    Jun 29, 2005

    This didn't seem to work for me...now I get just the caret...here's the syntax I am using:
    REG ADD HKLM\\SOFTWARE\\Classes\\Property.Script\\shell\\Edit\\command /ve /t REG_EXPAND_SZ /d "\\"%ProgramFiles%\\Desktop Publishing\\Notepad++\\notepad++.exe\\" "\\^%1\\"" /f
    and...
    REG ADD HKLM\\SOFTWARE\\Classes\\Property.Script\\shell\\Edit\\command /ve /t REG_EXPAND_SZ /d "\\"%ProgramFiles%\\Desktop Publishing\\Notepad++\\notepad++.exe\\" ^%1" /f

You must log on before posting a comment.

Are you a new visitor? Register Here

advertisement

advertisement

Windows is a trademark of the Microsoft group of companies. Windows IT Pro is used by Penton Media Inc. under license from owner.