Batch script that should set a path variable breaks the string
Matthew Martinez
Im trying to set a user-path variable via setx in a win-batch-script, but it keeps breaking. The path that I enter...
setx path "%path%;%ProgramFiles%\Pseudocode\"...leads to this:
Sadly, I found nothing online and running the script as an admin also makes no difference.
How can I assure, that everyone can download and run this script and still add the whole path?
or
Is there another approach to register a "custom inner console" in cmd (like java, git, vsce, npm) without using path variables?
I have tried to add a powershell-script and execute that one instead of calling setx...
$newPath = $env:Path + '%ProgramFiles%\Pseudocode\;'
[Environment]::SetEnvironmentVariable("Path", $newPath, "Machine")
$env:Path = $newPath...but that hasn't worked. (When executing the script, nothing changed.)
22 Answers
The limit of the %path% variable has a maximum length of 1024 characters, you may have already reached this limit so the rest after C:\Prog got cut off...
16You are running into the maximum length of how many entries you can have in the PATH environmental variable.
Keep in mind that the only thing the PATH environmental does, is allow you to run a command from anywhere and it will search for those folders too.
You can shorten paths by finding out what the old style dos name is, and then edit the path and change them. This will work on your system, but will become a problem on other systems. In case you want to do it on your system, here's how you find out.
cd /d c:\
dir /xThis will show you something like this:
C:\>dir /x Volume in drive C has no label. Volume Serial Number is 1C0C-DB56 Directory of C:\
07-12-2019 10:14 <DIR> PerfLogs
20-02-2022 15:10 <DIR> PROGRA~1 Program Files
08-03-2022 17:47 <DIR> PROGRA~2 Program Files (x86)
09-03-2022 14:53 <DIR> temp
14-11-2021 19:27 <DIR> Users
04-03-2022 21:11 <DIR> Windows 0 File(s) 0 bytes 6 Dir(s) 509.363.785.728 bytes free
C:\>You can substitute the full C:\Program Files (x86)\... by C:\PROGRA~2\... in all your path rows, and other folders with a space or that are longer than 8 chars will also have a shortened name.
Once done, you can add your entry to the Path, although it is recommended to use the short version, which would probably be: C:\PROGRA~1\Pseudo~1
Now, that said... given that path is only used to be able to run a program from anywhere so it searches for that path, you do not need to actually include entries in the path environmental if you always reference to the full path of your program. That could be done such as: %ProgramFiles%\Pseudocode\myprogram.exe