I want to create a batch file to add the current directory to my System variable PATH, (not my User variable).
When I use:
SETX /M PATH "%CD%;%PATH%"
it does the needed thing. However, I get an error message:
data being saved is truncated to 1024 characters.
When I check the System variable using the GUI, I saw that User Path is getting added to the System Path. As a result, the System Path has duplicated entries.
I tried assigning the %PATH% variable to a temporary variable and echoing but I saw the duplications there as well.
I saw in some stack answer that the %PATH% variable we use in the batch file is actually a concatenation of both User Path and System Path.
At the Command Prompt, I tried:
REG QUERY "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v PATH"
but I don't have idea much about whether we can use it to take that PATH value and assign to another variable.
So I need to find a way to assign ONLY the SYSTEM PATH to a temporary variable, let's say SYS_PATHS. How can I do that?
Is there a better way to overcome this scenario?
I found the answer to the question I asked in a link provided by #Mofi. This is how you can take the system path only, and append a directory to it.
set "env=HKLM\System\CurrentControlSet\Control\Session Manager\Environment"
for /f "tokens=2*" %%I in (
'reg query "%env%" /v Path ^| findstr /i "\<Path\>"'
) do setx /m PATH "%%J;%CD%"
Related
I'm trying to set up a script that will install python automatically, and I'm stuck on setting up the user path. I have only a vague clue about what I'm doing here so please excuse me if I'm using any terms incorrectly.
I'm trying to set the environment variables using setx path "%PATH%;%LOCALAPPDATA%\Programs\Python\Python310"\ but I've run into several issues.
I finally have this command not failing because of 'multiple default arguments' or something but now when trying to set PATH, I get duplicate entries.
If originally %PATH% gave me '\path1;\path2', and I run setx path "%PATH%;\path3", %PATH% outputs '\path1;\path2;\path1;\path2;\path3',
when I expected to have '\path1;\path2;\path3'
As per what I've been reading from other answers, I think %PATH% gives you the combined SYSTEM and USER paths, but setx path modifies the USER path only. So everytime I run setx path I'm adding the system variables again.
I just want to add my python.exe location to the user path variable in a .bat script without this duplicating issue. Does anyone have a working solution?
#ECHO OFF
SETLOCAL
SET "python=%%USERPROFILE%%\AppData\Local\Microsoft\WindowsApps"
ECHO %path%>u:\pp.txt
FOR /f "tokens=1,2,*" %%u IN ('reg query HKCU\Environment') DO IF "%%u"=="Path" (
FIND /v "%python%" "u:\pp.txt" >NUL
IF NOT ERRORLEVEL 1 (
ECHO CHANGE path
ECHO SETX PATH "%%w;%python%"
)
)
DEL u:\pp.txt
GOTO :EOF
I used a path that I have installed as python. Note that the % need to be doubled.
Write the current path to a tempfile (u:\pp.txt is simply on a RAMDRIVE for me)
Read the environment data from the registry, tokenise and select for the first item in %%u being Path. Its value will be in %%w.
See whether the "python path" is already in the path; if not, errorlevel will be 1 so execute the setx.
I merely echoed the setx as I'm not going to change the registry. If the command echoed appears correct, remove the echo to actually execute the setx.
It may be an idea to also set the path in the current environment, as setx changes the variable's value for future instances, not for the current one.
===== Revision ==== in the light of comments:
#ECHO Off
SETLOCAL
SET "python=%%USERPROFILE%%\AppData\Local\Microsoft\WindowsApps"
ECHO %path%>u:\pp.txt
FOR /f "tokens=1,2,*" %%u IN ('reg query HKCU\Environment') DO IF /i "%%u"=="Path" (
FIND /v "%python%" "u:\pp.txt" >NUL
IF NOT ERRORLEVEL 1 (
SETLOCAL ENABLEDELAYEDEXPANSION
SET "xpath=%%w;%python%"
SET "xpath=!xpath:~159!"
IF DEFINED xpath (ECHO PATH too long) ELSE (
ECHO CHANGE path
ECHO SETX %%u "%%w;%python%"
)
ENDLOCAL
)
)
DEL u:\pp*.txt
GOTO :EOF
Fixes:
Comparison in for ...%%u made case-insensitive.
Length of resultant user-path variable checked. I used a value of 159 for testing, 1022 for real-world.
variable name being setx'd will be identical to that retrieved from the registry. (For me, it's Path (W11 22H2) - My editor helpfully changes any batch keyword followed by a space to upper-case)
I want to make a script so i could use Win+R, "venv (some path)" and add it in path env variable.
Problem is that i'm not really a batch coder and idk language.
I wrote sth that "should look like working code" but it's not.
Could you help me to make this script working?
echo %venvPath%
#set PATH=%PATH%;%venvPath
If you simply need the variable to be updated for the duration of a specific cmd.exe instance, then you should change your second line to:
#Set "PATH=%PATH%%venvPath%;"
If you need it to be more permamently defined, (for future cmd.exe instances), and as there are both a User, and a System, environment, (and the PATH variable is a joining of both), below is one method you could employ from a Windows batch/command script.
User Environment, (recommended):
#For /F "EOL=H Tokens=2,*" %%G In ('%SystemRoot%\System32\reg.exe Query "HKCU\Environment" /V Path 2^>NUL') Do #%SystemRoot%\System32\setx.exe Path "%%H%venvPath%;"
System Environment, (will need to be Run as administrator):
#For /F "EOL=H Tokens=2,*" %%G In ('%SystemRoot%\System32\reg.exe Query "HKLM\SYSTEM\CurrentControlset\Control\Session Manager\Environment" /V Path 2^>NUL') Do #%SystemRoot%\System32\setx.exe Path "%%H%venvPath%;" /M
If, of course, you need it for both the current cmd.exe instance, and all future instances, you'd need to implement both methods.
Notes
There is a 1024 character string length limitation when using setx.exe. This means that for safety, the expanded content of %%H plus the expanded value of %venvPath%, should not exceed that character length. It may therefore be prudent to save the content of %%H%venvPath% as a variable, then perform a character length check on it, before invoking the setx.exe command.
In addition, it would also be prudent to determine whether the expanded content of %venvPath%, is already listed within the existing %PATH% variable value before you attempt to add it. Whilst duplicating it may not immediately or seriously impact the variable, it would unnecessarily increase the character length, and therefore increase the likelihood of the forementioned limitation in subsequent setx.exe commands, with PATH.
This question already has answers here:
What is the reason for "X is not recognized as an internal or external command, operable program or batch file"?
(4 answers)
Closed 1 year ago.
Whenever I set MingW64 PATH environment variable it is automatically removed.
I also tried using cmd to set the PATH
setx path "%path%;C:\msys64\mingw64\bin"
But even after this command if I try
g++ --version
I still gets this error
'g++' is not recognized as an internal or external command, operable program or batch file.
I am using Windows 10
What should I do?
Thanks in advance
To add a new location to your User Environment Path value string, (or create a new value if one did not already exist), for use in future cmd.exe sessions, but not in the current one, you could use something like this:
#Echo Off
Rem Please note: The Path variable stringtype is REG_EXPAND_SZ.
Rem This means it will auto expand contained variables within the location string.
Rem It is therefore recommended that you include those possible.
Rem But please be aware, when you include them below, double all percent characters.
Rem Enter your new User Path string entry here:
Set "NewString=%%SystemDrive%%\msys64\mingw64\bin"
Set "Reg=%SystemRoot%\System32\reg.exe"
Set "Key=HKCU\Environment"
Set "StX=%SystemRoot%\System32\setx.exe"
%Reg% Query "%Key%" /V Path 1>NUL 2>&1 && (For /F "EOL=H Tokens=2,*" %%G In ('
%Reg% Query %Key% /V Path 2^>NUL') Do %StX% Path "%NewString%;%%H" 1>NUL
) || %Stx% Path "%NewString%;" 1>NUL
For the current cmd.exe session only, you'd do it more like this:
#Echo Off
Path "C:\msys64\mingw64\bin;%Path%"
You could therefore, if you wish, append this as the next line in the upper script, to allow its use in the current instance, and for all future ones:
Path "%NewString%;%Path%"
In my batch file I started to use variables and suddenly the following commands do not work anymore.
Here is the part of my code with the problem
SET "path=MyPath"
REG ADD "HKCU\Software\ETC\ETC" /f /v "MyRegNameA" /t REG_SZ /d "%path%\ETC\"
REG ADD "HKCU\Software\ETC\ETC" /f /v "MyRegNameB" /t REG_SZ /d "%path%"
PAUSE
START "" "%path%\MyProgram.exe"
This code works without the SET... and of course with MyPath instead of %path%. Error Message is:
The command "REG" is either spelled wrong or couldn't be found
I previously found how to use Variables here: stackEx.SetVariables
To my knowledge I am doing it exactly as supposed, and I couldn't find specific help so far.
path is a logical name, but it's not a good name to use as it is assigned by Windows.
path is a semicolon-separated list of the directories that Windows uses to find programs. When you change it, Windows can no longer find reg.exe since reg.exe is not in mypath.
Simply choose another name - don't use path. If you enter set at the prompt, you will see a list of many of the variables that are established by Windows. Simple rule - don't use any of them for user-variables.
I am writing a batch script that, quite reasonably, depends on "C:\WINDOWS\system32" being part of the PATH environment variable. I recently encountered a (developer's) machine that had a really weird path that didn't include system32, and therefore my batch script didn't work.
I looked up ways for my batch script to check the PATH variable and add system32 if it is not there. However, the solution I found used setx which ironically enough ALSO depends on system32 in the PATH variable. Are there any programmatic ways to add system32 to the PATH without it already being there?
Also please let me know if this is such an edge case that it doesn't make sense to make my script robust against it. I'm not expecting any of my typical users to have such a borked PATH variable. Should I bother?
try this:
for /f "delims=" %%a in ("%comspec%") do set "PATH=%PATH%;%%~DPa"
or this:
for /f "delims=" %%a in ("%comspec%") do set "compath=%%~DPa"
set "PATH=%PATH%;%compath:~0,-1%"
#ECHO OFF
SETLOCAL
SET "required=c:\windows\system32"
for %%p in ("%path:;=" "%") do (
FOR %%j IN ("" \) DO (
IF /i %%p=="%required%%%~j" GOTO :nextstep
)
)
SETx PATH "%required%;%path%"
:nextstep
ECHO PATH=%path%
GOTO :EOF
Here's my take on the problem. PATH may contain the required directory with or without a trailing \, and it may or may not have a preceding or trailing ;
I'd suggest that you examine the operation of SETX however. It sets an environment variable for FUTURE cmd sessions, not the CURRENT or EXISTING sessions, AFAIAA....and perhaps not for PATH or some other variables (I tried setting PATH using the SETX in the above batch - future sessions did NOT acquire the new value set, but it appeared to be set according to regedit32 - perhaps it needs a reboot - haven't investigated further at this stage)
Note that the above will need to have the required directory appended to %path% if my observations are borne out...