VBScript 在 TXT 中搜索两个字符串

发布于 2024-09-11 17:23:34 字数 1087 浏览 8 评论 0原文

我尝试制作一个VBScript来读取txt并搜索两个字符串并给出仅最后一个结果。

字符串1:Hello123

字符串2:Test123强>

TXT 看起来像这样:

27.07.2010 09:45 ... DumDumDum ... 
27.07.2010 09:45 ... BlaBlaBla ... 
27.07.2010 09:45 ... Hello123 ... 
27.07.2010 09:45 ... BlaBlaBla ... 
27.07.2010 09:45 ... DumDumDum ... 
27.07.2010 09:45 ... DumDumDum ... 
27.07.2010 09:45 ... Hello123 ...   'This Result
27.07.2010 09:45 ... BlaBlaBla ... 
27.07.2010 09:45 ... BlaBlaBla ... 
27.07.2010 09:45 ... DumDumDum ... 
27.07.2010 09:45 ... Test123 ... 
27.07.2010 09:45 ... DumDumDum ... 
27.07.2010 09:45 ... Test123 ...        'And this Result
27.07.2010 09:45 ... DumDumDum ... 
27.07.2010 09:45 ... BlaBlaBla ... 

我尝试这样的事情,但我不知道该怎么做:

我认为使用 ReadALL 读取 txt,而不是搜索部分。

if string 1 not found then
    msgbox "nothing found"
    Goto NEXT
else
    if string 2 not found then
        msgbox "nothing found"
    else
        msgbox "found"

    End if
End if
NEXT

有人有想法并且可以帮助我吗?

问候, 马蒂亚斯

i try to make a VBScript that read a txt and search for two strings and gives out only the last results.

String 1: Hello123

String 2: Test123

The TXT looks like this:

27.07.2010 09:45 ... DumDumDum ... 
27.07.2010 09:45 ... BlaBlaBla ... 
27.07.2010 09:45 ... Hello123 ... 
27.07.2010 09:45 ... BlaBlaBla ... 
27.07.2010 09:45 ... DumDumDum ... 
27.07.2010 09:45 ... DumDumDum ... 
27.07.2010 09:45 ... Hello123 ...   'This Result
27.07.2010 09:45 ... BlaBlaBla ... 
27.07.2010 09:45 ... BlaBlaBla ... 
27.07.2010 09:45 ... DumDumDum ... 
27.07.2010 09:45 ... Test123 ... 
27.07.2010 09:45 ... DumDumDum ... 
27.07.2010 09:45 ... Test123 ...        'And this Result
27.07.2010 09:45 ... DumDumDum ... 
27.07.2010 09:45 ... BlaBlaBla ... 

I try something like this, but i don't know how to do this:

Read the txt with ReadALL i think and than the search part.

if string 1 not found then
    msgbox "nothing found"
    Goto NEXT
else
    if string 2 not found then
        msgbox "nothing found"
    else
        msgbox "found"

    End if
End if
NEXT

Has someone an idea and can help me?

Greetings,
matthias

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

人海汹涌 2024-09-18 17:23:34

如果它不是更大的 vbscript 程序的一部分,并且您有足够的能力下载东西,您可以使用文件处理工具,例如 gawk for windows 例如一个行

C:\test> gawk "/Hallo123/{h=$0}/Test123/{t=$0}END{print h \"\n\" t}" file
27.07.2010 09:45 ... Hallo123 ...   'This Result
27.07.2010 09:45 ... Test123 ...        'And this Result

使用 vbscript,使用 instr() 检查每个字符串“Hallo123”和“Test123”,然后如果找到,则为该行分配一个变量。在文件迭代结束时,打印出这两个变量。

搜索“Hallo”的示例

Set objFS = CreateObject("Scripting.FileSystemObject")
'File to scan
strFile = "c:\test\file.txt"
'Pattern to search for, eg Hallo
strPattern = "Hallo"
Set objFile = objFS.OpenTextFile(strFile)
Do Until objFile.AtEndOfStream
    strLine = objFile.ReadLine  
    If InStr(strLine,strPattern)>0 Then
        WScript.Echo strLine
                H=strLine
    End If  
Loop
Wscript.Echo H 

if its not part of a bigger vbscript program and you have the luxury to download stuff, you can use a file processing tool, such as gawk for windows eg one liner

C:\test> gawk "/Hallo123/{h=$0}/Test123/{t=$0}END{print h \"\n\" t}" file
27.07.2010 09:45 ... Hallo123 ...   'This Result
27.07.2010 09:45 ... Test123 ...        'And this Result

With vbscript, use instr() to check for each string "Hallo123" and "Test123", then if found, assign a variable to that line. At the end of file iteration, print out those 2 variables.

example searching for "Hallo"

Set objFS = CreateObject("Scripting.FileSystemObject")
'File to scan
strFile = "c:\test\file.txt"
'Pattern to search for, eg Hallo
strPattern = "Hallo"
Set objFile = objFS.OpenTextFile(strFile)
Do Until objFile.AtEndOfStream
    strLine = objFile.ReadLine  
    If InStr(strLine,strPattern)>0 Then
        WScript.Echo strLine
                H=strLine
    End If  
Loop
Wscript.Echo H 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文