tags:

views:

388

answers:

2

Can anyone here help me out with a script that will let me, when run, delete all the folders and their contents in whatever folder it is placed in.

+3  A: 

What operating system? Do you want to remove files in the current directory also?

Under cmd.exe in Windows, for files and folders, you can run

del /s /q *
rmdir /s /q *

or to remove just folders and their contents,

for /d %d in (*.*) do rmdir /s /q %d

Under most Linux/UNIX shells, to delete files and folders, you can run

rm -rf *

or as pointed out below by derobert (and tidied up a little), you can do just folders and their contents with

find . -maxdepth 1 -not -name '.' -type d -exec rm -rf \{\} \;

This will find all the directories in the current directory (maxdepth 1) excluding the current directory '.', and run rm -rf on each of them.

crb
Ummm, he asked to delete /folders/ not folders AND FILES.
derobert
... to be specific, given a folder with a subfolder A and a file B, he asked to delete A (and its contents), but not B.
derobert
The originally-worded question didn't actually spell that out explicitly, or give any detail. Your approach for that case is great though.
crb
+1  A: 

On Unix, you can do something like this:

find -type d -maxdepth 1 -not -name '.' -print0 | xargs -0 rm -Rf

This will get rid of all folders (and their contents) in the current working directory, leaving only the files not inside a folder. Given:

test/folder1
test/folder1/file1
test/file2

if you run it in test, then only file2 will be left.

derobert