从用户和系统PATH中删除文件夹路径的批处理注释代码:

2023-07-08 14:27:46 浏览数 (1)

代码语言:javascript复制
@echo off
setlocal EnableExtensions DisableDelayedExpansion
set"PathToRemove1=C:\Temp\Test"
set"PathToRemove2=C:\Temp"

rem Get directly from Windows registry the system PATH variable value.
for /F"skip=2 tokens=1,2*" %%N in ('%SystemRoot%\System32
eg.exe query"HKLM\System\CurrentControlSet\Control\Session Manager\Environment" /v"Path" 2^>nul') do (
    if /I"%%N" =="Path" (
        set"SystemPath=%%P"
        if defined SystemPath goto CheckSystemPath
    )
)
echo Error: System environment variable PATH not found with a value in Windows registry.
echo/
goto UserPath

:CheckSystemPath
setlocal EnableDelayedExpansion
rem Does the system PATH not end with a semicolon, append one temporarily.
if not"!SystemPath:~-1!" ==";" set"SystemPath=!SystemPath!;"
rem System PATH should contain only backslashes and not slashes.
set"SystemPath=!SystemPath:/=\!"

rem Check case-insensitive for the folder paths to remove as defined at top
rem of this batch script and remove them if indeed found in system PATH.
set"PathModified=0"
for /F"tokens=1* delims==" %%I in ('set PathToRemove') do (
    if not"!SystemPath:%%J;=!" =="!SystemPath!" (
        set"SystemPath=!SystemPath:%%J;=!"
        set"PathModified=1"
    )
)

rem Replace all two or more ; in series by just one ; in system path.
:CleanSystem
if not"!SystemPath:;;=;!" =="!SystemPath!" set"SystemPath=!SystemPath:;;=;!" & goto CleanSystem

rem Remove the semicolon at end of system PATH if there is one.
if"!SystemPath:~-1!" ==";" set"SystemPath=!SystemPath:~0,-1!"
rem Remove a backslash at end of system PATH if there is one.
if"!SystemPath:~-1!" =="" set"SystemPath=!SystemPath:~0,-1!"

rem Update system PATH using command SETX which requires administrator
rem privileges if the system PATH needs to be modified at all. SETX is
rem by default not installed on Windows XP and truncates string values
rem longer than 1024 characters to 1024 characters. So use alternatively
rem command REG to add system PATH if command SETX cannot be used or is
rem not available at all.
if"%PathModified%" =="1" (
    set"UseSetx=1"
    if not"!SystemPath:~1024,1!" =="" set"UseSetx="
    if not exist %SystemRoot%\System32\setx.exe set"UseSetx="
    if defined UseSetx (
        %SystemRoot%\System32\setx.exe Path"!SystemPath!" /M >nul
    ) else (
        set"ValueType=REG_EXPAND_SZ"
        if"!SystemPath:%%=!" =="!SystemPath!" set"ValueType=REG_SZ"
        %SystemRoot%\System32
eg.exe ADD"HKLM\System\CurrentControlSet\Control\Session Manager\Environment" /f /v Path /t !ValueType! /d"!SystemPath!">nul
    )
)
endlocal

:UserPath
rem Get directly from Windows registry the user PATH variable value.
for /F"skip=2 tokens=1,2*" %%N in ('%SystemRoot%\System32
eg.exe query"HKCU\Environment" /v"Path" 2^>nul') do (
    if /I"%%N" =="Path" (
        set"UserPath=%%P"
        if defined UserPath goto CheckUserPath
        rem User PATH exists, but with no value, delete user PATH.
        goto DeleteUserPath
    )
)
rem This PATH variable does often not exist and therefore nothing to do here.
goto PathUpdateDone

:CheckUserPath
setlocal EnableDelayedExpansion
rem Does the user PATH not end with a semicolon, append one temporarily.
if not"!UserPath:~-1!" ==";" set"UserPath=!UserPath!;"

rem Check case-insensitive for the folder paths to remove as defined at top
rem of this batch script and remove them if indeed found in user PATH.
set"PathModified=0"
for /F"tokens=1* delims==" %%I in ('set PathToRemove') do (
    if not"!UserPath:%%J;=!" =="!UserPath!" (
        set"UserPath=!UserPath:%%J;=!"
        set"PathModified=1"
        if not defined UserPath goto DeleteUserPath
    )
)

rem Replace all two or more ; in series by just one ; in user path.
:CleanUser
if not"!UserPath:;;=;!" =="!UserPath!" set"UserPath=!UserPath:;;=;!" & goto CleanUser

rem Remove the semicolon at end of user PATH if there is one.
if"!UserPath:~-1!" ==";" set"UserPath=!UserPath:~0,-1!"
if not defined UserPath goto DeleteUserPath

rem Update user PATH using command SETX which does not require administrator
rem privileges if the user PATH needs to be modified at all. SETX is
rem by default not installed on Windows XP and truncates string values
rem longer than 1024 characters to 1024 characters. So use alternatively
rem command REG to add user PATH if command SETX cannot be used or is
rem not available at all.
if"%PathModified%" =="1" (
    set"UseSetx=1"
    if not"!UserPath:~1024,1!" =="" set"UseSetx="
    if not exist %SystemRoot%\System32\setx.exe set"UseSetx="
    if defined UseSetx (
        %SystemRoot%\System32\setx.exe Path"!UserPath!" /M >nul
    ) else (
        set"ValueType=REG_EXPAND_SZ"
        if"!UserPath:%%=!" =="!UserPath!" set"ValueType=REG_SZ"
        %SystemRoot%\System32
eg.exe ADD"HKCU\Environment" /f /v Path /t !ValueType! /d"!UserPath!">nul
    )
)
goto PathUpdateDone

:DeleteUserPath
rem Delete the user PATH as it contains only folder paths to remove.
%SystemRoot%\System32
eg.exe delete"HKCU\Environment" /v"Path" /f >nul

:PathUpdateDone
rem Other code could be inserted here.
endlocal
endlocal

代码语言:javascript复制
HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment
代码语言:javascript复制
C:Windowssystem32;C:Windows;C:WindowsSystem32Wbem;C:WindowsSystem32WindowsPowerShellv1.0;C:WindowsSystem32OpenSSH;C:Program Files (x86)NVIDIA CorporationPhysXCommon;C:Program Files (x86)Windows Kits8.1Windows Performance Toolkit;C:Program Files (x86)CMake 2.8bin;C:UsersAdministratorAppDataLocalMicrosoftWindowsApps;C:Program FilesJetBrainsIntelliJ IDEA 2020.1.4bin;C:UsersAdministratorAppDataLocalMicrosoftWindowsApps;C:Program FilesJetBrainsIntelliJ IDEA 2020.1.4bin;D:a_softa_greenjavajdk17bin;D:a_softa_greenjavamavenbin;
代码语言:javascript复制
HKEY_CURRENT_USER\Environment
代码语言:javascript复制
在这里插入代码片

0 人点赞