Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete all files and folders in a folder by cmd call

I use Windows.

I want to delete all files and folders in a folder by system call.

I may call like that:

>rd /s /q c:\destination >md c:\destination 

Do you know an easier way?

like image 270
ufukgun Avatar asked Oct 01 '09 09:10

ufukgun


People also ask

How delete all files and folders using CMD?

To delete all of the files in the current directory, press Y and then press ENTER. To cancel the deletion, press N and then press ENTER. Before you use wildcard characters with the del command, use the same wildcard characters with the dir command to list all the files that will be deleted.

How do I delete files and subfolders in CMD?

Run the command RMDIR /Q/S foldername to delete the folder and all of its subfolders.

How do I delete all files and folders in a folder?

To remove a directory and all its contents, including any subdirectories and files, use the rm command with the recursive option, -r . Directories that are removed with the rmdir command cannot be recovered, nor can directories and their contents removed with the rm -r command.


2 Answers

No, I don't know one.

If you want to retain the original directory for some reason (ACLs, &c.), and instead really want to empty it, then you can do the following:

del /q destination\* for /d %x in (destination\*) do @rd /s /q "%x" 

This first removes all files from the directory, and then recursively removes all nested directories, but overall keeping the top-level directory as it is (except for its contents).

Note that within a batch file you need to double the % within the for loop:

del /q destination\* for /d %%x in (destination\*) do @rd /s /q "%%x" 
like image 169
Joey Avatar answered Sep 23 '22 06:09

Joey


del c:\destination\*.* /s /q worked for me. I hope that works for you as well.

like image 40
Sean Avatar answered Sep 21 '22 06:09

Sean