Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2013-10-24
12
Medium Priority
?
309 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 57

Accepted Solution

by:
Bill Prew earned 2000 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 57

Expert Comment

by:Bill Prew
ID: 39597478
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

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 57

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 57

Expert Comment

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

~bp
0
 
LVL 57

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 57

Expert Comment

by:Bill Prew
ID: 39598473
Welcome.

~bp
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…

721 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