通过使用vbscript检查2 x 2行检查文件中是否存在数据 [英] Check a data exist or not in file by checking 2 by 2 line using vbscript

查看:89
本文介绍了通过使用vbscript检查2 x 2行检查文件中是否存在数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





I have a file like this : 7G8h6Tghstwt6Y.y8 . Then, I have a string data like this : "2w" & "fr" & "7G" I am going to check whether my string data exist in the file or not.





我尝试过:





What I have tried:

<pre lang="vb"><pre>

filepath = "C:\Users\Desktop\TS.txt"
substrToFind = "2w" & "fr" & "6J"

For i = 1 To Len(tmpStr) Step 2
    If InStr(tmpStr, substrToFind) <= 0 Then
        WScript.Echo "0:"
    Else
        WScript.Echo "1"
    End If

Next

推荐答案

首先实际打开文件总是一个好主意!



Well it's always a good idea to actually open the file first!

Dim objFile, tmpStr, objFSO
Const ForReading = 1
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile= objFSO.OpenTextFile("C:\Users\Desktop\TS.txt", ForReading)

然后你可以逐行阅读它,如果你真的想......

Then you can read it line by line if your really want to...

Do While Not objFile.AtEndOfStream
   tmpStr = objFile.readline
   ' Do your checks in here
Loop
objFile.Close

或者你可以读完整个文件

Or you could just read in the entire file

tmpStr = objFile.ReadAll

你的下一个问题是你试图找到的东西 substrToFind =2w& fr& 6J实际上会导致你试图找到字符串

Your next problem is the stuff you are trying to find substrToFind = "2w" & "fr" & "6J" will actually result in you trying to find the string

"2wfr6J"

- 我认为这不是你的意思。你应该分别搜索2w,fr和6J。

- I don't think that is really what you mean. You should search for 2w, fr and 6J separately.

Dim b1, b2, b3
b1 = False : b2 = False : b3 = False
Const vbTextCompare = 1
If Instr(1, tmpStr, "2w", vbTextCompare) > 0 Then
    b1 = True
End If
If Instr(1, tmpStr, "fr", vbTextCompare) > 0 Then
    b2 = True
End If
If Instr(1, tmpStr, "6J", vbTextCompare) > 0 Then
    b3 = True
End If
If b1 And b2 And b3 Then
    WScript.Echo "Found"
End If

另外请注意,我已经让搜索进行了文本搜索 - Instr的默认值是二进制。



最后,这里有一些参考资料 - 总是一个好的开始

VBScript - OpenTextFile方法 [< a href =https://www.vbsedit.com/html/8575e5c4-dec5-48e7-92a2-790cac708c7f.asp#seeAlsoToggletarget =_ blanktitle =New Window> ^ ]

VBScript - ReadAll方法 [ ^ ]

VBScript - InStr功能 [ ^ ]

Also note that I've made the search do a text search - the default for Instr is binary.

Finally, here is some reference material - always a good place to start
VBScript - OpenTextFile Method[^]
VBScript - ReadAll Method[^]
VBScript - InStr Function[^]


这是有效的。但如果找不到字符串,我无法弄清楚打印结果。任何的想法?请帮帮我



This is working. But I can not figure out to print the result if the string is not found. Any Idea? Help me please

Dim fb1, fb2, fb3
Dim objFile, FBString, objFSO
Const ForReading = 1
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile= objFSO.OpenTextFile("C:\Users\fitri\Desktop\Project 2\ScanFB\FS_Data\FS_TST.txt", ForReading)

FBString = objFile.ReadAll
WScript.Echo "Feature_Byte : "  & FBString

fb1 = False : fb2 = False : fb3 = False
If Instr(1, FBString, "6J") > 0 Then
	fb1 = True
	WScript.Echo "Found 6J"
End If

If Instr(1, FBString, "fr") > 0 Then
	fb3 = True
	WScript.Echo "Found fr"
End If

If Instr(1, FBString, "jC") > 0 Then
	fb3 = True
	WScript.Echo "Found jC"
End If


这篇关于通过使用vbscript检查2 x 2行检查文件中是否存在数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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