Skip to content
TheChatArchiveThe Chat Archive
Qwenqwen3-235b-a22b-no-thinking· Alibaba (Qwen)Hugging Face LMArena Human Preference 140K

why batch script below removed %SystemRoot%\system32\ from path? @echo off setlocal…

From a Chatbot Arena battleThe same person sent this prompt to two anonymous models at once. Compare it with Geminigemini-2.5-flash’s answer.
  1. User

    why batch script below removed %SystemRoot%\system32\ from path? @echo off setlocal enabledelayedexpansion

    REM Retrieve system PATH from registry set "syspath=" for /f "skip=2 tokens=2,*" %%A in ( 'reg query "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v Path 2^>nul' ) do set "syspath=%%B"

    if not defined syspath ( echo System PATH not found. exit /b 1 )

    REM Process each directory in PATH set "newpath=" set "modified_syspath=%syspath:;=";"%" for %%D in ("%modified_syspath%") do ( set "dir=%%~D" echo !dir! pause if exist "!dir!" ( set "newpath=!newpath!;!dir!" ) else echo Removing !dir!... )

    REM Remove leading semicolon if newpath exists if defined newpath ( set "newpath=!newpath:~1!" ) else ( set "newpath=" )

    REM Update registry with cleaned PATH reg add "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v Path /t REG_EXPAND_SZ /d "!newpath!" /f >nul if errorlevel 1 ( echo Failed to update system PATH. Run as Administrator? exit /b 1 )

    echo System PATH cleaned successfully. echo Restart applications to apply changes.

    endlocal pause

  2. Qwen Assistant

    The batch script you provided is intended to clean up the system PATH environment variable by removing directories that no longer exist on the system. However, you're observing that it's removing %SystemRoot%\system32\ from the PATH, even though that directory exists and should not be removed.

    Let’s analyze why this might happen, step-by-step.


    🔍 Root Cause: Improper Handling of %SystemRoot% in Paths

    Your system PATH likely contains paths like:

    %SystemRoot%\system32;...
    

    In your script, you process these paths without expanding environment variables like %SystemRoot%. As a result, when it checks if the path exists using:

    if exist "!dir!"
    

    …this will fail, because %SystemRoot% has not been expanded to something like C:\Windows, so Windows doesn't recognize the literal %SystemRoot%\system32 as a valid path.

    This causes the script to think that %SystemRoot%\system32 does not exist — and therefore removes it.


    ✅ Solution: Expand Environment Variables Before Checking Existence

    You need to expand any environment variables in each path before checking if the directory exists.

    Add this line just after setting dir:

    set "dir=!dir:%%SystemRoot%%=%SystemRoot%!"
    call set "dir=%%dir%%"
    

    Or more generally (to expand all variables):

    call set "dir=%%dir%%"
    

    This uses call with double expansion to resolve any environment variables in the path string (like %SystemRoot%, %ProgramFiles%, etc.).


    ✏️ Fixing Your Script

    Here is your corrected version with the fix applied:

    @echo off
    setlocal enabledelayedexpansion
    
    REM Retrieve system PATH from registry
    set "syspath="
    for /f "skip=2 tokens=2,*" %%A in (
        'reg query "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v Path 2^>nul'
    ) do set "syspath=%%B"
    
    if not defined syspath (
        echo System PATH not found.
        exit /b 1
    )
    
    REM Process each directory in PATH
    set "newpath="
    set "modified_syspath=%syspath:;=";"%"
    for %%D in (%modified_syspath%) do (
        set "dir=%%~D"
        
        REM Expand environment variables in the path
        call set "expanded_dir=%%dir%%"
        
        echo !expanded_dir!
        pause
        
        if exist "!expanded_dir!" (
            set "newpath=!newpath!;!expanded_dir!"
        ) else (
            echo Removing !dir!...
        )
    )
    
    REM Remove leading semicolon if newpath exists
    if defined newpath (
        set "newpath=!newpath:~1!"
    ) else (
        set "newpath="
    )
    
    REM Update registry with cleaned PATH
    reg add "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v Path /t REG_EXPAND_SZ /d "!newpath!" /f >nul
    if errorlevel 1 (
        echo Failed to update system PATH. Run as Administrator?
        exit /b 1
    )
    
    echo System PATH cleaned successfully.
    echo Restart applications to apply changes.
    
    endlocal
    pause
    

    🧠 Key Takeaways

    • Always expand environment variables like %SystemRoot% before doing file/path operations.
    • Use call set "var=%%var%%" to force expansion of variables inside strings.
    • Be cautious when modifying the system PATH — especially under HKLM.

    ⚠️ Important Notes

    • Running this script requires Administrator privileges, otherwise the registry update will fail.
    • This script modifies the system-wide PATH, which affects all users and may impact system behavior.
    • Always back up the current PATH value before making changes.

    Let me know if you'd like a version that logs the old PATH or prompts for confirmation before applying changes.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.