Delete system folders using objFSO.Delete?

November 8, 2010 at 16:08:14
Specs: Windows XP

I have cobbled together a vbs script to automate the deletion of file types from servers that are running low on space. I'm no vbs expert so I'm sure it could be coded better but that said, it works just fine.

The problem I'm having is when I've tried to extend it to delete the content.ie5 and history.ie5 folders from user's profiles and the recycler folders from user's home drives. It works if I test it on a dummy file server folder structure that I've created on my desktop PC but not when I run it on a server.

I was wondering, is this because the content.ie5, history.ie5 and recycler folders are system folders? Is it possible to delete system folders using objFSO.DeleteFolder?


See More: Delete system folders using objFSO.Delete?

Report •


#1
November 9, 2010 at 07:08:30

With CreateObject("Scripting.FileSystemObject")
  .CreateFolder("SystemFolder").Attributes = 20
  MsgBox "SystemFolder exists!"
  .DeleteFolder "SystemFolder"
  MsgBox "SystemFolder died :("
End With

It's possible. I'd tell you why yours isn't working, but it turns out I'm not physic. If I'd venture a guess, it'd be because you're running the script while something is using those directories, or you don't have delete permission.

How To Ask Questions The Smart Way


Report •

#2
November 12, 2010 at 00:49:31

Do you mean psychic? Are you referring to the fact that I didn't post the code? If so, the reason I didn't post the code because it works if I manually create the folder structure but if it helps, here you go...

Sub CheckFolder(objCurrentFolder, objLogFile)

on error resume next

Dim strTemp
Dim strSearch
Dim strOutput
Dim objNewFolder
Dim objFile
Dim objStream
Dim TempFile


strExt1 = ".tmp"
strExt2 = ".ost"
strExt3 = ".mmf"
strExt4 = ".old"
strExt5 = ".dmp"
strExt6 = ".bak"


For Each objFile In objCurrentFolder.Files

strTemp = Right(objFile.Name, 4)
TempFile = False

If strTemp = strExt1 Then TempFile = True
If strTemp = strExt2 Then TempFile = True
If strTemp = strExt3 Then TempFile = True
If strTemp = strExt4 Then TempFile = True
If strTemp = strExt5 Then TempFile = True
If strTemp = strExt6 Then TempFile = True

If TempFile = True Then
strOutput = CStr(objFile.Path) + ", " +_
CStr(FormatNumber(objFile.Size/1024/1024, 2)) + " MB"
TotalSizeFound = TotalSizeFound + objFile.size
objLogFile.writeline strOutput
objFSO.DeleteFile objFile, true
End If
Next

For Each objNewFolder In objCurrentFolder.subFolders

strFolderPath = CStr(Lcase(objNewFolder.Path))

If InStr(strFolderPath,"home")>0 Then
If objNewFolder.Name = "recycler" Then
strOutput = CStr(objNewFolder.Path) + ", " +_
CStr(FormatNumber(objNewFolder.Size/1024/1024, 2)) + " MB"
TotalSizeFound = TotalSizeFound + objNewFolder.size
objLogFile.writeline strOutput
objFSO.DeleteFolder objCurrentFolder, true
End If
ElseIf InStr(strFolderPath,"profiles")>0 Then
If objNewFolder.Name = "content.ie5" Then
strOutput = CStr(objNewFolder.Path) + ", " +_
CStr(FormatNumber(objNewFolder.Size/1024/1024, 2)) + " MB"
TotalSizeFound = TotalSizeFound + objNewFolder.size
objLogFile.writeline strOutput
objFSO.DeleteFolder objCurrentFolder, true


ElseIf objNewFolder.Name = "history.ie5" Then
strOutput = CStr(objNewFolder.Path) + ", " +_
CStr(FormatNumber(objNewFolder.Size/1024/1024, 2)) + " MB"
TotalSizeFound = TotalSizeFound + objNewFolder.size
objLogFile.writeline strOutput
objFSO.DeleteFolder objCurrentFolder, true
End If
End If

CheckFolder objNewFolder, objLogFile
Next

End Sub

This is just the recursive sub that deletes the files and like I said, it's probably a bit clunky because I've sort of cobbled together bits I've found on the net with bits that I've written.

By the way, I don't think it's either of the reasons you suggest because I can manually delete the files if I search for them using Windows Explorer.


Report •

#3
November 12, 2010 at 06:59:57

on error resume next
To see what errors your script gets, don't disable error checking.

If objNewFolder.Name = "content.ie5" Then
objFSO.DeleteFolder objCurrentFolder, true

If the folder has a subfolder called content.ie5, delete the entire folder? (Or: Delete content.ie5's parent folder.) Are you sure this logic is correct?

If objNewFolder.Name = "content.ie5" Then
ElseIf objNewFolder.Name = "history.ie5" Then

I'm pretty sure it's Content.IE5 and History.IE5. Possibly also RECYCLER, but who can say?

How To Ask Questions The Smart Way


Report •

Related Solutions

#4
November 12, 2010 at 07:22:50

The reason I had on error resume next in the sub was to stop it from falling over when hits a .tmp file that's locked. Is there a better way to do this?

I think the logic is correct, what it's trying to do is delete folders called content.ie5 or history.ie5 only if the path also contains the string profiles. Same idea for recycler where the path contains home.

One thing I've maybe not made clear is that it's not as if the script is finding these folders yet failing to delete them - it just doesn't find the folders in the first place. If I was getting an error deleting the folders then they'd still show up in the log file.


Report •

#5
November 12, 2010 at 08:53:06

The reason I had on error resume next in the sub was to stop it from falling over when hits a .tmp file that's locked. Is there a better way to do this?
There really isn't, but the first step in debugging a VBScript is to kill the error checking. Otherwise, you have no idea if something is failing, because it does so silently.

what it's trying to do is delete folders called content.ie5 or history.ie5 only if the path also contains the string profiles.
That's not the logic. It's trying to delete the parent folders of content.ie5 and history.ie5 if the path also contains the string "profiles."

it's not as if the script is finding these folders yet failing to delete them - it just doesn't find the folders in the first place.
I know. You probably don't have a content.ie5 nor history.ie5. You do, however, have Content.IE5 and History.IE5. Case matters.

How To Ask Questions The Smart Way


Report •

#6
November 13, 2010 at 00:28:00

This is great - I can see what I've done wrong now. I thought I was converting the entire folder path to lower case using CStr(Lcase(objNewFolder.Path)) when I was actually just converting everything above objnewfolder.name. Thank you so much for all your help.

By the way, the logic is correct, it goes like this...

- if the folder path contains the string "home" then check whether the current folder is called "recycler" (now "RECYCLER")
- if the folder path contains the string "profiles" then check whether the current folder is called "content.ie5" or "history.ie5" (now "Content.ie5" or "History.ie5")

Once again, thanks for your help - I feel like I've been banging my head against a wall for days with this!!


Report •

#7
November 13, 2010 at 05:46:50

Okay, let's put it this way:
If objNewFolder.Name = "recycler" Then
What is objNewFolder? Because it's what you're looking for.

objFSO.DeleteFolder objCurrentFolder, true
What is objCurrentFolder? Because that's what you're deleting.

How To Ask Questions The Smart Way


Report •

#8
November 13, 2010 at 06:49:17

My god, you're completely right! I hang my head in shame...

Report •


Ask Question