none
Create Batch file to delete files and subfolders, but not root folder

    Question

  • 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.
    Tuesday, March 08, 2011 7:42 PM

Answers

  • 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
    Tuesday, March 08, 2011 8:05 PM

All replies

  • 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


    Tuesday, March 08, 2011 7:57 PM
    Moderator
  • 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
    Tuesday, March 08, 2011 8:05 PM
  • 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!

    Wednesday, March 09, 2011 2:28 PM
  • That's exactly what mine does (rd /q /s .).

    Bill

    Wednesday, March 09, 2011 10:10 PM
    Moderator
  • No worries - just wanted to point out the similarity - two different ways of saying the same thing.

    Bill

    Wednesday, March 09, 2011 10:43 PM
    Moderator
  • 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

    Wednesday, March 16, 2011 1:43 PM
  • 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

    Wednesday, March 16, 2011 2:15 PM
    Moderator
  • 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

    Wednesday, March 16, 2011 2:47 PM
  • 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

    Wednesday, March 16, 2011 2:50 PM
    Moderator
  • Thanks for the solution
    Thursday, April 14, 2011 9:15 AM
  • 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

    Thursday, January 19, 2012 3:24 PM
  • Hi,

    Your question is already answered in this thread.

    Bill

    Thursday, January 19, 2012 3:34 PM
    Moderator
  • 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?

    Thursday, September 13, 2012 8:40 AM
  • 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.

    Grant Ward, a.k.a. Bigteddy

    Thursday, September 13, 2012 9:03 AM
  • how desktop files deleted instead of given path.
    Thursday, September 13, 2012 11:26 AM
  • 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.


    ¯\_(ツ)_/¯

    Thursday, September 13, 2012 11:48 AM
  • 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
    Wednesday, October 10, 2012 2:33 PM
  • 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.
    Wednesday, October 10, 2012 3:06 PM
  • That's not the case with the shell script (batch file) I posted above, which IMO is a more robust solution.

    Bill

    Wednesday, October 10, 2012 3:25 PM
    Moderator
  • hiii how to use.is it use for bat file or what 
    Thursday, October 31, 2013 6:20 AM
  • Hi dude ,

    Thanks it rily works

    Tuesday, March 18, 2014 4:37 AM
  • 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
    Tuesday, March 03, 2015 10:03 PM
  • This needs to be taken down. Re: stillstill.

    Not an optimal way to do this.

    Thursday, March 03, 2016 8:59 PM