Search code examples
windowsbatch-filefile-iocmddirectory

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?


Solution

  • 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