- PR -

a script to delete a bogus path

1
投稿者投稿内容
Oregon
ベテラン
会議室デビュー日: 2001/08/01
投稿数: 52
お住まい・勤務地: Tualatin 97062
投稿日時: 2001-08-09 03:35
sample

C:\bin> path
PATH=C:\WINNT\system32;C:\WINNT;C:\WINNT\System32\Wbem;.;C:\Program F
iles\Bogus;.;c:\bin
==========================================
C:\bin> delpath.bat C:\Program Files\Bogus
PATH=C:\WINNT\system32;C:\WINNT;C:\WINNT\System32\Wbem;.;.;c:\bin

an implementation:

@echo off
set OUTCOME=
set EXCLUDE=%*
call :accumulate "%PATH%"
set PATH=%OUTCOME%
path
goto :eof
rem
:accumulate
if %1.==. goto :eof
for /f "tokens=1* delims=;" %%i in (%1) do (
if /i not "%%i"=="%EXCLUDE%" call :execset %%i
call :accumulate "%%j"
)
goto :eof
rem
:execset
if "%OUTCOME%".=="". (
set OUTCOME=%*
) else (
set OUTCOME=%OUTCOME%;%*
)
goto :eof

easy to change it to "insert a path at..."
yamachan
会議室デビュー日: 2001/08/02
投稿数: 18
お住まい・勤務地: 東京に生息
投稿日時: 2001-08-09 11:45
Hi Oregon,

Do you like this script? :-)

-------------------------------
@echo off
set PATH=!PATH:;%~1;=;!
set PATH=!PATH:;%~1=;!

[ メッセージ編集済み 編集者: yamachan 編集日時 2001-08-09 11:48 ]
Oregon
ベテラン
会議室デビュー日: 2001/08/01
投稿数: 52
お住まい・勤務地: Tualatin 97062
投稿日時: 2001-08-10 01:34
nice try, but pain starts here.
the %var:xxx=yyy% trick fails for space separated folder names:
(C:\Program Files\)
コード:

C:\bin> path
PATH=C:\WINNT\system32;C:\WINNT;C:\WINNT\System32\Wbem;.;C:\Program F
iles\Bogus;C:\Program F
iles\Another Bogus;.;c:\bin
==========================================
C:\bin> delp2.bat C:\Program Files\Bogus
PATH=C:\WINNT\system32;C:\WINNT;C:\WINNT\System32\Wbem; Files\Bogus; F
iles\Another Bogus;.;.;c:\bin


it deleted only %1 portion of "Bogus" and "Another Bogus" folders with PATH being left imcomplete.
in addition arg quoting also fail:
コード:

C:\bin> delp2.bat "C:\Program Files\Bogus"
PATH=C:\WINNT\system32;C:\WINNT;C:\WINNT\System32\Wbem;;C:\Program Files\Another Bogus;.;.;c:\bin


notice ";;" was left in the midst.
yamachan
会議室デビュー日: 2001/08/02
投稿数: 18
お住まい・勤務地: 東京に生息
投稿日時: 2001-08-10 11:36
Hi Oregon, Thanks!

Yes, it needs quoting for space separated folder names. That's why I used "%~1", not "%1". :-)

About another problem, I have no idea. I found a bug, but I think it wasn't the cause of the ";;" problem.

Anyway, the following code looks working fine in my environment. CMD's option is "/f:on /v:on"
-------------------------------
@echo off
set PATH=!PATH:;%~1;=;!
set PATH=!PATH:;%~1=!
Oregon
ベテラン
会議室デビュー日: 2001/08/01
投稿数: 52
お住まい・勤務地: Tualatin 97062
投稿日時: 2001-08-11 02:16
the last line of your code really bothers me.
because it's not guarded by ";" delimiter.

the open ended pattern like this would cause a diaster: e.g.
suppose we try to remove "C:\WINNT" from
PATH=C:\WINNT\system32;C:\WINNT;C:\WINNT\System32\Wbem
the result is:
C:\WINNT\system32;\System32\Wbem

another thing is that the two lines for middle ones and the last one in the path
but the very first one.

to be constuctive, and since I really like the yamachan's idea, how about putting the ";" guards around?

[1] prepend/append ";" to PATH
[2] prepend/append ";" to the BOGUS path
[3] use %var:a=b% to replace [2] out of [1]
[4] remove ";" guards from [3]

コード:

@echo off
set FROM=;%PATH%;
set PATH=!FROM:;%~1;=;!
set PATH=%PATH:~1%
set PATH=%PATH:~0,-1%

yamachan
会議室デビュー日: 2001/08/02
投稿数: 18
お住まい・勤務地: 東京に生息
投稿日時: 2001-08-17 15:20
Hi Oregon,

Sorry for my slow response. This week is a big vacation season in Japan, and I was off-lined. :-)

"prepend/append" solution is cool! Your code looks fine. I updated it just to suit my taste. :-)
-------------------------------------
@echo off
set PATH=;%PATH%;
set PATH=!PATH:;%~1;=;!
set PATH=%PATH:~1,-1%
1

スキルアップ/キャリアアップ(JOB@IT)