Computing.Net > Forums > Programming > VBScript to delete all subfolders

Computing.Net: Over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to sign up now, it's free!

VBScript to delete all subfolders

Reply to Message Icon

Original Message
Name: Spike2006
Date: August 16, 2006 at 23:17:41 Pacific
Subject: VBScript to delete all subfolders
OS: Windows 2K/XP
CPU/Ram: Pentium IV 1.3
Model/Manufacturer: Compaq
Comment:

Hello,

I'm very new to scripting. I was wondering if someone could help me with a script to delete all subfolders and files within a specified folder. I have a script below which is made up of two scripts that I took from a couple of people. It works, but I have lines in there that are not necessary because I do not need to save anything from the folder. The first script erases files from the specified folder, but does not erase the subfolders or files within those subfolders. The second part erases the subfolders and files within those, but not any files that are within the specified folder.

Set fso=CreateObject("Scripting.FileSystemObject")
CleanPath="C:\Test\"

For Each file In fso.GetFolder(CleanPath).Files
file.attributes = file.attributes And Not 1
file.delete
Next

---------------End first Part---------------

Set fso = CreateObject("Scripting.FileSystemObject")
Set oFolder = fso.GetFolder("C:\Test\")

arrFolders = Array()
For Each oFolder In oFolder.SubFolders
' Note : Only use *lowercase* letters in the folder names below:
If Not LCase(oFolder.Name) = "foldera" _
And Not LCase(oFolder.Name) = "folderb" _
And Not LCase(oFolder.Name) = "folderc" Then
intCount = UBound(arrFolders) + 1
ReDim Preserve arrFolders(intCount)
arrFolders(intCount) = oFolder.Path
End If
Next

For n = 0 To UBound(arrFolders)
fso.DeleteFolder arrFolders(n), True
Next

---------------

Can someone help refine this script so that it is neater. The person the script was originally created for wanted to keep certain subfolders. Since I don't, I don't need those extra lines of code.

Thanks

Spike


Report Offensive Message For Removal


Response Number 1
Name: Michael J (by mjdamato)
Date: August 17, 2006 at 02:15:46 Pacific
Reply: (edit)

You cannot delete all "contents" of a folder in one easy step - at least not directly. The easiest way to accomplish what you are asking is to delete the target folder, thereby deleting all of it's contents, and then recreate the folder.

========BEGIN CODE==========
target = "C:\Test"

Set fso = CreateObject("Scripting.FileSystemObject")

If fso.FolderExists(target) Then
    fso.DeleteFolder target
    fso.CreateFolder target
End If
========BEGIN CODE==========

Michael J


Report Offensive Follow Up For Removal

Response Number 2
Name: Mechanix2Go
Date: August 17, 2006 at 03:33:22 Pacific
Reply: (edit)

I use w98 deltree.exe in w2k.

deltree /y somedir

[That's not what you asked, but MUCH simpler.]


If at first you don't succeed, you're about average.

M2


Report Offensive Follow Up For Removal

Response Number 3
Name: tonysathre
Date: August 17, 2006 at 13:43:40 Pacific
Reply: (edit)

I use rmdir /s

@echo off
rmdir /s /q somedir
mkdir somedir

That removes all subfolders, files, and the directory itself.

I don't see the point in writing lines and lines of code in VBSript when you can accomplish the same with 2 line of Batch scripting.

There's no place like 127.0.0.1

Visit my site Back Slash


Report Offensive Follow Up For Removal

Response Number 4
Name: Mechanix2Go
Date: August 17, 2006 at 14:42:48 Pacific
Reply: (edit)

Hi Tony,

I'm with you on 'simpler is better.

I wrote this, but was hampered by them old DOS habits. LOL

::== d2.bat
for /f %%D in ('dir/s/b/ad ^| sort /r') do (
del %%D /q
rd %%D
)
:: DONE


=====================================
If at first you don't succeed, you're about average.

M2



Report Offensive Follow Up For Removal







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home








Do you have your own blog?

Yes
No
I did before
I will soon


View Results

Poll Finishes In 4 Days.
Discuss in The Lounge
Poll History




Data Recovery Software