Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

How to recursively delete files but keep the directory structure through the Windows command prompt?

The other day, I had to clear a structure of hundreds of folders containing over 60Gb worth of logs that we moved to an archive file server. After copying the logs to the archive location, I had to delete all the files while keeping the folder structure. Being accustomed to high level languages for too long, I first wondered how to do this in a batch file. It turns out that the Forfiles Command would do the task rather simply and elegantly:

forfiles /p D:\Archive\ /s /c “cmd /c IF @isDir EQU FALSE (del /Q /F @file)”

forfiles parameters:
/p -> Path in which the Forfiles Command will search for files.
/s -> Orders the forfiles command to execute recursively in all subdirectories.
/c -> Command to execute for each file found by forfiles. The command must be in quotes and start with cmd /c.

As forfiles returns all files and folders found within the given path, I simply had to check that the file found is not a directory before deleting it silently using the /Q parameter (the /F parameter forces the deletion of read only files).

For the 60Gb worth of data I had, the command ran for a good 15 minutes and the job was done (no heavy CPU usage or anything else noticeable so it is safe to use even on production environment).

The post How to Recursively Delete Files but keep the directory structure through the Windows command prompt? appeared first on Francois Malgreve - .Net, BizTalk, Azure - Bangkok, Brussels, Dublin.

Share the post

How to recursively delete files but keep the directory structure through the Windows command prompt?

×

Subscribe to Francois Malgreve - .net, Biztalk, Azure - Bangkok, Brussels, Dublin - About Biztalk, .net, Thoughts And Opinions.francois Malgreve – .net, Biztalk, Azure – Bangkok, Brussels, Dublin | About Biztalk, .net, Thoughts And Opinions.

Get updates delivered right to your inbox!

Thank you for your subscription

×