用于查找字符串的Vb脚本 [英] Vb script to find the string

查看:124
本文介绍了用于查找字符串的Vb脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在文本文件中设置了MAP如下。



地图别名elr72_511

类型常规byRule

角色替换admin给owner_roles

评论Converted_from_elr72_rule_31

到1/2 / x29,1 / 3 / x27

从1/4 / x5..x6

退出

地图别名elr72_582

类型常规byRule

角色将admin替换为owner_roles

评论Converted_from_elr72_rule_274

到1/2 / x29,1 / 3 / x27

从1/4 / x5..x6

退出



来自该文本文件我需要找到所有的地图名称到1/2 / x29



我尝试过:



Dim objFSO,filepath,objInputFile,tmpStr ,substrToFind



设置objFSO = CreateObject(Scripting.FileSystemObject)

filepath =C:\ Users \Desktop \新文本Document.txt

substrToFind =1/2 / x27

设置objInputFile = objFSO.OpenTextFile(filepath)

tmpStr = objInputFile.Read

如果InStr(tmpStr,substrToFind)< = 0那么

WScript.Echo 没有比赛

否则

WScript.Echo找到匹配

结束如果

解决方案

有两种简单的方法可以解决这个问题:

1)如果每个地图都以关键字'exit'结束并且这个关键字不会出现在其他地方是正确的在地图中的位置,您可以将完整的文件内容读入单个字符串,然后使用split方法将字符串转换为数组,使用exit作为分隔符。使用For-Next-Loop,您可以检查每个数组元素以查找搜索到的字符串。



2)您可以使用正则表达式对象来解析文件内容 - 这将更优雅,更容易,但需要对正则表达式有一个很好的理解。



这是用VBScript创建的示例:

  Dim  objFSO,filepath,objInputFile,tmpStr,substrToFind,arrRecords,r 
Const ForReading = 1

设置 objFSO = CreateObject( Scripting.FileSystemObject
filepath = < span class =code-string> C:\ Users \Desktop\New Text Document.txt
substrToFind = 1/2 / x29,1 / 3 / x27
设置 objI nputFile = objFSO.OpenTextFile(filepath,ForReading)
tmpStr = objInputFile.ReadAll()


If InStr( tmpStr,substrToFind)< = 0 然后
WScript.Echo 不匹配
其他
arrRecords =拆分(tmpStr, 退出

For r = LBound(arrRecords, 1 UBound(arrRecords , 1
如果 Instr(arrRecords(r),substrToFind)> 0 然后
MsgBox '& substrToFind& ''in& (r +1)& 。record
结束 如果
下一步

MsgBox 找不到更多匹配项
结束 如果


i have set of MAP in text file as below.

map alias elr72_511
type regular byRule
roles replace admin to owner_roles
comment Converted_from_elr72_rule_31
to 1/2/x29,1/3/x27
from 1/4/x5..x6
exit
map alias elr72_582
type regular byRule
roles replace admin to owner_roles
comment Converted_from_elr72_rule_274
to 1/2/x29,1/3/x27
from 1/4/x5..x6
exit

from that text file i need to find all maps name which are going to 1/2/x29.

What I have tried:

Dim objFSO, filepath, objInputFile, tmpStr, substrToFind

Set objFSO = CreateObject("Scripting.FileSystemObject")
filepath = "C:\Users\Desktop\New Text Document.txt"
substrToFind = "1/2/x27"
Set objInputFile = objFSO.OpenTextFile(filepath)
tmpStr = objInputFile.Read
If InStr(tmpStr, substrToFind) <= 0 Then
WScript.Echo "No matches"
Else
WScript.Echo "Found match"
End If

解决方案

There are two simple ways to solve this:
1) if it is correct that each map ends with the key word 'exit' and if this key word won't occur in other positions within a map, you may read the complete file content into a single string, then use the split method to turn the string into an array using 'exit' as the separator. With a For-Next-Loop you can check each array element for the searched string.

2) you may use a regex object to parse the file content - this would be more elegant and easier but requires a good understanding of regular expressions.

Here's a sample created with VBScript:

Dim objFSO, filepath, objInputFile, tmpStr, substrToFind, arrRecords, r
Const ForReading = 1

Set objFSO = CreateObject("Scripting.FileSystemObject")
filepath = "C:\Users\Desktop\New Text Document.txt"
substrToFind = "1/2/x29,1/3/x27"
Set objInputFile = objFSO.OpenTextFile(filepath, ForReading)
tmpStr = objInputFile.ReadAll()


If InStr(tmpStr, substrToFind) <= 0 Then
	WScript.Echo "No matches"
Else
	arrRecords = Split(tmpStr, "exit")
	
	For r = LBound(arrRecords, 1) To UBound(arrRecords, 1)
		If Instr(arrRecords(r), substrToFind) > 0 Then
			MsgBox "'" & substrToFind & "' found in " & (r +1) & ". record"
		End If
	Next
	
	MsgBox "No more matches found"
End If


这篇关于用于查找字符串的Vb脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆