Akshay
Akshay

Reputation: 1817

How to delete files recursively

I have the directory structure /foo/bar/fooBar/.. . I want to write a Windows command where I can mention the path till foo directory and it deletes all the files and directory recursively in /foo, but it should NOT delete the foo directory.

I have been using rmdir /q /s [path to foo] but this command deletes the foo directory as well. Let me know if there is any command(s) to accomplish this.

Upvotes: 35

Views: 99474

Answers (9)

Rosberg Linhares
Rosberg Linhares

Reputation: 3687

Try to use Powershell:

powershell -Command "Remove-Item '\foo\*' -Recurse -Force"

Upvotes: 1

user7415858
user7415858

Reputation: 1

To prevent deleting of the foo directory try change directory to foo prior to the delete such as:

cd c:\foo rd /s /q c:\foo

This will delete all the files and folders under foo but NOT foo. An error message will be displayed as follow "The process cannot access the file because it is being used by another process."

Upvotes: 0

user1743310
user1743310

Reputation:

del X /f /s /q 
rd X /s /q 

this WILL remove the ROOt directory though. make it again with

 md X

or make a copy of it first.

otherwise you'll have to do batch funkiness

 dir X /ad /b

will give you a list of the immediate subdirectories of X. you can work out the rest

Upvotes: 5

fup
fup

Reputation: 31

I was looking for a simple command to delete all files in a directory recursively but leaving the directory structure unchanged. So, maybe this could be interesting ;)

for /f "delims=" %i in ('dir /B /S /A:-DH') do @del /F /Q /A:H "%i"

The command 'dir /B /S /A:-D' lists only files (/A:-D) in current directory recursively (/S) without 'dir' summary report (/B). The 'for' loops through each full line (/delims=) and executes the delete command, forced and quiet. I additionally used the hidden flag (/H) both for listing and deletion for some mysterious (e.g. thumbs.db) files.

Upvotes: 3

Neil
Neil

Reputation: 61

I had been scratching my head on this one as well. It is easy enough to create a for loop that uses rmdir however it leaves behind folders that have spaces in the long names. It is possible to manipulate a dir list and get the 8.3 filenames however here is a much simpler solution.

Create an empty folder then;

robocopy \empty_folder \folder_with_sub_folders /PURGE

All subfolders & files will be deleted.

Upvotes: 6

Jim Cubbage Jr
Jim Cubbage Jr

Reputation: 21

I have used this in a batch file in the past. It uses a for loop to navigate the directory structure.

Here I remove the cvs sub directories off of a tree, needed when copying from one branch to another.

@echo off
if /I exist CVS. rd CVS /s /q >nul
for /F %%z in ('dir cvs /ad /s /b') do echo %%z && rd /s /q %%z
echo Batchfile %0 is complete

Upvotes: 1

Libish Jacob
Libish Jacob

Reputation: 308

del /f /s /q DirectoryWhichContainsFilesToDelete/\*

This will delete all files in the folder DirectoryWhichContainsFilesToDelete without deleting the folder itself. Have fun :)

Upvotes: 9

Ferruccio
Ferruccio

Reputation: 100748

rd /s /q /path/to/foo
md /path/to/foo

Upvotes: 49

Dan Paradox
Dan Paradox

Reputation: 1380

deltree /foo/* should work fine.

Upvotes: 2

Related Questions