Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
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)

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.

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+"\"") doneScript 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

Or, far more simply:
del /s/q *.*
=====================================
Helping others achieve escape felicityM2

![]() |
fgets is slow
|
c language
|
| Login or Register to Reply | |
| Login | Register |
| Ads by Google |