问题是:
我需要在VBS中比较FOLDER1
路径和FOLDER2
路径字符串。
FOLDER1
我从文本文件中读取,它之前已保存。 FOLDER2
- 来自“选择文件夹”对话框。
我想阻止用户选择FOLDER2
if:
FOLDER2 = FOLDER1
FOLDER2 = FOLDER1\some_folder
FOLDER2 = Parent_Folder\FOLDER1
例如:
Folder1 = c:\users\user\Documents
那么Folder2
不能是:c:\users\user\Documents
,c:\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
答案 0 :(得分: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个字段。
答案 1 :(得分: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
下方选择一个文件夹,除非他在输入字段中输入不同的路径。