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?

Recommended Answers

All 12 Replies

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

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

Try rd /s dirToDelete

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

Hoppy

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!

Nice!

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