Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Script to delete all folders EXCEPT ones I specify? 1

Status
Not open for further replies.

Chambers

IS-IT--Management
Jan 19, 2001
257
US
Hi guys, I want to create a script that will delete all folders in a given directory EXCEPT ones I choose. Basically I need a script that will delete everything in C:\documents and setting\ except "default user" "all users" administrator" Is there any easy way to do this? Thanks!
 
Take a look at FileSystemObject and come back with what you have so far.

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
function DeleteFolder(fdirDEL)
Set objFso = CreateObject("Scripting.FileSystemObject")
if objFso.FolderExists(fdirDEL) Then
objFso.DeleteFolder(fdirDEL)
End if
Set objFso = nothing
End function

I have that to delete a folder, but i don't know how to tell it all folders except "these ones
 
Take a look at the Subfolders collection.

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
... taken and modified from
note: I added a couple more folders to the GoodFolders list and commented out the actual delete untill you can test it.

:) -Andrew

Dim GoodFolders
GoodFolders = Array("Default User","All Users","Administrator","LocalService","NetworkService")

Call ShowFolderList("C:\Documents and Settings")

Sub ShowFolderList(folderspec)
Dim fso, f, f1, fc, i, delete
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.GetFolder(folderspec)
Set fc = f.SubFolders
For Each f1 in fc
delete = True
For i = 0 to UBound(GoodFolders)
If f1.name = GoodFolders(i) Then delete = False
Next
If delete = True Then msgbox(f1.name)'f1.delete
Next
End Sub

alien.gif

[TAG]
anongod@hotmail.com
'Drawing on my fine command of language, I said nothing.'
 
Hey, that worked like a charm! Thanks so much AnonGod!!
 
Hey AnonGod, everything works great except sometimes I get an permission denied error. However if I deleted them from explorer I don't get an error. Any ideas?
 
Try this?
[tt] If delete = True Then f1.delete [blue]true[/blue][/tt]
 

Get all subfolders from which you want to delete. in your case it is C:\documents and setting\ and try following loop

set objFolders = objFSO.GetFolder(folderspec).Subfolders
for each objFolder in objFolders
.
.
Check objfolder name if it is one which you want to keep then skip else delete that folder
.
.

next

I beleive this will help you . . .

Thanks,

Sajid A
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top