Page 1 of 1

help to capture some words from file

Posted: 03 Aug 2009 04:44
by willy.w
Hi,

I'm new in batch commands and need some help.
Let's say I have a file (test.txt) which contain these lines.

Subject: test test
To: 6597712345@sgw.abc.com
Message-ID: <OF2BFE240E.DD9201BC-ON48257607.0033BBC9-48257607.0033DD71@abc.com>

I need to capture some words from the file starting with 'To: ' and 'Subject: ' however I need to delete the @sgw.abc.com and also Subject:

So the output of the new file will be something like this.

To: 6597712345
<empty line here>
test test

The number 65**** is always the same length, which is 10 numbers.
Appreciate if any of you can help.

Posted: 03 Aug 2009 06:38
by ghostmachine4
you can use vbscript

Code: Select all

Set objFS = CreateObject("Scripting.FileSystemObject")
strFile = "c:\test\file.txt"
Set objFile = objFS.OpenTextFile(strFile)
Do Until objFile.AtEndOfStream
   strLine = objFile.ReadLine
   If InStr(strLine,"To:") > 0 Then
      s = Split(strLine ,"@")
      strTo =  s(0)            
   End If
   If InStr(strLine,"Subject:") > 0 Then
      s = Split(strLine,":")
      strSubject = s(1)
   End If
Loop
objFile.Close
WScript.Echo strTo
WScript.Echo
WScript.Echo strSubject


save as myscript.vbs and on command line

Code: Select all

C:\test>cscript /nologo myscript.vbs
To: 6597712345

 test test


Posted: 03 Aug 2009 19:23
by willy.w
Great it works..

Another question, how bout if I need to export the output to another txt file.

Also is there a way for the script to capture the source file without specifying the name like *.txt or *.MAI etc.

Thanks..