在 vbs 脚本中排除文件夹

在 vbs 脚本中排除文件夹

我有一个 VBS 脚本,它可以从开始菜单中删除所有内容,然后从头开始重新添加。我将其用作域中的登录脚本。但上周我意识到有些人使用不同的应用程序,而这些应用程序不在中央共享中,所以我的问题是,是否可以从此脚本中排除某些文件夹以将其从删除中排除

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

编辑:类型不匹配?

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

答案1

对于多个条件,您可以使用例如数组和循环来检查所有条件:

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

(我对 VBA 更有经验,也无法测试它,所以如果不进行小修改它可能无法工作,只是输入它来解释我的想法)

相关内容