0

I wanna write a bat file to set system variables on any windows system. My software which I made needs to set a path instead of asking the user to process the method I heard we can do this task using bat files so I tried multiple ways to set a path nut most of them are duplicates of all existing paths instead of adding new ones and some times it's removing all paths and keeping only the new path

setx Path "%Path%;c:\ffmpeg"

which is duplicating all the existing path variable

I did read multiple Stackoverflow queries on this but none helped if ur testing on your system to check please save/make a copy of all ur paths first
Thank you Eswar

  • 1
    Since you said you want to set the system variables, I highly advise you use the `/M` option. – Squashman Jan 11 '21 at 03:38
  • 2
    Note that `setx` changes the variable *for future sessions*, but not for the current session. – Stephan Jan 11 '21 at 07:11
  • 3
    Okay once again, it is a __NO GO - NEVER EVER__ to use `%PATH%` on modification of persistent stored __system__ or __user__ environment variable `PATH` as done by you to modify the __user__ `PATH` with the used command line. If that command line was really executed by you already on your machine, you have already corrupted the __user__ environment variable `PATH`. I recommend to read my answer on [System cannot find path even though it exists in paths](https://stackoverflow.com/a/57442052/3074564) with instructions how to repair the `PATH` configuration after corruption. – Mofi Jan 11 '21 at 07:34
  • 1
    I recommend next to read [What is the reason for "X is not recognized as an internal or external command, operable program or batch file"?](https://stackoverflow.com/a/41461002/3074564) It explains very detailed the `PATH` management and usage by Windows and Windows command processor. Then read [Why are other folder paths also added to system PATH with SetX and not only the specified folder path?](https://stackoverflow.com/a/25919222/3074564) and [How to search and replace a string in environment variable PATH?](https://stackoverflow.com/a/24650324/3074564) – Mofi Jan 11 '21 at 07:37
  • 1
    See also [Adding the current directory to Windows path permanently](https://stackoverflow.com/a/47080452/3074564) and last but not least [How can I use a .bat file to remove specific tokens from the PATH environment variable?](https://stackoverflow.com/a/38664286/3074564) for the opposite to remove a folder path from __user__ or __system__ environment variable path. If a batch file adds a folder path to __user__ or __system__ environment variable path, there should be one more batch file which removes the folder path from the environment variable. – Mofi Jan 11 '21 at 07:40
  • But why does your software require a folder path added to __system__ or __user__ `PATH` at all? The installer of a software should do that only if the software is often used by the users from within a command prompt window and so the user should not need to enter the file name of the executable or script of the software with full path. The software itself (executable or script) should not require that any folder path is added to __user__ or __system__ `PATH`. A software which requires that is really poor coded. If an executable or script references other files installed together with the main – Mofi Jan 11 '21 at 07:45
  • program file, the executable or script can reference all these files easily by determining the path of the folder containing the executable or script file and concatenate this path with the file names to reference the files installed with the software with full path on users´ machine. Every, really every programming and scripting language has a function or method to get the path in which the executable or script file is stored which is currently executed. In a batch file can be used `%~dp0` to reference the path of currently executed batch file always ending with a backslash. – Mofi Jan 11 '21 at 07:49
  • The installer can register the application also as recommended by Microsoft, see [Application Registration](https://docs.microsoft.com/en-us/windows/win32/shell/app-registration) and [Where is “START” searching for executables?](https://stackoverflow.com/a/27386403/3074564) An installer can create also a configuration file in a subfolder of `%APPDATA%` or `%LOCALAPPDATA%`, see [Windows Environment Variables](https://en.wikipedia.org/wiki/Environment_variable#Windows) which can be used by the installed executables and scripts. Or a folder path is added somewhere in registry by the installer – Mofi Jan 11 '21 at 07:53
  • with a string value under a specific key which is used by the software. All these methods to work with installation path of a software do not affect other applications installed on a machine as done with a modification of persistent stored __system__ or __user__ environment variable `PATH`. So if you are not writing an installer for `ffmpeg.exe`, your software should not add the folder path of this executable to __system__ or __user__ `PATH`. – Mofi Jan 11 '21 at 07:56
  • the proper way is to change PATH env variable (user or system) in registry. Not with setx _ever_ – Jean-François Fabre Jan 11 '21 at 20:39

2 Answers2

0

i wouldn't recommend you to mess with system variables but if you want to store path for your program here's a script that will do the thing but before doing so i would encourage you to make make a backup for files and all the paths stored in system variable %Path% and if something happen read this but anyway here is a script that worked for me:

@echo off
echo validating Path
ping localhost -n 2 >nul
set "store_path=Path to your program "
rem /M to set the variable in system wide environment
setx Path "%path%;%store_path%"
setx Path "%path%;%store_path%" /M

simply i am storing the new path with the old one.

Justaus3r
  • 395
  • 5
  • 15
  • I don't what's wrong but even using ur code it just creates duplicates/multiple copies and doesn't work always –  Jan 11 '21 at 10:03
  • @EswarT Try using it without the semicolons in last 2 commands,i think you are using windows 10 that's why it ain't working.i checked it on windows 7 – Justaus3r Jan 11 '21 at 10:37
  • yes I'm using windows 10 even trying that didn't work –  Jan 11 '21 at 13:59
  • @Justaus3r, did you actually see any of the comments by Mofi. It was made clear that this is not the way to perform the task, as it would still duplicate some entries, _(unless the user had no defined %Path% variable)_. They even provided several links explaining the reason why! – Compo Jan 11 '21 at 16:05
  • @Compo For your kind information it didn't duplicate any of my entries.that's why i put the script and also specified the OS version it worked on and also warned before using it and i know it isn't the ideal way to store path thats why i warned about it!.... – Justaus3r Jan 11 '21 at 16:17
  • Please explain to me how adding everything from `%Path%`, which is everything under the System Environment variable `%Path%`, and everything under the User Environment variable `%Path%`, to the User Environment variable `%Path%`, doesn't propagate all of the content of one into the other! Then to make it worse, you're adding everything under the System and User Environment variables `%Path%`, back to the System Environment variable, `%Path%`. In my estimation this should mean that your resulting System Environment would then contain, its initial values, three times and your user values twice. – Compo Jan 11 '21 at 18:17
  • There is noting wrong with using `setx` with the other normal variables, but the `%Path%` variable is not normal, it is different, in that its content is that of the System Environment's `%Path%` variable with the content of the User Environment's `%Path%` variable appended to it. This fact is not limited to Windows 10, it is true of Windows 7 too, so I cannot understand how your tests have not highlighted it. If this isn't happening immediately, I'm fairly sure that after a reboot of the system, it will show those duplicates. – Compo Jan 11 '21 at 18:24
  • @Compo it creates duplicates if you run the script again so for my personal tool i worked it out with a long hard fix to refrain from creating duplicates if some conditions were met,it was a long fix but it worked out.thats why i warned **not** to mess with it. – Justaus3r Jan 11 '21 at 19:36
  • "Compo it creates duplicates" is correct. As the intention of the question was to not create duplicates, or more technically to do it properly, your answer will never do that! – Compo Jan 11 '21 at 21:02
  • __ATTENTION__ to all readers: This code is corrupting __user__ and __system__ environment variable `PATH` and should be never used in a batch file. See for example [Why are other folder paths also added to system PATH with SetX and not only the specified folder path?](https://stackoverflow.com/a/25919222/3074564) It explains the reasons why this code corrupts __user__ and __system__ `PATH` and contains an example for a much better solution. – Mofi Jan 12 '21 at 07:20
-2

I don't suggest you mess with system variables but if you want to store path from cmd use

setx Path "%Path%;C:\your\path\here\" C:\ffmpeg\bin

I tested on windows 10 it worked for me
Hopefully it works for u too!!

Education 4Fun
  • 176
  • 3
  • 16
  • But it too creates duplicates but at least it adds the required thing thank you –  Jan 11 '21 at 17:35
  • PATH in windows is the system+user path. Doing this can make the path so long no path work... – Jean-François Fabre Jan 11 '21 at 20:36
  • 1
    __ATTENTION__ to all readers: This code is corrupting __user__ environment variable `PATH` and should be never used on a command line or in a batch file. See for example [Why are other folder paths also added to system PATH with SetX and not only the specified folder path?](https://stackoverflow.com/a/25919222/3074564) It explains the reasons why this command line corrupts __user__ `PATH` and contains an example for a much better solution. – Mofi Jan 12 '21 at 07:18