M HYPE SPLASH
// news

How to empty a folder in a command prompt? (Windows7)

By John Peck

Need to empty a folder in Windows 7 from the command prompt or batch file. This means deleting all the files and all sub folders and leaving the empty folder.del /s /q leaves empty subfolders so this solution doesn't work for me. I don't want to delete and recreate the folder either.

3

5 Answers

You can use the sdelete (Secure delete) command to clean a folder.

sdelete -s *

from with in said folder to clear all of the contents.

2

This command will empty the content of the current folder:

rmdir . /s /q

The only drawback is that there will be an error:

The process cannot access the file because it is being used by another process.

This could be avoided by adding a 2>nul at the end of the command:

rmdir . /s /q 2>nul

Try the RMDIR (or aka the RD) command.

RMDIR [/S] [/Q] [drive:]path

RD [/S] [/Q] [drive:]path

/S Removes all directories and files in the specified directory in addition to the directory itself. Used to remove a directory tree.

/Q Quiet mode, do not ask if ok to remove a directory tree with /S

1

In a batch file:

for /D %%p IN (*) do rmdir /S /Q %%p
for %%p in (*) do del %%p

See the following commands:

It will delete the whole folder:

rmdir /s /q C:\FolderName\

It will create the empty folder:

mkdir C:\FolderName\
1

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy