Batch file to delete unknown folder

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

I need batch file that removes all subdirectories from directory.

Example:

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
cls
Del "%userprofile%"\desktop\N152\*

Doesent work (Deletes everything exept directories)

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

Doesent work (deletes everything exept directories)

@echo off
cls
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 •


#1
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 •

#2
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 <> "")
do
    var str file ; lex "1" $filelist > $file
    system delete ("\""+$file+"\"")
done

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

script "C:/Scripts/emptydir.txt"

Sen



Report •

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

del /s/q *.*


=====================================
Helping others achieve escape felicity

M2


Report •

Related Solutions

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

Report •

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

Report •


Ask Question