VB 脚本 - 正在搜索文件夹...
我不确定这个论坛是否适用于VB脚本,或者是否有人在这里使用它。我希望如此,因为这是我要张贴的地方。因此,我有一个工作目录(W:\ACAD工作\2011项目\...)这里我列出了几个设计公司(子文件夹)。
我有同一个设计公司的几个子文件夹,但是位置不同。例如:(为了避免尴尬,我将保留虚构的名称)
W:\ ACAD·乔布斯\2011项目\设计公司-阿拉巴马州
W:\ ACAD·乔布斯\2011项目\设计公司-华盛顿州
W:\ ACAD·乔布斯\2011项目\设计公司-亚利桑那州
我想搜索名称“设计公司”,并将全名分配给一个变量以供显示。所以,我需要搜索“设计公司”,有Var_A store“设计公司-阿拉巴马”,Var_B store“设计公司-华盛顿”,Var_C store“设计公司-亚利桑那州”。
根据我可靠的O'reilly Pocket reference,我应该能够使用\b开关在句子中搜索单词(如果您愿意,可以使用通配符)。
有人能告诉我这是什么意思吗?
此外,如果我要搜索“设计公司”,我该如何输入开关来查找任何异常文件夹,如“设计公司-亚利桑那州”?
这是我目前拥有的一段代码(也许我看问题的方式不对。如果是,请告知):
cTitle = "Test run"
Set FSO = CreateObject("Scripting.FileSystemObject")
str2011dir = "W:\ACAD JOBS\2011 Projects\"
strDF = InputBox("Design Firm Name: ", cTitle, "Design Firm")
strDFm = "\b " & strDF ' Normally, the switch would go to the front of the string right?
MsgBox "Input is -" & strDfm,, cTitle ' This is here to show me what the string consists of.
If FSO.FolderExists(str2011dir & strDFm) Then
MsgBox "The folder " & UCase(strDF) & " exists.",, cTitle
Else
MsgBox "No folder found",, cTitle
End If
编辑:根据MS
所以我的代码
strDF = InputBox("Design Firm Name: ", cTitle, "Design Firm")
strDFm = strDF & "\b"
MsgBox "Input is -" & strDfm,, cTitle
消息框应该显示“设计公司\b ”,所以我的搜索应该会找到任何带有“公司”的内容,对吗?但事实并非如此。
**** Hidden Message ***** FWIW-Dim strSearchString, strSearchFor。
。
strSearchString="C:\foo\"。
strSearchFor="foo"。
。
如果InStr(1, strSearchString, strSearchFor)>0则。
MsgBox"True"。
其他。
MsgBox“假”。
结束如果。
编辑-修改代码以获得更好的示例。
这肯定需要一些改进,但作为一个简单的例子,请尝试一下:
”
”定义函数:
”
函数ShowFolderList(strFolderPath,strSearchString)
Dim i,strTitle,strFolderName,strSubFolderList
strTitle=“显示文件夹列表”
一组so=CreateObject(“Scripting.FileSystemObject”)
如果oFso。FolderExists(strFolderPath)=为真,则
设置oSubFolders=_
o.GetFolder(strFolderPath).子文件夹的
对于oSubFolders中的每个OSUBFonders
strFolderName=oSubFolder.Name
如果InStr(1,strFolderName,strSearchString)>0,则
i=i+1
strSubFolderList=strSubFolderList&
strFolderName和vbNewLine
如果
下一个
<div>MsgBox i和>of>oSubFolders。计数&
“子文件夹匹配:”&vbNewLine&_
vbNewLine和strSubFolderList,即strTitle
其他
MsgBox“未能创建FileSystemObject对象”、_
strTitle
如果
设置oSubFolders=Nothing
文件夹集=无
一组so=Nothing
结束函数
”
使用多个参数调用函数:
”
ShowFolderList“C:\_foovScriptTest\”、“foo-”
HTH
页:
[1]