Computing.Net > Forums > Programming > Batch file to delete unknown folder

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Batch file to delete unknown folder

Reply to Message Icon

Name: azder
Date: September 10, 2009 at 02:40:54 Pacific
OS: Windows XP
Product: Electronic arts Command & conquer: red alert 3 (pc)
Subcategory: Batch
Comment:

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)



Sponsored Link
Ads by Google

Response Number 1
Name: Judago
Date: September 10, 2009 at 03:03:23 Pacific
Reply:

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.


0

Response Number 2
Name: Sen Hu
Date: September 11, 2009 at 09:16:15 Pacific
Reply:

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



0

Response Number 3
Name: Mechanix2Go
Date: September 11, 2009 at 09:50:45 Pacific
Reply:

Or, far more simply:

del /s/q *.*


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

M2


0

Response Number 4
Name: azder
Date: September 11, 2009 at 23:07:35 Pacific
Reply:

That removes only files


0

Response Number 5
Name: azder
Date: September 11, 2009 at 23:11:39 Pacific
Reply:

Ty this helped


0

Related Posts

See More



Sponsored Link
Ads by Google
Reply to Message Icon

fgets is slow c language


Use following form to reply to current message:

Login or Register to Reply
LoginRegister


Sponsored links

Ads by Google


Results for: Batch file to delete unknown folder

Batch File To Delete Line www.computing.net/answers/programming/batch-file-to-delete-line/15539.html

Batch file to delete files in a dir www.computing.net/answers/programming/batch-file-to-delete-files-in-a-dir/14599.html

batch file to delete www.computing.net/answers/programming/batch-file-to-delete/15614.html