help to capture some words from file

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
willy.w
Posts: 2
Joined: 03 Aug 2009 04:37

help to capture some words from file

#1 Post by willy.w » 03 Aug 2009 04:44

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.

ghostmachine4
Posts: 319
Joined: 12 May 2006 01:13

#2 Post by ghostmachine4 » 03 Aug 2009 06:38

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


willy.w
Posts: 2
Joined: 03 Aug 2009 04:37

#3 Post by willy.w » 03 Aug 2009 19:23

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..

Post Reply