0

質問: VBS でFOLDER1パスとパス文字列を比較する必要があります。FOLDER2

FOLDER1先ほど保存したテキストファイルから読み込みました。FOLDER2- [フォルダーの選択] ダイアログから。ユーザーが次の場合に選択できないようにしたいFOLDER2:
FOLDER2 = FOLDER1 FOLDER2 = FOLDER1\some_folder FOLDER2 = Parent_Folder\FOLDER1

例: Folder1 = c:\users\user\Documents then にFolder2することはできません: c:\users\user\Documentsc:\users\user\Documents\Lettersまたはc:\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

これらのいずれにも当てはまらない場合:c:\users\user\Documents, c:\users\user\Documents\Lettersまたは最初の 2 つc:\users\user\Instr役立つ可能性があります。

例えば、

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

ユーザー入力のどの部分も Folder1 と同じにしたくない場合は、InStrチェックの順序を逆にすることができます。

これよりも具体的に取得する必要がある場合は、Folder 文字列を配列に分割し (「\」で分割)、配列内の最初の x 個のフィールドを比較する必要があります。

于 2013-10-07T14:25:36.897 に答える