-1

Hi my requirement is to set a path using a bat file for my application
I tried multiple ways but none worked for me I'm using windows 10.I do know that we can do it manually but I want to make it automate and I also know that this could cause multiple problems Here is a bat file code which I wrote

@echo off
set pathss =%path%
setx path "%pathss%";C:\VET\ffmpeg
TIMEOUT 10

when I directly use

setx path "%path%";C:\VET\ffmpeg

It is creating multiple copies So I thought of storing this path 1st in a variable and use it so duplicates will be avoided but I'm not so good with bat files so I don't know what's wrong but the paths are not storing the paths

The application is coded in python and the GUI is made using pyqt5 The application runs on cmd like the commands and all here is a reference image for observation enter image description here

So its mandatory to add the path else ffmpeg will not recognized.If their is any way to do in building setup like how python can add path as per user requirement refer image fore reference enter image description here

Files in FFmpeg folder refer to image for that info enter image description here

any safe way to add the path in any way will be great to hear
more info about FFmpeg https://ffmpeg.org/
Thanks a lot

Education 4Fun
  • 148
  • 1
  • 13
  • The content of your User Environment Path variable is available in your Windows registry, under `HKEY_CURRENT_USER\Environment`. You could use `reg.exe` to grab that information, within a `for` loop, parsing the `Path` value data. You could then append your new location to that, then write it back using `reg.exe`, or `setx.exe`. I will however tell you that there is no need ever to add anything to the `%Path%` environment variable, and I do not recommend that you do it. – Compo Jan 11 '21 at 18:58
  • 3
    You created a variable named `pathss[space]`. Don't put spaces between the variable name and the equals symbol. – Squashman Jan 11 '21 at 20:04
  • You essentially [answered](https://stackoverflow.com/questions/65660921/setting-system-variables-using-bat-file/65671815#65671815) a similar question two hours ago but it looks like you failed to read any of the comments below the question before you answered it. – Squashman Jan 11 '21 at 20:08
  • 1
    Why do questioners not search before asking? That is the third time within 3 days that I write this. 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`. – Mofi Jan 12 '21 at 06:59
  • 1
    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. Well, wrong is also the space between to `pathss` and `=`. The reason is explained in detail in my answer on [Why is no string output with 'echo %var%' after using 'set var = text' on command line?](https://stackoverflow.com/a/26388460/3074564) – Mofi Jan 12 '21 at 07:00
  • Then 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 `PATH` management of Windows and Windows command processor in full details. For good code to update __system__ or __user__ `PATH` see [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 12 '21 at 07:03
  • 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`. – Mofi Jan 12 '21 at 07:05
  • 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 a 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 12 '21 at 07:06
  • 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 12 '21 at 07:07
  • 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 12 '21 at 07:09
  • 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 does the folder `C:\lol\testing\lol\testing` contain executables or scripts mainly executed by the user from within a command prompt window by typing just the file name? No, then enhance the code of your application so that it is not necessary to add its path to persistent stored `PATH` used by all processes. – Mofi Jan 12 '21 at 07:11
  • @Mofi Sir I did check multiple posts but I'm not getting a clear idea sorry to say. Like I'm using FFmpeg which is a video editing library that runs on the command line so I build a dummy software and when I try on other devices the installer installs all but not the path is not set so FFmpeg doesn't work. So I thought that writing a bat file and running it once the installation is done. Sorry, it, not the best idea but I thought of it and trying. if u have any better idea to establish it please specify it thanks a lot for spending ur time here thank you – Education 4Fun Jan 12 '21 at 08:07
  • An example of python installation setting path To be specific I want to achieve like this I Hope now it more clear what I'm trying to achieve – Education 4Fun Jan 12 '21 at 08:16
  • Is your application suite installed with `ffmpeg.exe` or do you request that the user installs *FFmpeg* first as a prerequisite step before the user can use your application? If your application suite installs also `ffmpeg.exe` into same directory as your program or a subdirectory `FFmpeg` of your program, then it is not necessary that the path to the directory containing `FFmpeg` is added to __user__ or __system__ `PATH`. Your application can find out itself in which directory the executable/script file is stored and use that path on referencing `ffmpeg.exe`. – Mofi Jan 12 '21 at 10:14
  • For example if your application is in real just a batch file which runs `ffmpeg.exe` installed together with the batch file in same directory as the batch file, the batch file should contain `"%~dp0ffmpeg.exe"` instead of just `ffmpeg` to reference the executable with full qualified file name (drive + path + name + extension) with full path being the path of the batch file. And if `ffmpeg.exe` is installed together with the batch file in a subdirectory `FFmpeg`, the batch file should contain `"%~dp0FFmpeg\ffmpeg.exe"`. – Mofi Jan 12 '21 at 10:17
  • Like the FFmpeg are just some files .exe files which are required to run commands so as the software is installing it copies the files to "the location they selected/FFmpeg" in that we can find the files and that need be added to path inorder to the software to work. else it says ffmpeg is not recognized as a command – Education 4Fun Jan 12 '21 at 10:18
  • Every programming compiled with a compiler like C/C++ and every scripting language like Python, Perl, PowerShell, etc. has a method to get from within the executable or script the path of the executable respectively script file. You have nothing posted about your application like in which programming or scripting language it is coded and how exactly `ffmpeg.exe` is run by your application. A well coded application (executable or script) references files installed together with the application using the path of the application file. – Mofi Jan 12 '21 at 10:19

1 Answers1

0
@ECHO OFF
SETLOCAL
:: New directory to be added to path
SET "addtopath=C:\lol\testing\lol\testing"

:: Make a copy of %path% for testing
SET "testpath=%path%"

:: get old path, replace any '\;' with ';' and sandwich between ';'
SET "oldpath=;%testpath:\;=;%;"
:: then remove any double-';'
SET "oldpath=%oldpath:;;=;%"

:: delayedexpansion mode to test for already-exists
SETLOCAL ENABLEDELAYEDEXPANSION 
:: does new addition already exist within path ? End if yes
IF /i "%oldpath%" neq "!oldpath:%addtopath%=!" GOTO End
ENDLOCAL
:: I'm not going to change my path, so I'll just echo the proposed changes
ECHO SET "path=%oldpath:~1%%addtopath%"
ECHO SETX path "%oldpath:~1%%addtopath%"

:End
for %%a in (oldpath addtopath) do set "%%a="

GOTO :EOF

Well, playing with fire, IMHO. That above should "intelligently" modify path, but remember, user-path is merged with system-defined path so I'd not use this method myself.

I've also simply echoed the proposed changes. Remove the echoes if all seems OK; remove the setting of testpath and change the remaining reference to testpath to path. Also remove the first setlocal otherwise the set "path... will have no effect on the current cmd instance.

Magoo
  • 68,705
  • 7
  • 55
  • 76