Solved

Need VBScript to skip Administrator Profile (and other system folders)

Posted on 2013-10-24
12
300 Views
Last Modified: 2013-10-24
Greetings all,

I located a good VBScript to delete an Internet shortcut from the Desktop of all user profiles.  the script works great but I'm having trouble getting it to skip the Administrator profile along with All Users, Default Users, LocalServices and NetworkServices.

I know I need a "For" somewhere in there like this but I'm not sure where to drop it in and make sure it is smooth.

or Each subfolder In FSfolder.subfolders

   If (subfolder.Name <> "All Users" And  subfolder.Name <> "Default User"_
   and subfolder.Name <> "LocalService" and subfolder.Name <> "NetworkService") Then

Below is the script:

START_IN = "C:\Backups"
PREFIX = "Picture Perfect 4 Client"

Set oFSO = CreateObject("Scripting.FileSystemObject")

ProcessSubFolders oFSO.GetFolder(START_IN), iCount

Sub ProcessSubFolders(oFolder, iCount)
  Set cFiles = oFolder.Files
  For Each oFile In cFiles
    If Left(oFile.Name, Len(PREFIX)) = PREFIX Then
      oFile.Delete
      iCount = iCount + 1
    End If
  Next
  For Each oSubFolder In oFolder.SubFolders
    ProcessSubFolders oSubFolder, iCount
  Next
End Sub

WScript.Echo "Files deleted: " & iCount
0
Comment
Question by:yccdadmins
  • 6
  • 6
12 Comments
 
LVL 51

Accepted Solution

by:
Bill Prew earned 500 total points
ID: 39597373
Here's an approach using a dictionary object that should work.

START_IN = "C:\Backups"
PREFIX = "Picture Perfect 4 Client"

' Create a dictionary to hold the list of folders to skip
Set dicSkip = CreateObject("Scripting.Dictionary")
dicSkip.CompareMode = vbTextCompare

' Add Folder names to skip
dicSkip.Add "All Users", 0
dicSkip.Add "Default User", 0
dicSkip.Add "LocalService", 0
dicSkip.Add "NetworkService", 0

Set oFSO = CreateObject("Scripting.FileSystemObject")

ProcessSubFolders oFSO.GetFolder(START_IN), iCount

Sub ProcessSubFolders(oFolder, iCount)
  ' Skip any folders we don't want to process
  If Not dicSkip.Exists(objFolder.Name) Then

    For Each oFile In oFolder.Files
      If Left(oFile.Name, Len(PREFIX)) = PREFIX Then
        oFile.Delete
        iCount = iCount + 1
      End If
    Next

    For Each oSubFolder In oFolder.SubFolders
      ProcessSubFolders oSubFolder, iCount
    Next

  End If
End Sub

WScript.Echo "Files deleted: " & iCount 

Open in new window

~bp
0
 

Author Comment

by:yccdadmins
ID: 39597458
Wow - never even heard of a dictionary object - awesome.  That is a lot cleaner than the if/then I was going to use.   I'll give that a try.
0
 
LVL 51

Expert Comment

by:Bill Prew
ID: 39597478
0
 

Author Comment

by:yccdadmins
ID: 39597552
Looks like something is missing in the line "If Not dicSkip.Exists(objFolder.Name) Then "

Received the following error:

Object required: "objfolder' 800a01a8

I'm looking for a syntax mismatch maybe?  Or objFolder missing from above?
0
 
LVL 51

Expert Comment

by:Bill Prew
ID: 39597587
Sorry, I usually preface with 3 character "type" indicators, rather than one, I missed that in that line.  Change objFolder to oFolder.

~bp
0
 

Author Comment

by:yccdadmins
ID: 39597836
That did it - runs perfectly on the test folder.  I'll set this as the accepted solution.

Now I have to create another question.  I'm dealing with this ridiculous permissions error when I try and run it on the C:\Users folder.  Looks like lots of people get this error but there are a million questions and no solutions that match my circumstance on Google.  Imagine that.

Thanks for your assistance!
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 51

Expert Comment

by:Bill Prew
ID: 39597929
No need for another question, just a sec...

~bp
0
 
LVL 51

Expert Comment

by:Bill Prew
ID: 39597934
Give this change a try:

START_IN = "C:\Backups"
PREFIX = "Picture Perfect 4 Client"

' Create a dictionary to hold the list of folders to skip
Set dicSkip = CreateObject("Scripting.Dictionary")
dicSkip.CompareMode = vbTextCompare

' Add Folder names to skip
dicSkip.Add "All Users", 0
dicSkip.Add "Default User", 0
dicSkip.Add "LocalService", 0
dicSkip.Add "NetworkService", 0

Set oFSO = CreateObject("Scripting.FileSystemObject")

ProcessSubFolders oFSO.GetFolder(START_IN), iCount

Sub ProcessSubFolders(oFolder, iCount)
  On Error Resume Next

  ' Skip any folders we don't want to process
  If Not dicSkip.Exists(oFolder.Name) Then

    For Each oFile In oFolder.Files
      If Left(oFile.Name, Len(PREFIX)) = PREFIX Then
        oFile.Delete
        iCount = iCount + 1
      End If
    Next

    If Err.Number = 0 Then
      For Each oSubFolder In oFolder.SubFolders
        ProcessSubFolders oSubFolder, iCount
      Next
    Else
      Err.Clear
    End If

  End If
End Sub

WScript.Echo "Files deleted: " & iCount 

Open in new window

~bp
0
 

Author Comment

by:yccdadmins
ID: 39597947
Error is Permission denied - 800A0046

I am running the script as a domain account with Local Administrator privileges on the server.  I tested running it as the actual Local Administrator account and received the same error.

I know this has something to do with permissions and the C:\User folder(s) but I can't seem to find anything on it.  I know the script is good because I copied user profiles folders to another folder and tested successfully.
0
 

Author Comment

by:yccdadmins
ID: 39597954
I see the "on error resume next" so I'll try that.
0
 

Author Comment

by:yccdadmins
ID: 39598020
That worked - kind of.  Apparently, user with Local Administrator, or the actual local Administrator account, do not have permissions to all of the User profile folders.  The script deleted the shortcut in all the folders I have access to but not the others.  It is strange because I can view all the folders but the one's I cannot delete from pop up a message when I view them that says, "You don't currently have access to this folder.  Click continue to permanently have access to this folder."

It is odd that some allow access and some do not.  This is a Microsoft permissions quirk.  The script works great and I'll figure out how to get my account permanent access to all the folders in the Users folder.

Thanks for your help!
0
 
LVL 51

Expert Comment

by:Bill Prew
ID: 39598473
Welcome.

~bp
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Citrix XenApp, Internet Explorer 11 set to Enterprise Mode and using central hosted sites.xml file.
When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now