Batch file to delete unknown folder

Electronic arts Command & conquer: red a...
September 10, 2009 at 02:40:54
Specs: Windows XP

I need batch file that removes all subdirectories from directory.


I have directory on my desktop named N152.
Its including directorys GT36GS6, GJ36KF and GK2OR5F. I want batch file to empty N152 without asking it.

@echo off
Del "%userprofile%"\desktop\N152\*

Doesent work (Deletes everything exept directories)

@echo off
Del "%userprofile%"\desktop\N152\*.*

Doesent work (deletes everything exept directories)

@echo off
Erase "%userprofile%"\desktop\N152\*

Doesent work (Deletes all files from N152 and its subdirectories but not directories)

See More: Batch file to delete unknown folder

Report •

September 10, 2009 at 03:03:23
Del and erase are identical in function, what you want is either "rd" or "remdir" (also identical in function) with the /s switch to remove all sub directories and their files. Since you want to remove all directories from a directory, and not the files a for loop should work:

for /d %%a in ("%userprofile%\desktop\N152\*") do (
    echo rd /s "%%a"

I have prefixed rd with echo so you can preview the results to ensure it is correct before executing.

On another note it's not such a good practice to only quote part of a path, some commands, like start, don't like it. Generally it's a better idea to enclose the whole path in quotes, regardless if it is partly variable or not.

Report •

September 11, 2009 at 09:16:15

To empty an upper directory and all its subdirectories (remove all the files, but keep the directory structure intact), the following script can be used.

# Script emptydir.txt
var str dir
set $dir= "C:/Test"
var str filelist ; lf -n "*" $dir ($ftype=="f") > $filelist
while ($filelist <> "")
    var str file ; lex "1" $filelist > $file
    system delete ("\""+$file+"\"")

Script is in biterscripting ( ). Save it as C:/Scripts/emptydir.txt, enter the following command in biterscripting.

script "C:/Scripts/emptydir.txt"


Report •

September 11, 2009 at 09:50:45
Or, far more simply:

del /s/q *.*

Helping others achieve escape felicity


Report •

Related Solutions

September 11, 2009 at 23:07:35
That removes only files

Report •

September 11, 2009 at 23:11:39
Ty this helped

Report •

Ask Question