simple script to delete files and folders?!

  • Thread starter Thread starter biggerbyfar
  • Start date Start date
B

biggerbyfar

Hi All,

I need a script to delelte the contents of a folder periodically as an
scheduled task. Is there a simple way to delete the contents of a
folder including subfolders?

The problem with RD is that it removes the directory itself (and all
contents). I could recreate the folder again but it would loose the
security permissions which are important as this is a netowork share.

Any ideas?
 
Hi All,

I need a script to delelte the contents of a folder periodically as an
scheduled task. Is there a simple way to delete the contents of a
folder including subfolders?

The problem with RD is that it removes the directory itself (and all
contents). I could recreate the folder again but it would loose the
security permissions which are important as this is a netowork share.

Any ideas?

Try this:

Line1 @echo off
Line2 set Target=Some Folder
Line3 cd /d "%Target%"
Line4 if /i "%cd%"=="%Target%" goto Action
Line5 echo Wrong folder - program terminated.
Line6 pause
Line7 goto :eof
Line8
Line9 :Action
Line10 echo Deleting the contents of %cd%
Line11 echo del /q *.*
Line12 echo for /d %%a in (*.*) do rd /s /q "%%a"
Line13 pause

Line4 will protect you against the event that the target folder
no longer exists. Without Line4 the results would be
disastrous . . .

Remove "echo" in Lines11 & 12 to activate the batch file
 
I need a script to delelte the contents of a folder periodically as an
scheduled task. Is there a simple way to delete the contents of a
folder including subfolders?

You can do it in two operations, files and directories separately:

del /A /F /Q c:\folder\*.*
for /D %%n in (c:\folder\*) do rd /S /Q %%n
 
Robert Roland said:
You can do it in two operations, files and directories separately:

del /A /F /Q c:\folder\*.*
for /D %%n in (c:\folder\*) do rd /S /Q %%n

Your method has a little oversight: it will fail for subfolder
names with embedded spaces.
 
Your method has a little oversight: it will fail for subfolder
names with embedded spaces.

You are right. Thanks for pointing it out. I've done that mistake
countless times.

The fix is simple. It is left as an exercise for the reader :-)
 
Back
Top