Solved

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

Posted on 2013-10-24
12
303 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 53

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 53

Expert Comment

by:Bill Prew
ID: 39597478
0
ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

 

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 53

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
 
LVL 53

Expert Comment

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

~bp
0
 
LVL 53

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 53

Expert Comment

by:Bill Prew
ID: 39598473
Welcome.

~bp
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Copy a row 12 62
Visual Studio 2005 text editor 10 38
Excel Automation VBA 19 70
Delphi: barcode reading on android platform 1 30
Having just graduated from college and entered the workforce, I don’t find myself always using the tools and programs I grew accustomed to over the past four years. However, there is one program I continually find myself reverting back to…R.   So …
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

773 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