1,105,375 Community Members

Delete a directory using batch script

Member Avatar
satya.vijai
Newbie Poster
9 posts since Apr 2007
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
-1
 

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?

Member Avatar
Killer_Typo
Master Poster
778 posts since Apr 2004
Reputation Points: 82 [?]
Q&As Helped to Solve: 39 [?]
Skill Endorsements: 0 [?]
 
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!

Member Avatar
Bulldog_466
Newbie Poster
1 post since Sep 2007
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
Killer_Typo
Master Poster
778 posts since Apr 2004
Reputation Points: 82 [?]
Q&As Helped to Solve: 39 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
Duoas
Postaholic
2,039 posts since Oct 2007
Reputation Points: 1,022 [?]
Q&As Helped to Solve: 229 [?]
Skill Endorsements: 10 [?]
Featured
 
0
 

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.

Member Avatar
hopalongcassidy
Junior Poster
148 posts since Oct 2007
Reputation Points: 35 [?]
Q&As Helped to Solve: 14 [?]
Skill Endorsements: 0 [?]
 
0
 

Try rd /s dirToDelete

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

Hoppy

Member Avatar
bugatha1
Newbie Poster
2 posts since Sep 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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?

Member Avatar
Duoas
Postaholic
2,039 posts since Oct 2007
Reputation Points: 1,022 [?]
Q&As Helped to Solve: 229 [?]
Skill Endorsements: 10 [?]
Featured
 
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.

Member Avatar
hmaarten
Newbie Poster
2 posts since Oct 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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!

Member Avatar
Duoas
Postaholic
2,039 posts since Oct 2007
Reputation Points: 1,022 [?]
Q&As Helped to Solve: 229 [?]
Skill Endorsements: 10 [?]
Featured
 
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

?

Member Avatar
hmaarten
Newbie Poster
2 posts since Oct 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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!

Member Avatar
Duoas
Postaholic
2,039 posts since Oct 2007
Reputation Points: 1,022 [?]
Q&As Helped to Solve: 229 [?]
Skill Endorsements: 10 [?]
Featured
 
0
 

Nice!

Member Avatar
JenniC
Newbie Poster
6 posts since Mar 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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"
You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article