Jump to content

VB Script deleting folders


yzharovsky

Recommended Posts

Greetings All

I am running a VB script to delete folders which are not "Administrator" "All Users" or "Default Users"

The Script works fine in every folder except for "Documents and Settings"

In "Test" directories it will delete everything except for the folders listed above; as it should.

However once I run the scrip against directory "Documents and Settings" the script will generate an error if there are folders which start with the letter "M" or any letter there after. (Please note that you can create a folder starting with the letter M in Documents and Settings and you will get the error, it seems that the error has nothing to do with profile folders but just folder names.)

Example " folder with the letter "A" such as "Aaaa" will be deleted however folder "Mmmmm" or "mmmb" or "mom" will not be delete and the script will generate an error of "Access Denied" ( There is no error or problem against locked folders such as Administrator )

I also did some tests and figured out that it’s not even the letter "M" that is the issue, the issue starts with folder name "Loo" So folder name "L" will be deleted while folder "Lz" will give me an error.

Here is the script

You can look at it and even try it yourself.

I know Documents and Settings folder is very special, what I want to know is how and what change is made to Documents and Settings vs other folders.

I did get my script to work, by inserting a "Resume on Error" command and it does then delete all the folders including the ones starting with M.

But I still am very curious.

Cheers

Yevgeny

This one will generate the error in Documents and Settings I commented out the part that fixes the error.

Set fso=CreateObject("Scripting.FileSystemObject")

CleanPath="C:\Documents and Settings\"

For Each file In fso.GetFolder(CleanPath).Files

file.attributes = file.attributes And Not 1

file.delete

Next

Set fso = CreateObject("Scripting.FileSystemObject")

Set oFolder = fso.GetFolder("C:\Documents and Settings\")

arrFolders = Array()

For Each oFolder In oFolder.SubFolders

' Note : Only use *lowercase* letters in the folder names below:

If Not LCase(oFolder.Name) = "administrator" _

And Not LCase(oFolder.Name) = "all users" _

And Not LCase(oFolder.Name) = "default user" Then

intCount = UBound(arrFolders) + 1

ReDim Preserve arrFolders(intCount)

arrFolders(intCount) = oFolder.Path

End If

Next

For n = 0 To UBound(arrFolders)

' On Error Resume Next

fso.DeleteFolder arrFolders(n), True

Next

Edited by yzharovsky
Link to comment
Share on other sites


First of all I'd expect something like this to work!

Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oFolder = oFSO.GetFolder("C:\Documents and Settings")

For Each Profile In oFolder.SubFolders
Select Case UCase(Profile.Name)
Case "ADMINISTRATOR", "ALL USERS", "DEFAULT USER"
'Do nothing
Case Else
oFSO.DeleteFolder Profile.Path, True
End Select
Next

You would of course need to be logged in as Administrator!

Second of all I'd suggest you use a specific utility such as Microsofts Resource Kits tool, DelProf, for such a task.

Link to comment
Share on other sites

Greetings Yzöwl

Thank you for your reply

I have ran your script and wanted to let you know that it too errors out in Documents and Settings on folders starting with the letter "mz".

I am running this script on my local machine and I am logged in as Administrator, also keep in mind that the folder starting with mz was created by me and has nothing inside the folder (the folder is empty)

You can easily try your script on your own machine, just make sure you are pointing it to Documents and Settings because the script does work in any other folder.

Thank you again for your reply

Cheers

Yevgeny

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...