5

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.

  1. 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?
  2. Is there a better way to overcome this scenario?
Compo
  • 36,585
  • 5
  • 27
  • 39
Phantom
  • 71
  • 4
  • 1
    You might not believe it, but others wanted to add a folder path also to system path using a batch file. So lots of solutions can be found on using Stack Overflow search for example with [\[batch-file\] system path add](https://stackoverflow.com/search?q=%5Bbatch-file%5D+system+path+add). There are lots of __not__ good solutions which tend to corrupt __system__ `PATH` and some very good solutions. – Mofi May 31 '19 at 10:11
  • 1
    Here are some of my solutions: [Adding the current directory to Windows path permanently](https://stackoverflow.com/a/47080452/3074564), [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) and [How can I use a .bat file to remove specific tokens from the PATH environment variable?](https://stackoverflow.com/a/38664286/3074564) There is a lot to read, but please read it. – Mofi May 31 '19 at 10:17
  • Use the registry key, instead! – Compo May 31 '19 at 10:17
  • Please take also a look on [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 looks like you don't need current directory path added persistent to __system__ `PATH`. You just want to add this folder path to __local__ `PATH`. This is usually not needed because of `cmd.exe` searches first always in current directory for a program or script before making use of __local__ `PATH` and __local__ `PATHEXT`. – Mofi May 31 '19 at 10:23
  • So it could be enough to use in batch file just `if "%PATH:~-1%" == ";" ( set "PATH=%PATH%%CD%" ) else ( set "PATH=%PATH%;%CD%" )` to __append__ current directory path to __local__ `PATH` environment variable not persistent stored in Windows registry and being used only by currently running command process and all applications started from within this command process. – Mofi May 31 '19 at 10:26
  • @mofi, yes, usually cmd.exe and `CreateProcess` (without an explicit application path) search the working directory. CMD searches there first, whereas a generic `CreateProcess` call searches the application directory (i.e. `%__APPDIR__%`) before the working directory. However, if we define an environment variable named "NoDefaultCurrentDirectoryInExePath" with any value, then they do not search in the working directory unless the name has a backslash in it (e.g. "\spam\eggs.exe"). A "." entry can be added to the end of the system or user "Path", which is more secure but still convenient. – Eryk Sun May 31 '19 at 10:58
  • @eryksun Yes, I know because of your comment on [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) resulting in chapter __H) Is it possible to disable file search in current directory?__ But I am quite sure that this environment variable is used only on Windows servers but not on Windows clients as nearly no batch file would work anymore with this environment variable defined. – Mofi May 31 '19 at 11:09
  • @Mofi, I always define this environment variable on my systems, but I add "." back explicitly to the end of the user "Path", so the working directory is still searched. It's just the last directory searched. Anyway, almost always I use ".\" to refer to a file in the working directory. – Eryk Sun May 31 '19 at 11:47
  • Thank you all for helping out. I found the answer I was looking for in one of the links provided by @Mofi. I'll add the answer below. – Phantom Jun 03 '19 at 03:30

1 Answers1

1

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%"
Phantom
  • 71
  • 4