Windows 7 empty entire directory from command line
Solution 1
You should use rmdir /S. Check out this link
Solution 2
I think what you are looking for is to clear the folder not to delete it and recreate it. This will actualy clear the folder so you don't have to recreate it.
CD "C:/Program Files (x86)/Apache Software Foundation/Tomcat 6.0/abtapps/ROOT" && FOR /D %i IN ("*") DO RD /S /Q "%i" && DEL /Q *.*
A few notes.
You only use a single % when running from the command line. doubles are for batch files.
&& is for running multiple commands on the same line.
* The &&'s are especially important in this case because you do not want the del /q . to run if the cd fails as you will delete to contents of what ever directory you started in.
Solution 3
Try this:
forfiles /p "folder" /s /c "cmd /c rd /q @path
replace "folder" with your folder name or drive letter
example: forfiles /p f: /s /c "cmd /c rd /q @path
This will recurse through all subfolders, and only remove the empty folders. If you use it in command line, you'll get error messages for folders that aren't empty. If you put it in a batch script, it will take some time to run, depending on how many folders you have.
Mechlar
I am a senior software developer and team/project manager. I develop with Javascript, Angular, React, NodeJS, Firebase, jQuery, CSS, Bootstrap, HTML5, etc, etc.
Updated on June 17, 2022Comments
-
Mechlar almost 2 years
I am running a batch file which deploys a war file to a specific directory. However, I want to empty that directory before deploying to it, recursively.
I found this script that is supposed to remove everything in the directory including folders and files but I get this error: "%%i was unexpected at this time."
FOR /D %%i IN ("C:/Program Files (x86)/Apache Software Foundation/Tomcat 6.0/abtapps/ROOT/*") DO RD /S /Q "%%i" DEL /Q "C:/Program Files (x86)/Apache Software Foundation/Tomcat 6.0/abtapps/ROOT/*.*"
I just need a simple way to recursively empty my ROOT directory either by fixing the script above or with another method. Have to be able to do this from a batch file.
I am running windows 7.
SOLUTION
rmdir ROOT /s /q mkdir ROOT
-
Mechlar about 13 yearsyeah, tried that but it doesn't work cause you have to specify a directory and it removes it, and to run this per directory in ROOT would be a waste of time, I have over 200 directories in ROOT. "/s : Removes the specified directory and all subdirectories including any files. Use /s to remove a tree." - from microsoft.com/resources/documentation/windows/xp/all/proddocs/…
-
Mechlar about 13 yearswhen I run just rmdir /s it gives me an error saying "Incorrect syntax". But I figured I could run rmdir ROOT /s /q then mkdir ROOT
-
Rich about 13 yearsUse
cd /d
to also change the directory if you're on a different drive. Or just usepushd
/popd
so you're in the same location afterwards again.