0

I have a VBS script that deletes everything from the start menu and adds it up again from scratch. I use this as a logon script in a domain. But last week I realized some people use different applications and those applications are not on a central share, so my question is is it possible to exclude certain folders from deletion from this script

Dim objShell, strCmd, intRunError, strFolder, objFileSys

Set objShell = CreateObject( "WScript.Shell" )
appDataLocation=objShell.ExpandEnvironmentStrings("%APPDATA%")


Set fso = CreateObject("Scripting.FileSystemObject")
Set folder = fso.GetFolder(appDataLocation & "\Microsoft\Windows\Start Menu\Programs")

' delete all subfolders and files
For Each f In folder.SubFolders
   On Error Resume Next
   name = f.name
   f.Delete True
Next

' delete all files in root folder
for each f in folder.Files
   On Error Resume Next
   name = f.name
   f.Delete True
Next

Set objFileSys = CreateObject("Scripting.FileSystemObject")
objFileSys.GetFolder("\\test\dfstest\Start Menu\programs").Copy appDataLocation & "\Microsoft\Windows\Start Menu\Programs"
Set objFileSys = Nothing

Edit: Type mismatch?

Dim objShell, strCmd, intRunError, strFolder, objFileSys
Dim Exceptions()
Dim Exception
Dim i

Set objShell = CreateObject( "WScript.Shell" )
appDataLocation=objShell.ExpandEnvironmentStrings("%APPDATA%")


Set fso = CreateObject("Scripting.FileSystemObject")
Set folder = fso.GetFolder(appDataLocation & "\Microsoft\Windows\Start Menu\Programs")

Exception(0) = appDataLocation & "\Microsoft\Windows\Start Menu\Programs\Access 2013"
Exception(1) = appDataLocation & "\Microsoft\Windows\Start Menu\Programs\Cameleon"  

For Each sf in MyFolder.SubFolders
    Exception = False
    For i = LBound(Exceptions) to UBound(Exceptions)
        If lCase(sf.Path) = lCase(Exceptions(i)) Then
            Exception = True
            Exit For
        End If
    Next
    If Not Exception Then
        deleteSubFolders
        sf.Delete
    End If
Next

Public Sub deleteSubFolders(byRef MyFolder, exclFolder)
    Dim sf
    For Each sf in MyFolder.SubFolders
        If not (lCase(sf.Path) = lCase(exclFolder)) Then
            deleteSubFolders sf, exclFolder
            sf.Delete
        End If
    Next
End Sub

' delete all files in root folder
for each f in folder.Files
   On Error Resume Next
   name = f.name
   f.Delete True
Next

Set objFileSys = CreateObject("Scripting.FileSystemObject")
objFileSys.GetFolder("\\test\dfstest\Start Menu\programs").Copy appDataLocation & "\Microsoft\Windows\Start Menu\Programs"
Set objFileSys = Nothing
5
  • 2
    Surely it's possible. But if you've written this script, then you should also know how to do it... Please note that this isn't a free scripting service, please share what you've tried and where exactly it doesn't work. Commented Sep 22, 2016 at 9:02
  • I would add the names of the folders into an array. Then before deleting, iterate through each, if the string in the array is contained within the SubFolders then don't delete it! :)
    – Dave
    Commented Sep 22, 2016 at 9:20
  • Killing any organization your coworkers have setup certainly makes them like you very much. Why are you doing this in the first place? Are you sure it's a good approach? Shouldn't every setup manage their own shortcuts?
    – Seth
    Commented Sep 22, 2016 at 9:32
  • @MátéJuhász well this is what I tried so far. by just adding an exclusion however I think with this method you can only exclude one folder. Because I dont think variables can have multiple strings. But I am not that great of a coder however.
    – Dylan_R
    Commented Sep 22, 2016 at 9:33
  • you've declared excludeFolder, but use exclFolder:(. Commented Sep 22, 2016 at 9:35

1 Answer 1

1

For multiple conditions you can use e.g. array and loop for checking all of them:

Dim Exceptions(10) as String
Dim Exception as Boolean

Exception(0)= appDataLocation & "\Microsoft\Windows\Start Menu\Programs\Access 2013"
Exception(1)=...
...
Exception(9)=...

...

For Each sf in MyFolder.SubFolders
    Exception = False
    For i = LBound(Exceptions) to UBound(Exceptions)
        If lCase(sf.Path) = lCase(Exceptions(i)) Then
            Exception = True
            Exit For
        End If
    Next i
    If Not Exception Then
        deleteSubFolders sf, exclFolder
        sf.Delete
    End If
Next sf

(I'm more experienced with VBA and also couldn't test it, so it might not work without small corrections, just typed it to explain my idea)

6
  • Dim's work a little different in VBS however same with the Foreach next. But I keep getting a type mismatch error with the exceptions.
    – Dylan_R
    Commented Sep 22, 2016 at 10:41
  • Where exactly do you get the error? Commented Sep 22, 2016 at 10:43
  • At the Exception(0) etc.
    – Dylan_R
    Commented Sep 22, 2016 at 10:45
  • Has appDataLocation has already a value at that time? Commented Sep 22, 2016 at 10:48
  • It does yes its objShell.ExpandEnvironmentStrings("%APPDATA%")
    – Dylan_R
    Commented Sep 22, 2016 at 10:49

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .