How do you avoid over-populating the PATH Environment Variable in Windows?

This will parse your %PATH% environment variable and convert each directory to its shortname equivalent and then piece it all back together:

@echo off

SET MyPath=%PATH%
echo %MyPath%
echo --

setlocal EnableDelayedExpansion

SET TempPath="%MyPath:;=";"%"
SET var=
FOR %%a IN (%TempPath%) DO (
    IF exist %%~sa (
        SET "var=!var!;%%~sa"
    ) ELSE (
        echo %%a does not exist
    )
)

echo --
echo !var:~1!

Take the output and update the PATH variable in environment variables.

Leave a Comment