Search Batch String in Windows PATH

I am writing a batch file in Windows to run scripts after installation, and one of the things that needs to be done is to add the directory to the system path.

The script works and it does something like this:

setx Path "%PATH%;c:\path\to\add" -m

This sets the path correctly, but this script may run several times if the user reinstalls the program.

I would like to search for a string for c:\path\to\add , so I will not constantly add the same path to the system path. This is pretty trivial on Linux with sed , but I don't know what this command is on Windows. I found findstr , but it looks like this only works with files.

Is this possible in Windows without installing additional software?

EDIT:

I am using Inno Setup to create an installation executable.

+4
source share
5 answers

In the case of a risk associated with a decrease, until the expert provides a reliable way to do this,
below deletes a specific path from the environment variable, if one exists, so that it can be added again:

 set str=%path% :: str is the same with path set str=%str:;C:\Path\To\Add=% :: ";c:\path\to\add" is now removed from str setx Path "%str%;c:\path\to\add" -m :: proceed with setting the path 


This carries the risk of deleting the line if it is actually part of the path, for example c:\path\to\add\somefolder . Also, if the path actually ends with \ , or this is the first record, and it does not actually start with ; etc.

Different forms can be called sequentially to get around some of them,

 set str=%str:;C:\Path\To\Add\;=;% set str=%str:;C:\Path\To\Add;=;% set str=%str:;C:\Path\To\Add\=% set str=%str:C:\Path\To\Add\;=% set str=%str:;C:\Path\To\Add=% 

But, AAMOF I'm not sure if this is a reasonable way to do this.

+2
source

This is my piece of code to find the path "cvsnt" in the PATH variable.

 if not "x%PATH:cvsnt=%" == "x%PATH%" goto proceed set PATH=w:\build-repository\cvsnt\2.5.03-2382;%PATH% echo Path added :proceed 

The part to look at is

 not "x%PATH:cvsnt=%" == "x%PATH%" 

First, I replace all occurrences of "cvsnt" with an empty string. The result is compared with PATH without replacing "cvsnt". If they were not equal due to "cvsnt", then they were replaced, then it exists in PATH, and the code can continue .

The initial x to% PATH% is only a placeholder to protect against some "incorrect" start characters. see Batch file: find if the substring is in the line (not in the file)

+1
source

The setx utility has a drawback; it cannot handle variables longer than 1024 characters.

I wrote a script to process cases longer than the limit, and without having to set anything. The answer to the question here: Set environment variables with NSIS in window 7

+1
source

Instead of adding a path every time - you can check if the executable you are looking for is in the path using the following command:

 for %f in (cmd.exe) do if [%~$PATH:f]==[] setx Path "%PATH%;c:\path\to\add" -m 

Be sure to check for /? to learn more about magic %~$PATH:f .

0
source

This is a bit of a hacky solution, but follows the logic you expect:

  • Search PATH
  • Conditionally add to PATH

It never removes anything from PATH, so there is no fear of twisting Windows by deleting something by accident. In addition, it checks the PATH variable directly, so you don't have to worry about another file with the same name as somewhere in PATH.

 echo %PATH% > myTmpPath.tmp find /C /I "c:\path\to\add" myTmpPath.tmp if %ERRORLEVEL% neq 0 setx PATH "%PATH%;c:\path\to\add" del myTmpPath.tmp 

I hate using temporary files, but it's quick and dirty and probably safer than deleting anything from PATH.

The third line is the only difficult task. Basically, this line checks the result of the FIND command above. Rob van der Vude states :

FIND returns an error level of 1 or higher if the search string is not found.

He also explains :

Some executables return negative numbers for error levels! However, this can be fixed using the following code to check for non-zero return codes:
IF% ERRORLEVEL% NEQ 0 ...

0
source

All Articles