springenthusiast
springenthusiast

Reputation: 447

Windows batch command to delete folders only

I have a folder, which has files and folders inside it like

 C:/MyFolder
 C:/MyFolder/File1.txt
 C:/MyFolder/File2.txt
 C:/MyFolder/File3.sql
 C:/MyFolder/Folder1
 C:/MyFolder/Folder1/File5.txt

What batch command do I need to use to delete all the folders and contents inside them without deleting files inside my folder. Example : Delete Folder1,Folder1/File5.txt but retain File1.txt,File2.txt and File3.sql?

Upvotes: 3

Views: 3969

Answers (3)

Boof
Boof

Reputation: 43

For DOS/Command prompt use

for /d %F in ("path*") do rmdir /s /q "%F"

Use double % if you use it in a batch file.

for /d %%F in ("H:\EDGE-backup*") do rmdir /s /q "%%F"

I used this to backup the EDGE bookmarks and such, and since XCOPY always brings with it it's root directory subfolders i had to delete these after the copy.

The above worked for this. Result,only files remained in H:\EDGE-backup.

Upvotes: 1

npocmaka
npocmaka

Reputation: 57252

from command prompt:

for /f "tokens=* delims=" %a in ('dir /b /a:d "C:\someDir"') do @rd /s /q "%~fa"

from batch file:

for /f "tokens=* delims=" %%a in ('dir /b /a:d "C:\someDir"') do @rd /s /q "%%~fa"

Upvotes: 2

foxidrive
foxidrive

Reputation: 41234

This shows you the commands - if you are happy with them then remove the echo keyword and run it again.

@echo off
for /d %%a in ("C:\MyFolder\*") do echo rd "%%a" /q /s
pause

Upvotes: 4

Related Questions