views:

86

answers:

1

Hello,

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

+1  A: 

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 
ghostdog74
thanks for that but i will have it in vbscript :-), because its only one part from a big script
matthias
can you show me this with an script?
matthias
ok thanks for the example, but my problem is how can i search for a second string after the last founded string 1? than I'm happy :-D
matthias
ghostdog74
thanks for the link, i will read it...thanks...i think i have only to do a second instr() after the first one, with the second string. thats right?
matthias
yes. that's right
ghostdog74
thanks, you helped me a lot :-)
matthias