Computing.Net > Forums > Programming > Remove a Directory

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Remove a Directory

Reply to Message Icon

Name: hoffmann2002
Date: May 12, 2008 at 12:46:49 Pacific
OS: XP
CPU/Ram: 504mb of ram
Product: HP
Comment:

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?



Sponsored Link
Ads by Google

Response Number 1
Name: chrisyunke
Date: May 12, 2008 at 13:39:51 Pacific
Reply:

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.


0

Response Number 2
Name: klint
Date: May 12, 2008 at 14:12:43 Pacific
Reply:

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.


0

Response Number 3
Name: hoffmann2002
Date: May 13, 2008 at 07:24:55 Pacific
Reply:

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


0

Response Number 4
Name: klint
Date: May 13, 2008 at 09:53:45 Pacific
Reply:

Ok, here's a batch file I've just written. Save it as DELPATH.CMD and run it as "DELPATH folder".


@echo off
setlocal EnableDelayedExpansion

if 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.


0

Response Number 5
Name: hoffmann2002
Date: May 13, 2008 at 13:15:18 Pacific
Reply:

Thank you,
Its exactly what i wanted!


0

Related Posts

See More



Response Number 6
Name: Mechanix2Go
Date: May 14, 2008 at 08:15:56 Pacific
Reply:

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 EnableDelayedExpansion

if %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


0

Response Number 7
Name: klint
Date: May 14, 2008 at 10:45:47 Pacific
Reply:

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.


0

Response Number 8
Name: Razor2.3
Date: May 14, 2008 at 19:20:10 Pacific
Reply:

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...

0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

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


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Remove a Directory

Renaming a directory of files with spaces www.computing.net/answers/programming/renaming-a-directory-of-files-with-spaces/19875.html

functions to share a directory www.computing.net/answers/programming/functions-to-share-a-directory/83.html

java routine to read a directory www.computing.net/answers/programming/java-routine-to-read-a-directory/11359.html