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
Solved

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

Posted on 2013-10-24
12
304 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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

When you upgrade from Windows 8 to 8.1 or to Windows 10 or if you are like me you are on the Insider Program you may find yourself with many 450MB recovery partitions.  With a traditional disk that may not be a problem but with relatively smaller SS…
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
With the advent of Windows 10, Microsoft is pushing a Get Windows 10 icon into the notification area (system tray) of qualifying computers. There are many reasons for wanting to remove this icon. This two-part Experts Exchange video Micro Tutorial s…

839 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