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?

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!

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

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

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


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

rd /s/q %1
goto newdir

deltree /y %1

md %1
goto end

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


Hope this helps.

Try rd /s dirToDelete

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


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?

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.

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!

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


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!


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 <> "")
    lex "1" $folderlist > $folder
    system -s rd "/S" $folder

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"
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.