我發現solution通過Windows命令行直接從環境變量PATH中刪除某個目錄。但我想通過從cmd文件中發出命令來達到相同的結果。使用cmd文件從PATH中刪除不需要的目錄
什麼我能做的就是呼應我想PATH設置爲結果,如果我把
echo %PATH:D:\dir-path;=%
到我的CMD文件。但如果我寫
set PATH=%PATH:D:\dir-path;=%
PATH只包含我想要刪除的目錄。
此外,我想爲導演路徑使用一個變量,但如果我嘗試結果更糟。我把
set dirPath=D:\dir-path
echo %PATH:%dirPath%;=%
和我所得到的全部是dirPath;=
。
我不知道如何解決這個問題,所以如果有人能夠協助我會很感激。
編輯1
正如@Anders建議我現在有:
@echo off
set exampleRemoveDir=d:\bar
REM If I use %exampleRemoveDir% it does not work at all.
call :removeFromPath exampleRemoveDir & set result=%ERRORLEVEL%
:removeFromPath
set removeDir=%~1
setlocal enableextensions enabledelayedexpansion
set _=!PATH:%removeDir%;=!
if "%_%" == "%PATH%" set _=!PATH:%removeDir%=!
endlocal & set PATH=%_%
echo %PATH%
exit /b 0
但是與解決途徑仍保持不變。代碼完美無缺地工作,如果我沒有將刪除步驟的範圍縮小,因爲我想爲多個目錄執行該操作,如果沒有這樣一個好幫手爲我完成這項工作真的很痛苦。
EDIT 2
因爲這似乎比我還以爲這裏是我的完整不變的腳本,使複雜得多。
@echo off
:: Switching Perl version from ActivePerl to StrawberryPerl or the other way round depending on which paths are set in
:: PATH.
:: Directories for ActivePerl
set activePerl_SiteBinPath=D:\ProgramFiles\ActivePerl\site\bin
set activePerl_BinPath=D:\ProgramFiles\ActivePerl\bin
:: Directories for StrawberryPerl
set strawberryPerl_SiteBinPath=D:\ProgramFiles\StrawberryPerl\perl\site\bin
set strawberryPerl_BinCPath=D:\ProgramFiles\StrawberryPerl\c\bin
set strawberryPerl_BinPath=D:\ProgramFiles\StrawberryPerl\perl\bin
:: Determine which of the directories are present in PATH and which are not.
call :isInPath %activePerl_SiteBinPath% & set foundActivePerl_SiteBinPath=%ERRORLEVEL%
call :isInPath %activePerl_BinPath% & set foundActivePerl_BinPath=%ERRORLEVEL%
call :isInPath %strawberryPerl_SiteBinPath% & set foundStrawberryPerl_SiteBinPath=%ERRORLEVEL%
call :isInPath %strawberryPerl_BinCPath% & set foundStrawberryPerl_BinCPath=%ERRORLEVEL%
call :isInPath %strawberryPerl_BinPath% & set foundStrawberryPerl_BinPath=%ERRORLEVEL%
:: Test
call :removeFromPath %strawberryPerl_SiteBinPath% & set removedStrawberryPerl_SiteBinPath=%ERRORLEVEL%
rem if /i %foundActivePerl_SiteBinPath% equ 0 if /i %foundActivePerl_BinPath% equ 0 (
rem if /i %foundStrawberryPerl_SiteBinPath% neq 0 if /i %foundStrawberryPerl_BinPath% neq 0 if /i %foundStrawberryPerl_BinCPath% neq 0 (
rem echo Switching from ActivePerl to StrawberryPerl.
rem TODO
rem exit /b 0
rem )
rem)
rem
rem if /i %foundStrawberryPerl_SiteBinPath% equ 0 if /i %foundStrawberryPerl_BinPath% equ 0 if /i %foundStrawberryPerl_BinCPath% equ 0 (
rem if /i %foundActivePerl_SiteBinPath% neq 0 if /i %foundActivePerl_BinPath% neq 0 (
rem echo Switching from StrawberryPerl to ActivePerl.
rem TODO
rem exit /b 0
rem )
rem)
:: Error
:: TODO
exit /b
:isInPath
:: Tests if the path stored within variable pathVar exists within %PATH%.
::
:: The result is returned as the ERRORLEVEL:
:: 0 if the pathVar path is found in %PATH%.
:: 1 if pathVar path is not found in %PATH%.
:: 2 if parhVar path is missing or undefined.
:: Error checking
if "%~1"=="" exit /b 2
set pathVar=%~1
for /f %%i in ('echo ";%%PATH%%;" ^| find /c /i ";%pathVar%;"') do set /a foundPathVar=%%i
if /i %foundPathVar% equ 0 (
exit /b 1
)
exit b/ 0
:removeFromPath
:: Removes a given directory from environment variable PATH if the directory exists within PATH.
::
:: The result is returned as the ERRORLEVEL:
:: 0 if the given directory was removed from PATH or if it didn't exist in PATH.
:: 1 if no directory was given or the directory is undefined.
:: Error checking
if "%~1"=="" exit /b 2
set removeDir=%~1
setlocal enableextensions enabledelayedexpansion
set _=!PATH:%removeDir%;=!
if "%_%" == "%PATH%" set _=!PATH:;%removeDir%=!
endlocal & set PATH=%_%
echo %PATH%
exit /b 0
編輯3
由於@Anders從路徑現在刪除目錄工作正常。我刪除了上面未定義參數的錯誤檢查,並在函數調用的參數周圍添加了%
。 但不知何故:isInPath
現在總是返回零。就我而言,這已經奏效了......:/此外,使用當前代碼(在所有函數參數中添加了%
),在調用:removeFromPath
後,cmd總是直接關閉,而不管pause
。真是令人生氣!
爲什麼你想從'%PATH%'變量刪除位置?您是否知道,您在批處理/ cmd.exe會話中使用'SET'PATH =%PATH:match = replace%「進行的任何更改都不是先決條件,它只會持續與cmd實例一樣長。你能否提供更多的批處理文件,以便我們更好地處理你希望達到的目標。 – Compo
@Lilo,腳本中是否有其他功能可能導致問題?我測試了安德斯的腳本,確實確實刪除了路徑中的目錄。 –
從長遠來看,我想使用setx,但只要它不能與set一起工作,爲什麼我會使用setx? 我已經安裝了ActivePerl以及es StrawberryPerl,我希望能夠通過簡單地雙擊批處理腳本來切換當前使用的Perl變體。爲了實現這一點,我必須檢查哪些目錄當前在PATH中,因此必須刪除或添加以從當前使用的變體切換到另一個。 – Lilo