首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >从用户和系统PATH中删除文件夹路径的批处理注释代码:

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

作者头像
用户3519280
发布2023-07-08 14:27:46
发布2023-07-08 14:27:46
1.3K00
代码可运行
举报
文章被收录于专栏:c++ 学习分享c++ 学习分享
运行总次数:0
代码可运行
代码语言:javascript
代码运行次数:0
运行
复制
@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
代码运行次数:0
运行
复制
HKEY_LOCAL_MACHINE\\System\\CurrentControlSet\\Control\\Session Manager\\Environment
代码语言:javascript
代码运行次数:0
运行
复制
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;
代码语言:javascript
代码运行次数:0
运行
复制
HKEY_CURRENT_USER\\Environment
代码语言:javascript
代码运行次数:0
运行
复制
在这里插入代码片
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2023-06-02,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档