0

问题是:我需要比较VBS 中的FOLDER1路径和FOLDER2路径字符串。

FOLDER1我从文本文件中读取,它是之前保存的。FOLDER2- 从选择文件夹对话框。我想阻止用户选择FOLDER2是否:
FOLDER2 = FOLDER1 FOLDER2 = FOLDER1\some_folder FOLDER2 = Parent_Folder\FOLDER1

例如: Folder1 = c:\users\user\Documents thenFolder2不能是: c:\users\user\Documents,c:\users\user\Documents\Lettersc:\users\user\

无法制作正确的正则表达式进行比较。现在使用以下代码,但需要正常的解决方案。

    RightPath = 0
    Do

    Set objShell = CreateObject("Shell.Application")
    Set objFolder = objShell.BrowseForFolder(0, "Select folder:", &H10&, strPath)

        If objFolder Is Nothing Then
        msgbox "Configuration canceled" ,64 , "Information"
        Wscript.Quit
    End If

    Set objFolderItem = objFolder.Self
    objPath = objFolderItem.Path

    ' Right now, Check for users folder only
    RightPath = RightPath + 1
    Dim re, targetString
    Set re = New RegExp
    With re
      .Pattern = "Desktop|Documents|Downloads|Music|Pictures|Videos"
      .Global = False
      .IgnoreCase = True
    End With

    targetString = objPath
    If re.Test(targetString)  Then
        msgbox "You cannot choose:" & vbCrLf & vbCrLf &  _
        "Desktop, Documents, Downloads, Music, Pictures or Videos" & vbCrLf & vbCrLf & _
   "Please select another location" ,48 , "Warning!"
        RightPath = 0
    End If

    Loop Until RightPath > 0

    msgbox "You selected "+targetString ,0 , "Information,"
    Wscript.Quit
4

2 回答 2

0

我将假设只应排除直接父文件夹和子文件夹(例如FOLDER1\foo,禁止,但FOLDER1\foo\bar允许)。如果这个假设是正确的,那么这样的事情应该可以工作:

Set fso = CreateObject("Scripting.FileSystemObject")
Set app = CreateObject("Shell.Application")

dir1 = LCase(fso.GetAbsolutePathName("C:\Users\user"))
parent1 = fso.GetParentFolderName(dir1)

Do
  Set fldr = app.BrowseForFolder(0, "Select folder:", &H10&, dir1)
  If fldr Is Nothing Then Exit Do  'user pressed [Cancel]

  dir2 = LCase(fldr.Self.Path)
  parent2 = fso.GetParentFolderName(dir2)
Loop Until dir1 <> dir2 And parent1 <> dir2 And dir1 <> parent2

请注意,使用您为BrowseForFolder用户使用的参数将只能选择下面的文件夹,dir1除非他在输入字段中键入不同的路径。

于 2013-10-07T19:01:24.240 回答
0

如果它不能是这些中的任何一个:或者c:\users\user\Documents, c:\users\user\Documents\Letters可能对前两个有帮助。c:\users\user\Instr

例如,

Folder1 = "c:\users\user\Documents"
Folder2 = "c:\users\user\Documents\Letters"
If InStr(Folder2,Folder1) Then FAIL

如果您不希望用户输入的任何部分与 Folder1 相同,那么您可以颠倒InStr检查的顺序。

如果您需要比这更具体,您可能必须将文件夹字符串拆分为数组(由“\”拆分),然后比较数组中的前 x 个字段。

于 2013-10-07T14:25:36.897 回答