I want the user to input the path of the bin directory and then store that path to system variable path
i tried this
set /p path = Please specify the path to the bin folder
echo Path is set to = %path%
does not seems to work
Basically i want the user to enter the path and that entered path gets permanently stored in the system variable 'path' hope i am more clear now
You may get help from the answer of this question: Setting a system environment variable from a Windows batch file?
It says you should use setx
Remove the spaces. The set command doesn't trim, so the input is actually stored in a variable named path<space>.
This is something you should be able to spot easily if you call set without parameters. You will see an extra variable that has this extra space.
BUT NOTE: path is already an existing variable, containing the search path for the command session. You'd better use a different name altogether if you don't want to overwrite that variable.
Related
We can use setx as discussed here.
setx PATH "%PATH%;C:\Something\bin"
But this command can just make changed to user PATH variable not the system one.
How can we make a similar system wide command?
Type setx /? to get basic command help. You'll easily discover:
/M Specifies that the variable should be set in
the system wide (HKEY_LOCAL_MACHINE)
environment. The default is to set the
variable under the HKEY_CURRENT_USER
environment.
You need to run this from an elevated command prompt. Right-click the cmd shortcut and select Run as Administrator.
E.g.
setx /M PATH "%PATH%;C:\Something\bin"
Caution:
We may destroy the current system's PATH variable. Make sure you backup its value before you modify it.
From powershell
setx /M PATH "$($env:path);c:\program files\mynewprogram"
Solution when dealing with a >1024 char path:
None of the other answers worked in my case, but using pathed did the trick. You can append to path as simply as this:
pathed /append C:\Path\To\Be\Added /machine
You can check if the edit happened correctly by running
pathed
PS: if you want to change the user's path instead use:
pathed /append C:\Path\To\Be\Added /user
and pathed /user to check if it went through correctly.
PPS: In order to be able to run pathed from terminal, you need to put the exe in a directory already on your path (or add a new directory to path, but then you you might need to open a new instance of cmd.exe in order for the new path to be recognized)
One problem with %PATH%, is it includes the user's path. If you don't mind Powershell, you can run the following
$p = [Environment]::GetEnvironmentVariable("PATH", [EnvironmentVariableTarget]::Machine);
[Environment]::SetEnvironmentVariable("PATH", $p + ";C:\MyPath", [EnvironmentVariableTarget]::Machine);
If you want to add some location to the PATH environment variable on user level, use the following on the command line:
setx PATH ^%PATH^%;"C:\Program Files\Something\bin"
Why the strange syntax?
First, you do not want to expand the system PATH variable but keep it as a symbol, otherwise you will not participate in future additions to the system PATH variable. Therefore, you have to quote the % characters with ^.
If you use this in a command script, you have to use double %% instead of ^%.
The " encloses a string that contains spaces. If you do not have spaces, you can omit the quotes.
The added string has to follow directly without space so the whole thing forms a single argument to the setx command.
Please refer to Adding a directory to the PATH environment variable in Windows
append_user_path.cmd
append_system_path.cmd
- both work just fine
If I add C:\Program Files (x86)\WinMerge to the User PATH variable(by right click on computer -> advanced system settings -> environmental variables), once I open a new cmd shell WinmergeU.exe is not recognized. If I add that path to the System PATH variable, WinmergeU.exe is correctly recognized instead. I though there was no difference between User and System, beside the fact that if I set it on System all the users will see it, while the User PATH is local. Am I doing something wrong?
EDIT 1:
In the follow you can see first the case in which C:\Program Files (x86)\WinMerge is added to the System PATH variable (but not to the User), then when it is added to the User PATH variable (but not to the System). In the first case Winmerge window launch correctly (not shown) and as you can see the path is shown by the echo %PATH% command. In the second case it does not launch and the path it is not shown by echo %PATH%. (note that I clearly confirmed with OK and closed the environmental variable windows before taking these screenshots, and I opened a new cmd right after changing PATH and pressing ok). This issue might be related to my question here (Does echo %PATH% expand to only the system or also the user variables?) but since it might not be I posted two different question.
You must be getting something wrong, or have environmental problems with your machine. Adding a user PATH environment variable does result in it being merged into the environment of a new process.
Update: Perhaps this comment from the MSDN topic on environment variables might be pertinent:
Found out that on Windows Server 2003, once the system PATH passes 1920 characters, the user PATH environment variable is no longer merged with it to set the process PATH environment variable, even though the full system PATH (even if larger) will be included in the process PATH variable.
On Windows 7, also make sure that the system path does not end with a backslash. If it does, the USER PATH is appended to the system path as per usual, but after a line break, which breaks things. In the latter case, the simple command "path" and "echo %PATH% " will print 2 different outputs.
For 16.6, I've confirmed this issue to be a path length issue as well. At a cmd prompt, typing 'set', you can see all of the env. variables. The user path cadence variables and others weren't included. I saved the original complete path text, then I went through and trimmed specific (system) path elements that were deemed unnecessary. after this, in a new cmd session, typing 'set' now shows the user path elements tacked on to the end of the system path elements, because they now fit.
According to this post it is not possible to include %ProgramFiles% in the path varaible, because windows environment variables get resolved in alphabetical order.
However, I need to be able to get the correct location of program files in PATH from a batch file (it can be expanded, I just can't hardcode the drive letter into the batch file because it needs to be portable). How do I do this?
EDIT: In response to some of the comments below, here is some additional information:
-The change to PATH needs to be permanent.
-SETX appears to just stick the literal %programfiles% in the path variable.
You can't get %ProgramFiles% expanded when hand editing the PATH from the Control Panel System applet at system level.
But you can certainly use %ProgramFiles% in your BAT file. Just try
PATH %ProgramFiles%;%PATH%
If you want a permanent change in your PATH from a BAT file, then that's another question, already answered here Permanently altering a user's %PATH% environment variable via batch or Python
In environment variables I have a PATH variable for both user variables and system variables.
In a batch script, in order for me to append the user PATH variable with a new given path, I need to select the current value. Unfortunately %PATH% returns a combination of the user variable and the system variable.
Of course I only want to add a new custom path value to the user variable. There is no point in enhancing it with the system path as well. That's why I have 2 variables.
Thanks in advance.
Edit: Found in the documentation the following statement:
The %PATH% variable is set as both a system and user variable, the 2 values are combined to give the PATH for the currently logged in user....
Example:
User variables:
PATH
value: c:\dev
System variables
PATH
value: c:\Program Files
What I want to do, is to add to the user variable the value: c:\tmp, so that in the end PATH will have the value: c:\dev;c:\tmp
But, if open a cmd window:
echo %PATH%
c:\Program Files;c:\dev
so the setx will do the following
setx path "%path%;c:\tmp"
open new cmd
echo %PATH%
c:\Program Files;c:\dev;c:\tmp
And that is wrong, because I only needed c:\dev;c:\tmp
I hope I was more clear this time.
How are you modifying the variables?
There is only one environment variable PATH, so you can go ahead and change it. These changes are transient (and local to your process and its children).
There are two (actually more) persistent places in Registry from which the environment variables are initialized when a process is created. You can modify them using reg utility. There is no ambiguity since they are separate:
HKEY_CURRENT_USER\Environment
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment
You may have to re-login for changes in Registry to take effect (I don't remember whether there's a programmatic way to notify explorer that these settings have changed). Also note that by default child processes inherit the environment of their parent (unless the parent takes special measures to do otherwise), so e.g. if you launch a cmd window and later modify the environment via system settings dialog, applications started from that cmd won't see the changes.
[UPD] You can get the value of user-specific environment variable from registry using reg utility:
reg query HKCU\Environment /v PATH
Though you'll have to filter its output for the actual value, as it spits out some useless text. Here an example incantation:
for /f "usebackq tokens=2,*" %A in (`reg query HKCU\Environment /v PATH`) do set value=%B
It will store the result in the environment variable value. Remember to double %'s when using it in a batch file.
If I understand your question, you have 2 %PATH% variables. One system one, and one user one, (presumably you created the latter yourself).
I have tried this and it seems to work for user environment variables
setx /s computername PATH %PATH%;newpathvalue
When I tested this I actually substituted PATH for a new var to make sure it works, but it may be best making a copy of your existing vars before doing this, just in case.
It will tag onto the end of the existing user environment variable PATH with the new value you specify, separating it from the others with a semi-colon ;.
Hope this helps
I created a really small batch file that I want to be able to call anywhere in my command-line environment by name:
So if my batch file is hi.bat, I want to type hi in the command window and have it run. Where can i store the batch file so this happens?
Thanks
Store it somewhere in the PATH or have PATH include its directory. The PATH variable contains a list of directories that are searched when you enter a command. Depending on your version of windows, you can change environment variables from My Computer -> Properties or Advanced System Settings.
If you have the permissions, just modify the PATH environment variable to include the folder which hi.bat is in. You can then place hi.bat anywhere you want.