Solved

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

Posted on 2013-10-24
12
308 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 6
12 Comments
 
LVL 56

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 56

Expert Comment

by:Bill Prew
ID: 39597478
0
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 

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 56

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 56

Expert Comment

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

~bp
0
 
LVL 56

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 56

Expert Comment

by:Bill Prew
ID: 39598473
Welcome.

~bp
0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

If you haven’t already, I encourage you to read the first article (http://www.experts-exchange.com/articles/18680/An-Introduction-to-R-Programming-and-R-Studio.html) in my series to gain a basic foundation of R and R Studio.  You will also find the …
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.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

628 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