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?
9

1 Answer 1

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%"
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.