Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete files/subfolders in a specific directory at the command prompt in Windows

Say, there is a variable called %pathtofolder%, as it makes it clear it is a full path of a folder.

I want to delete every single file and subfolder in this directory, but not the directory itself.

But, there might be an error like 'this file/folder is already in use'... when that happens, it should just continue and skip that file/folder.

Is there some command for this?

like image 721
Deniz Zoeteman Avatar asked Dec 27 '09 11:12

Deniz Zoeteman


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.

Can I delete a folder using CMD?

To delete folders in Windows 10 with CMD you have to use the rmdir command. This command will remove all folders including the subfolders and files in them.


2 Answers

rmdir is my all time favorite command for the job. It works for deleting huge files and folders with subfolders. A backup is not created, so make sure that you have copied your files safely before running this command.

RMDIR "FOLDERNAME" /S /Q 

This silently removes the folder and all files and subfolders.

like image 91
Suresh Avatar answered Oct 04 '22 19:10

Suresh


You can use this shell script to clean up the folder and files within C:\Temp source:

del /q "C:\Temp\*" FOR /D %%p IN ("C:\Temp\*.*") DO rmdir "%%p" /s /q 

Create a batch file (say, delete.bat) containing the above command. Go to the location where the delete.bat file is located and then run the command: delete.bat

like image 29
Iain Avatar answered Oct 04 '22 17:10

Iain