-1

I want to make a script so i could use Win+R, "venv (some path)" and add it in path env variable. Problem is that i'm not really a batch coder and idk language. I wrote sth that "should look like working code" but it's not. Could you help me to make this script working?

echo %venvPath%
@set PATH=%PATH%;%venvPath
Mofi
  • 46,139
  • 17
  • 80
  • 143
Dopaminos
  • 1
  • 2
  • "not really a bash coder" -- this is not bash at all; bash and _batch_ are two completely different languages. – Charles Duffy Sep 18 '22 at 20:46
  • That said, if by "venv" you're referring to a Python virtualenv, there should be an `activate.bat` script you can run that will do all the necessary steps for you. – Charles Duffy Sep 18 '22 at 20:47
  • nah, i just mixed up virtual environment and environment variables (and mixed up bash and batch sorry) – Dopaminos Sep 18 '22 at 20:51
  • Please read [Adding the current directory to Windows path permanently](https://stackoverflow.com/a/47080452/3074564) if you want to change the persistent stored __system__ or __user__ `PATH` environment variable which is a nightmare using a batch file. But there is usually needed only `if "%PATH:~-1%" == ";" (set "PATH=%PATH%%venvPath%") else set "PATH=%PATH%;%venvPath%"` to redefine the __local__ environment variable `PATH` for the currently running Windows Command Processor instance and all programs started next from within this command prompt window. – Mofi Sep 19 '22 at 07:10
  • Please read [this answer](https://stackoverflow.com/a/61135726/3074564) explaining how to create a shortcut on Windows desktop or in Windows Start menu which could be also pinned on the Windows taskbar to open a command prompt window, activate a configured virtual environment (= set of environment variables suitable for a specific task) and let `cmd.exe` __keep__ running for commands entered by you next. That is better for using virtual environments than modification of persistent stored __system__ or __user__ environment variables used by all processes running on Windows. – Mofi Sep 19 '22 at 07:14

1 Answers1

1

If you simply need the variable to be updated for the duration of a specific cmd.exe instance, then you should change your second line to:

@Set "PATH=%PATH%%venvPath%;"

If you need it to be more permamently defined, (for future cmd.exe instances), and as there are both a User, and a System, environment, (and the PATH variable is a joining of both), below is one method you could employ from a Windows batch/command script.

User Environment, (recommended):

@For /F "EOL=H Tokens=2,*" %%G In ('%SystemRoot%\System32\reg.exe Query "HKCU\Environment" /V Path 2^>NUL') Do @%SystemRoot%\System32\setx.exe Path "%%H%venvPath%;"

System Environment, (will need to be Run as administrator):

@For /F "EOL=H Tokens=2,*" %%G In ('%SystemRoot%\System32\reg.exe Query "HKLM\SYSTEM\CurrentControlset\Control\Session Manager\Environment" /V Path 2^>NUL') Do @%SystemRoot%\System32\setx.exe Path "%%H%venvPath%;" /M

If, of course, you need it for both the current cmd.exe instance, and all future instances, you'd need to implement both methods.

Notes

There is a 1024 character string length limitation when using setx.exe. This means that for safety, the expanded content of %%H plus the expanded value of %venvPath%, should not exceed that character length. It may therefore be prudent to save the content of %%H%venvPath% as a variable, then perform a character length check on it, before invoking the setx.exe command.

In addition, it would also be prudent to determine whether the expanded content of %venvPath%, is already listed within the existing %PATH% variable value before you attempt to add it. Whilst duplicating it may not immediately or seriously impact the variable, it would unnecessarily increase the character length, and therefore increase the likelihood of the forementioned limitation in subsequent setx.exe commands, with PATH.

Compo
  • 36,585
  • 5
  • 27
  • 39