Delete a directory using batch script

Reply

Join Date: Apr 2007
Posts: 9
Reputation: satya.vijai is an unknown quantity at this point 
Solved Threads: 0
satya.vijai satya.vijai is offline Offline
Newbie Poster

Delete a directory using batch script

 
-1
  #1
Aug 23rd, 2007
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?
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 759
Reputation: Killer_Typo will become famous soon enough Killer_Typo will become famous soon enough 
Solved Threads: 35
Killer_Typo's Avatar
Killer_Typo Killer_Typo is offline Offline
Master Poster

Re: Delete a directory using batch script

 
0
  #2
Sep 7th, 2007
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!
Dont forget to spread the reputation to those that deserve!
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 1
Reputation: Bulldog_466 is an unknown quantity at this point 
Solved Threads: 0
Bulldog_466 Bulldog_466 is offline Offline
Newbie Poster

Re: Delete a directory using batch script

 
0
  #3
Sep 27th, 2007
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
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 759
Reputation: Killer_Typo will become famous soon enough Killer_Typo will become famous soon enough 
Solved Threads: 35
Killer_Typo's Avatar
Killer_Typo Killer_Typo is offline Offline
Master Poster

Re: Delete a directory using batch script

 
0
  #4
Sep 27th, 2007
Originally Posted by Bulldog_466 View Post
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
Dont forget to spread the reputation to those that deserve!
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,951
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 214
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: Delete a directory using batch script

 
0
  #5
Oct 19th, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 147
Reputation: hopalongcassidy is an unknown quantity at this point 
Solved Threads: 13
hopalongcassidy's Avatar
hopalongcassidy hopalongcassidy is offline Offline
Junior Poster

Re: Delete a directory using batch script

 
0
  #6
Oct 23rd, 2007
Try rd /s dirToDelete

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

Hoppy
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 2
Reputation: bugatha1 is an unknown quantity at this point 
Solved Threads: 0
bugatha1 bugatha1 is offline Offline
Newbie Poster

Re: Delete a directory using batch script

 
0
  #7
Oct 2nd, 2009
Originally Posted by Killer_Typo View Post
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?
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,951
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 214
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: Delete a directory using batch script

 
0
  #8
Oct 5th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 2
Reputation: hmaarten is an unknown quantity at this point 
Solved Threads: 0
hmaarten hmaarten is offline Offline
Newbie Poster
 
0
  #9
Oct 8th, 2009
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!
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,951
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 214
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso
 
0
  #10
Oct 10th, 2009
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
?
Reply With Quote Quick reply to this message  
Reply

Message:



Similar Threads
Other Threads in the Legacy and Other Languages Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC