2010-07-27 63 views
1

我嘗試製作一個VBScript,它讀取一個txt並搜索兩個字符串,並僅給出最後結果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 

有人有一個想法,可以幫助我嗎?

問候, 馬蒂亞斯

回答

2

如果它不是一個更大的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」,然後如果找到,則爲該行分配一個變量。在文件迭代結束時,打印出這2個變量。

例如搜索「你好」

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 
+0

感謝這一點,但我一定要有計劃在VBScript :-),因爲它只是其中的一部分,從一個大的腳本 – Sebastian 2010-07-27 08:11:04

+0

你能告訴我這個有腳本? – Sebastian 2010-07-27 09:01:15

+0

好的謝謝你的例子,但我的問題是我如何搜索最後建立的字符串1後的第二個字符串?比我高興:-D – Sebastian 2010-07-27 09:25:46