Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
I wanted to write a batch file that would remove a directory from the path, but not delete it. Is there a way to do that, and if there is how do I do it?

i dont get what you mean....
do you want to move it to another location? if you 'remove' it it will be the same as deleting it.

Do you mean remove the directory from the PATH environment variable? It can be done, but it's not trivial. You also need to specify whether you want it removed from the environment of the current process, or from the persistent environment stored in the Registry that's used by all processes.
I don't have any such batch file but I'm sure someone, somewhere, must have written one. I just edit the path in the System Properties dialog box instead.

I want a batch file that would remove the directory from the path (not delete it), so when I type in PATH, it won't show the directory that I removed.
Its fine if it removes the directory from the current process. I really appreciate the help

Ok, here's a batch file I've just written. Save it as DELPATH.CMD and run it as "DELPATH folder".
@echo off
setlocal EnableDelayedExpansionif not @%1==@ if not "%~1"=="/?" goto :doit
echo.
echo Usage: DELPATH directory
echo.
echo Deletes a folder specification from the PATH environment variable. For example,
echo if the PATH contains C:\WINDOWS\system32;C:\my docs\temp;C:\prog and you type
echo DELPATH C:\my docs\temp then the PATH will now end up with just
echo C:\WINDOWS\system32;C:\prog. It does not touch the actual folder itself. The
echo PATH is only altered for the current Command Prompt window.
exit /b 1:doit
set NEWPATH=
for %%i in (%PATH: =@4#c@%) do (
set p=%%i
set p=!p:@4#c@= !
if not "!p!" == "%*" (
echo Including !p!
if defined NEWPATH (set NEWPATH=!NEWPATH!;!p!) else (set NEWPATH=!p!)
) else (
echo Excluding !p!
)
)endlocal & set PATH=%NEWPATH%
The above command works for the environment of the current process, i.e. if you remove a folder from the path, and then open another command window, the folder will still be there. To remove it permanently, you need to remove it from the User or System path in the Registry, and for that you need to download the Windows Server 2003 Resource Kit Tools and run the PATHMAN utility.

Hi klint,
You got me mystified with (%PATH: =@4#c@%).
Anyway, your bat seems to require a case SENSITIVE parameter.
::== PATHDROP.BAT
@echo off
setlocal EnableDelayedExpansionif %1'==' echo which chunk? & goto :eof
for %%P in (%PATH%) do (
if /i not "%%P"=="%1" set NEWPATH=!NEWPATH!;%%P
)echo the new path is !NEWPATH!
=====================================
If at first you don't succeed, you're about average.M2

Hi M2,
> You got me mystified with (%PATH: =@4#c@%)
Don't worry. It's actually a bit of crap. No, really, it is. The problem is that the Command Processor's tokenizer breaks up the path using both spaces and semicolons as delimiters.
To see what I mean, if you look at your example, you will see that it will give you separate iterations for "C:\Program", "Files\Microsoft\Visual", "Studio\etc....".
I want it to use only semicolons as delimiters. Therefore, I initially replace every space in the path with a bit of crap (@4#c@) that is very unlikely to be used in a real string, so I can assume it's a unique string. I then look at each semicolon-delimited path component, and replace the crap with a space, thus restoring the original.
You're right, I could have used IF/I to make it case-insensitive. Thanks for the suggestion. That was my version 0.1, and there may be other improvements possible too.

there may be other improvements possible too.
I got one!@set ParseablePath="%PATH:;=";"%"
@set ParseablePath=%ParseablePath:";""="%
@for %%a in (%ParseablePath%) do @echo %%a
If you want the version I was going to post, replace ParseablePath with a. I figured that might have been a bit too confusing...

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |