none
Delete Folder Content

    Question

  • Hi! I need to regularily delete the content of a folder including the folders within and without deleting the folder itself. The "del"-Command leaves the empty folders behind and "deltree" doesnt work anymore on Windows Server 2008 R2. Is there an easy way to achieve this? Thanks

    Wednesday, April 21, 2010 7:10 AM

Answers

All replies

  • Hi,

    A script should be able to handle your requirements but short of reinventing the wheel, RD or RMDIR (with the /S switch) should also be able to do that.

    Removes (deletes) a directory.

    RMDIR [/S] [/Q] [drive:]path
    RD [/S] [/Q] [drive:]path

        /S      Removes all directories and files in the specified directory
                in addition to the directory itself.  Used to remove a directory
                tree.

        /Q      Quiet mode, do not ask if ok to remove a directory tree with /S

    Regards,

    Salvador Manaois III
    MCSE MCSA MCTS MCITP:EA/SA C|EH CIWA
    ----------------------------------------------------------------------------
    Bytes & Badz: http://badzmanaois.blogspot.com
    My Passion: http://flickr.com/photos/badzmanaois
    My Scripting Blog: http://sgwindowsgroup.org/blogs/badz

     

    Wednesday, April 21, 2010 8:30 AM
    Moderator
  • Yes RD can be used and a script can be an overkill. But if you still need a PowerShell one-liner to do that

    Remove-Item -Path C:\Folder -Recurse -Force  <= This will delete C:\folder also

    Get-ChildItem C:\Folder | Remove-Item -Force <= This will just empty C:\folder but leave the parent


    Ravikanth
    http://www.ravichaganti.com/blog
    Twitter: @ravikanth
    PowerShell 2.0 remoting - eBook
    Wednesday, April 21, 2010 11:03 AM
    Moderator
  • Thank you very much for the quick answers. I will test this as soon as possible.

    Regards

    Jürgen Winter

    Friday, April 23, 2010 1:16 PM