943,940 Members | Top Members by Rank

Ad:
You are currently viewing page 1 of this multi-page discussion thread
Aug 23rd, 2007
-1

Delete a directory using batch script

Expand Post »
Hi,

I need a batch script, which has to create a directory, but if the directory already exists, it has to delete it, and create a fresh one. I dont know, what command to be used.

Normally, to check for a file exist, we do like

IF EXIST sample.txt DEL sample.txt, so that we can create a new file.

but, this doesn't work for the directory like if exist <dir name> del <dir name>

could anyone suggest what is the script to be used, to do this?
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
satya.vijai is offline Offline
9 posts
since Apr 2007
Sep 7th, 2007
0

Re: Delete a directory using batch script

are you sure?

what OS?

i just opened up the command prompt and typed


IF EXIST Desktop ECHO hi

my prompt Echo'd out HI

to test i wrote


IF EXIST DirThatDoesNotExist ECHO hi

nothing was echo'd back out!
Reputation Points: 152
Solved Threads: 39
Master Poster
Killer_Typo is offline Offline
778 posts
since Apr 2004
Sep 27th, 2007
0

Re: Delete a directory using batch script

DEL will not remove directorys

use RD <dirname> - only works if <dirname> is empty (ie. no files in it)

DELTREE <dirname> - will delete <dirname> and all subdirectorys even if it contains files
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Bulldog_466 is offline Offline
1 posts
since Sep 2007
Sep 27th, 2007
0

Re: Delete a directory using batch script

DEL will not remove directorys

use RD <dirname> - only works if <dirname> is empty (ie. no files in it)

DELTREE <dirname> - will delete <dirname> and all subdirectorys even if it contains files
RD /S will achieve the same as DELTREE
Reputation Points: 152
Solved Threads: 39
Master Poster
Killer_Typo is offline Offline
778 posts
since Apr 2004
Oct 19th, 2007
0

Re: Delete a directory using batch script

The answer actually depends on which version of Windows you are using. Also, since this is a batch file, you want to suppress questions like "Do you really want to delete X?", which will really confuse and frighten your end-user.

Here's a batch that should work on all versions of windows for you: redir.bat
@echo off

if %1.==. goto usage

if exist %1\nul goto deldir
goto newdir

::------------------------------------------------
:deldir

:: Determine if we are using Windows 2000 or XP
ver | find "XP" > nul
if %ERRORLEVEL% == 0 goto xp-and-2000

ver | find "2000" > nul
if %ERRORLEVEL% == 0 goto xp-and-2000

goto nt-and-older

:xp-and-2000
rd /s/q %1
goto newdir

:nt-and-older
deltree /y %1

::------------------------------------------------
:newdir
md %1
goto end

::------------------------------------------------
:usage
echo usage:
echo   %0 DIRNAME
echo.
echo Deletes the directory named DIRNAME and everything in it if it exists!
echo Then creates a new directory named DIRNAME
echo.

:end

Hope this helps.
Featured Poster
Reputation Points: 1140
Solved Threads: 229
Postaholic
Duoas is offline Offline
2,039 posts
since Oct 2007
Oct 23rd, 2007
0

Re: Delete a directory using batch script

Try rd /s dirToDelete

If you don't want it to ask if you're sure, then
try rd /s /q dirToDelete

Hoppy
Reputation Points: 53
Solved Threads: 13
Junior Poster
hopalongcassidy is offline Offline
148 posts
since Oct 2007
Oct 2nd, 2009
0

Re: Delete a directory using batch script

RD /S will achieve the same as DELTREE

But, from my batch file, it asks me to confirm the delete. Can I somehow force the delete without a question at the command prompt?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
bugatha1 is offline Offline
2 posts
since Sep 2009
Oct 5th, 2009
0

Re: Delete a directory using batch script

It is always great to give a nice, complete example and to be completely ignored not only at the time the example was given, but also a full year later.
Featured Poster
Reputation Points: 1140
Solved Threads: 229
Postaholic
Duoas is offline Offline
2,039 posts
since Oct 2007
Oct 8th, 2009
0
Re: Delete a directory using batch script
Hi there

your answer worked very well indeed.

but i have another question: how can I delete a folder (Cookies) that is found in multiple folders (user1\cookies ; user2\cookies ; ...)

so I need the "rd /q /s cookies" to e executed on different folders.

thanks in advance!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
hmaarten is offline Offline
2 posts
since Oct 2009
Oct 10th, 2009
0
Re: Delete a directory using batch script
Do you mean like
rd /q/s C:\DOCUME~1\John\Cookies
rd /q/s C:\DOCUME~1\Jill\Cookies
rd /q/s C:\DOCUME~1\Johann\Cookies
?
Featured Poster
Reputation Points: 1140
Solved Threads: 229
Postaholic
Duoas is offline Offline
2,039 posts
since Oct 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Legacy and Other Languages Forum Timeline: Prolog [Assert & retract]
Next Thread in Legacy and Other Languages Forum Timeline: Can anyone check if my prolog solutions are correct?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC