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

@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
HKEY_LOCAL_MACHINE\\System\\CurrentControlSet\\Control\\Session Manager\\Environment
C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Windows\System32\OpenSSH\;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\Program Files (x86)\Windows Kits\8.1\Windows Performance Toolkit\;C:\Program Files (x86)\CMake 2.8\bin;C:\Users\Administrator\AppData\Local\Microsoft\WindowsApps;C:\Program Files\JetBrains\IntelliJ IDEA 2020.1.4\bin;C:\Users\Administrator\AppData\Local\Microsoft\WindowsApps;C:\Program Files\JetBrains\IntelliJ IDEA 2020.1.4\bin;D:\a_soft\a_green\java\jdk\17\bin;D:\a_soft\a_green\java\maven\bin;
HKEY_CURRENT_USER\\Environment
在这里插入代码片
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

lst0426

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值