954,523 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Delete a directory using batch script

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 del

could anyone suggest what is the script to be used, to do this?

satya.vijai
Newbie Poster
11 posts since Apr 2007
Reputation Points: 10
Solved Threads: 0
 

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!

Killer_Typo
Master Poster
781 posts since Apr 2004
Reputation Points: 152
Solved Threads: 39
 

DEL will not remove directorys

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

DELTREE - will delete and all subdirectorys even if it contains files

Bulldog_466
Newbie Poster
1 post since Sep 2007
Reputation Points: 10
Solved Threads: 0
 
DEL will not remove directorys use RD - only works if is empty (ie. no files in it) DELTREE - will delete and all subdirectorys even if it contains files



RD /S will achieve the same as DELTREE

Killer_Typo
Master Poster
781 posts since Apr 2004
Reputation Points: 152
Solved Threads: 39
 

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.

Duoas
Postaholic
2,043 posts since Oct 2007
Reputation Points: 1,140
Solved Threads: 229
 

Try rd /s dirToDelete

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

Hoppy

hopalongcassidy
Junior Poster
148 posts since Oct 2007
Reputation Points: 53
Solved Threads: 13
 
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?

bugatha1
Newbie Poster
2 posts since Sep 2009
Reputation Points: 10
Solved Threads: 0
 

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.

Duoas
Postaholic
2,043 posts since Oct 2007
Reputation Points: 1,140
Solved Threads: 229
 

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!

hmaarten
Newbie Poster
2 posts since Oct 2009
Reputation Points: 10
Solved Threads: 0
 

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

?

Duoas
Postaholic
2,043 posts since Oct 2007
Reputation Points: 1,140
Solved Threads: 229
 

that's what I ment indeed.

Meanwhile I solved the problem, using this bat-file:
call dellDir.cmd john
call dellDir.cmd Jill
...

dellDir.cmd was one line of code: rd /q /s c:\D..\%1\Cookies

the bat-file was built with an excell-sheet, I already had the names in a file which made things a lot easier!

hmaarten
Newbie Poster
2 posts since Oct 2009
Reputation Points: 10
Solved Threads: 0
 

Nice!

Duoas
Postaholic
2,043 posts since Oct 2007
Reputation Points: 1,140
Solved Threads: 229
 

If you don;t want to hard code the user names john, jill, etc, here is a script that will delete all "cookies" folders from /users for all users.

# Script DeleteCookies.txt
var str folderlist, folder
cd /users
lf -r -n "*cookie*" "." ($ftype=="d") > $folderlist
while ($folderlist <> "")
do
    lex "1" $folderlist > $folder
    system -s rd "/S" $folder
done

Script is in biterscripting ( http://www.biterscripting.com ) . Copy and paste the script code into file C:/Scripts/DeleteCookies.txt. Execute it by entering the following command.

script "C:/Scripts/DeleteCookies.txt"
JenniC
Newbie Poster
6 posts since Mar 2009
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You