Create Batch file to delete files and subfolders, but not root folder
set folder="C:\test" cd /d %folder% for /F "delims=" %%i in ('dir /b') do (rmdir "%%i" /s/q || del "%%i" /s/q)
  • Marked as answer by ALOTFI Wednesday, March 09, 2011 2:28 PM
March 8th, 2011 8:05pm

Good evening,

Does anyone know an efficient way of deleting files and subfolders in a shared folder, but not deleting the root folder?

my goal is to create a batch file that starts every Friday evening as scheduled task which deletes the contents of a shared folder (files and subfolder) without removing the root directory, the fact of using the command "md" will not return the same access rights.

Thank you in advance for your assistance.
Free Windows Admin Tool Kit Click here and download it now
March 8th, 2011 10:42pm

Hi,

Here is a shell script (.cmd batch file) I have used for a number of years:

@echo off
setlocal enableextensions
if {%1}=={} goto :HELP
if {%1}=={/?} goto :HELP
goto :START

:HELP
echo Usage: %~n0 directory-name
echo.
echo Empties the contents of the specified directory,
echo WITHOUT CONFIRMATION. USE EXTREME CAUTION!
goto :DONE

:START
pushd %1 || goto :DONE
rd /q /s . 2> NUL
popd

:DONE
endlocal

HTH,

Bill

March 8th, 2011 10:57pm

Hi,

Here is a shell script (.cmd batch file) I have used for a number of years:

Free Windows Admin Tool Kit Click here and download it now
March 8th, 2011 10:57pm

set folder="C:\test" cd /d %folder% for /F "delims=" %%i in ('dir /b') do (rmdir "%%i" /s/q || del "%%i" /s/q)
  • Marked as answer by ALOTFI Wednesday, March 09, 2011 2:28 PM
March 8th, 2011 11:05pm

set folder="C:\test" cd /d %folder% for /F "delims=" %%i in ('dir /b') do (rmdir "%%i" /s/q || del "%%i" /s/q)
  • Marked as answer by ALOTFI Wednesday, March 09, 2011 2:28 PM
Free Windows Admin Tool Kit Click here and download it now
March 8th, 2011 11:05pm

Hi,

Thank you very much!! I tested it and it works perfectly! I still have to program it as a scheduled task.

good day everyone!

March 9th, 2011 5:28pm

That's exactly what mine does (rd /q /s .).

Bill

Free Windows Admin Tool Kit Click here and download it now
March 10th, 2011 1:10am

So it does! Sorry, I overlooked this - I will now delete my "contribution".
March 10th, 2011 1:37am

No worries - just wanted to point out the similarity - two different ways of saying the same thing.

Bill

Free Windows Admin Tool Kit Click here and download it now
March 10th, 2011 1:43am

Bill,

How would you modify your script to use it with Windows Scheduler and to use it on a network share?  I have a folder that I want to delete the contents in every night at midnight with the path of \\crdr661a.cr.trapezeits.com\didk5004 (DIDk5004 is the dfs share name).  The name of the folder associated to the dfs share is Xfer.  Any help would be appreciated.  Thank you.

 

Vince

March 16th, 2011 4:43pm

Hi,

My script is written so it accepts a command-line argument. If you save the script as CleanDir.cmd, then schedule the script as follows (for example):

C:\Scripts\CleanDir.cmd \\crdr661a.cr.trapezits.com\didk5004

Of course the scheduled task would need to run using an account that has sufficient permissions.

HTH,

Bill

Free Windows Admin Tool Kit Click here and download it now
March 16th, 2011 5:15pm

Sorry Bill.  A little confused.  Where do I put the "C:\Scripts\CleanDir.cmd \\crdr661a.cr.trapezits.com\didk5004"?  I place that in the script?  If so where does it go in your script?  Sorry, haven't dealt much with scripting so quite a noob.

 

Vince

March 16th, 2011 5:47pm

Hi,

No. The command

C:\Scripts\CleanDir.cmd \\crdr661a.cr.trapezits.com\didk5004

...is what you would schedule as a scheduled task (of course, assuming you put the CleanDir.cmd script file in C:\Scripts).

If you're not familiar with the task scheduler, you should ask about it in an appropriate forum.

Bill

Free Windows Admin Tool Kit Click here and download it now
March 16th, 2011 5:50pm

Thanks for the solution
April 14th, 2011 12:15pm

Hello,

I'm not sure where I would specify the path to the folder within your script? Is this script to be named with a .bat extension. Does the script need to be located someplace as defined by the 'path' environment variable?

 

thanks

 

Dan

Free Windows Admin Tool Kit Click here and download it now
January 19th, 2012 6:24pm

Hi,

Your question is already answered in this thread.

Bill

January 19th, 2012 6:34pm

Hi,

sir i have used this syntax but desktop items get deleted instead of deleting file from folder from c:\program files\pointofsale

and nw i want those files back.how to get it nw?

Free Windows Admin Tool Kit Click here and download it now
September 13th, 2012 11:40am

Hi,

sir i have used this syntax but desktop items get deleted instead of deleting file from folder from c:\program files\pointofsale

and nw i want those files back.how to get it nw?


You can't.  They are deleted for good.
September 13th, 2012 12:03pm

how desktop files deleted instead of given path.
Free Windows Admin Tool Kit Click here and download it now
September 13th, 2012 2:26pm

how desktop files deleted instead of given path.

Sorry but this thread has been clsoed for over a year.  Please start a new topic with your complete question.

September 13th, 2012 2:48pm

Just be careful setting the folder: if cd is unable to change patch to the %folder% the script will nicely delete all the current folder content.
  • Edited by stillstill Wednesday, October 10, 2012 2:34 PM
Free Windows Admin Tool Kit Click here and download it now
October 10th, 2012 2:33pm

Just be careful setting the folder: if cd is unable to change patch to the %folder% the script will nicely delete all the current folder content.
  • Edited by stillstill Wednesday, October 10, 2012 2:34 PM
October 10th, 2012 5:33pm

Just be careful setting the folder: if cd is unable to change patch to the %folder% the script will nicely delete all the current folder content.
  • Edited by stillstill Wednesday, October 10, 2012 2:34 PM
Free Windows Admin Tool Kit Click here and download it now
October 10th, 2012 5:33pm

Explanation is as I said above:Just be careful setting the folder: if cd is unable to change patch to the %folder% the script will nicely delete all the current folder content.
October 10th, 2012 6:06pm

That's not the case with the shell script (batch file) I posted above, which IMO is a more robust solution.

Bill

Free Windows Admin Tool Kit Click here and download it now
October 10th, 2012 6:25pm

hiii how to use.is it use for bat file or what 
October 31st, 2013 2:24am

Hi dude ,

Thanks it rily works

Free Windows Admin Tool Kit Click here and download it now
March 18th, 2014 12:40am

OMG i need super help I did this and everyhing got deleted. I just wanted to delete the inside the path not all of the root file
March 3rd, 2015 10:03pm

OMG i need super help I did this and everyhing got deleted. I just wanted to delete the inside the path not all of the root file

This thread with 25 replies is now 4 years old and is marked as answered. If you have a problem then you should start your own thread, describe your problem, report what you did and formulate an actual question.
Free Windows Admin Tool Kit Click here and download it now
March 3rd, 2015 11:18pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics