Adding a directory to the system path variable through registry - windows

I am trying to add a directory to the PATH variable in windows. This is what I am entering the command line. (Or a batch file)
#echo off
set value=%path%;%ProgramFiles%\AtomScript\
reg add "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Sessions Manager\Environment" /v Path /t REG_EXPAND_SZ /d %value% /f
And it comes up with this message
ERROR: Invalid syntax.
Type "REG ADD /?" for usage.
What am I doing wrong?

You probably have to quote %value% (with double-quotes) because its expansion has embedded blanks for C:\Program Files, etc.
That would be
reg add "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Sessions Manager\Environment" /v Path /t REG_EXPAND_SZ /d "%value%" /f
You can see what the actual expansions are by turning echo on in your script:
#echo on

Maybe you solved it already, but as far as I can see you also might have a misspelling in "...\Sessions Manager...". At least on my system it's "Session" without the extra s.

Related

REG ADD with spaces within FORFILES command

I am trying to automate setting the App Compatibility flags for numerous EXE files we use. I can get the forfiles command to pull up all of the EXE full paths with no problem. The issue is passing that information to the REG ADD command with the space. What am I missing here? I've tried several sets of double quotes as well as single quotes but nothing is working right. What's the correct syntax for this command to work? If it can be done as shown below that's great. If it has to be done a different way, that's ok too.
Any help would be greatly appreciated.
SET Key="HKLM\SOFTWARE\Microsoft\Window NT\CurrentVersion\AppCompatFlags\Layers"
SET Command="'REG ADD' "%KEY%" /v #PATH /t "REG_SZ" /d "RUNASADMIN""
forfiles /p D:\<DIR> /S /m *.exe -c "cmd %Command%"
Try with
SET "Key=HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers"
for /r "d:\<dir>" %%a in ("*.exe") do (
reg add "%Key%" /t REG_SZ /v "%%~fa" /d RUNASADMIN
)

Reg.exe Error: Too many command-line parameters

I have a command in my bat file that appends the Path environmental variable:
reg.exe ADD "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v Path /t REG_EXPAND_SZ /d %PATH%;"C:\Program Files\Java\jdk1.7.0_51\bin"
However, I get an error: "Error: Too many command-line parameters"
How can I successfully append without using the GUI?
the code
reg.exe ADD "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v Path /t REG_EXPAND_SZ /d %PATH%;"C:\Program Files\Java\jdk1.7.0_51\bin"
Should read like this
reg.exe ADD "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v Path /t REG_EXPAND_SZ /d "%PATH%";"C:\Program Files\Java\jdk1.7.0_51\bin"
If the path was all one like like this C:\windows\system32 then it would work with out editing. But if it has spaces like C:\Users\user\AppData\Roaming\Intel Corporation\ then as it has a space then cmd read's it as C:\Users\user\AppData\Roaming\Intel so you will get your error.

How to get the full path of a file properly?

I've to add a script to windows registry via command line as follow:
Reg add HKCU\Software\...\.\.. /v backUp /t REG_SZ /d "%~dp0\backUp.bat" /f
The script is called "setup.bat" it contains only the above line. BTW backUp.bat is running at the same dir as the "setup.bat" so for that i'm using %~dp0 to get the full path of the script dir. I surrounded %~dp0 by ", because some path contains spaces.. So, normally when i run i set properly the full path of the backUp.bat, whenever the script is running, but the problem is that i get a double slash before backUp.bat in my registry, look:
C:\Users\marwen\Desktop\bin\\backUp.bat
If i change the command as this:
Reg add HKCU\Software\...\.\.. /v backUp /t REG_SZ /d "%~dp0"\backUp.bat /f
Same here, the result is faulty :
C:\Users\marwen\Desktop\bin"\backUp.bat
How to solve this ?
untested but Reg add HKCU\Software\...\.\.. /v backUp /t REG_SZ /d "%~dp0backUp.bat" /f without the slash should do it.

Adding a registry key in windows with quotes needed in the data using a batch script

Little Willis here. I am trying to using a batch script to edit an existing registry key that is used when double clicking a .jar file. The issue is that the data that I'm trying to enter contains quotes but I also need quotes for it to be considered a string.
Example:
reg add "HKEY_LOCAL_MACHINE\Software\Classes\jarfile\shell\open\command" /v "" /t REG_EXPAND_SZ /d "C:\Program Files\Java\jre7\bin\javaw.exe" -jar "%1" %* /f
When I run that in a batch script the cmd window prints out "Error: Too many command line parameters"
So to make this simple. I want to add a registry key with "C:\Program Files\Java\jre7\bin\javaw.exe" -jar "%1" %* as the data including the quotations and the %1 and %* exactly as they are not converted to any actual statement or string.
EDIT:
The registry is normally added using using this command line string:
ftype jarfile="C:\Program Files\Java\jre7\bin\javaw.exe" -jar "%1" %*
it works fine in the command line, but just as the code given below when I used this in a batch script the "%1" and %* don't appear.
Use backslashes to escape the inner quotes, i.e.:
reg add "HKEY_LOCAL_MACHINE\Software\Classes\jarfile\shell\open\command" /v "" /t REG_EXPAND_SZ /d "\"C:\Program Files\Java\jre7\bin\javaw.exe\" -jar \"%1\" %*" /f
Percent literals must be doubled in a batch file: \"%%1\" %%*"
as an addition to dbenham's answer, you should use backslaches and quotes for location path !! (i mean, you should use "\"C:\Program Files..... instead of "C:\Program Files..... )
so this is the final answer for typical percent sign & adding problem:
reg add "HKEY_LOCAL_MACHINE\Software\Classes\jarfile\shell\open\command" /v "" /t REG_EXPAND_SZ /d "\"C:\Program Files\Java\jre7\bin\javaw.exe\" -jar \"%%1\"" /f
thanks dbenham!
Another alternative is to use single quotes, some applications can read it properly, example:
reg add "HKEY_LOCAL_MACHINE\Software\Classes\jarfile\shell\open\command" /v "" /t REG_EXPAND_SZ /d "'C:\Program Files\Java\jre7\bin\javaw.exe\' -jar '%1' %*" /f

Writing %~DP0 to Registry using REG ADD

I've resolved the question I had about inserting %DATE% into a REG_SZ registry value (see link), but now I'm running into a slightly different problem trying to insert %~DP0 (long source path) into a registry value using REG ADD within a .BAT script. It won't do it, and I'm sure it's because I'm doing something wrong.
reg add "hklm\software\acme" /v "TestValue" /d "%~dp0" /t REG_SZ /f
I've also tried setting the value to a variable first, but that doesn't work either. What happens is that it inserts the expanded path without the preceeding double-quote, but with a trailing double-quote, and then bombs with an error about REG /? syntax, etc.
SET VX=%~DP0
reg add "hklm\software\acme" /v "TestValue" /d "%VX%" /t REG_SZ /f
Anyone see what I'm doing wrong?
The path %~dp0 ends in the directory separator character '\' (e.g. 'c:\temp\') which is being interpreted as an escape for the following double-quote character and so the parser is not seeing the closing double-quote. What you need to do is escape the trailing \ character with another one:
reg add "hklm\software\acme" /v "TestValue" /d "%~dp0\" /t REG_SZ /f

Resources