Windows Vista Tips

Windows Vista Tips > Newsgroups > Windows Server > Scripting > Re: Batch Script Text file parse

Reply
Thread Tools Display Modes

Re: Batch Script Text file parse

 
 
Richard Mueller [MVP]
Guest
Posts: n/a

 
      03-24-2009

<> wrote in message
news:231436ae-72ed-499e-a100-...
> Newbie here.
>
> I am trying to get a single line of text from each file within a
> directory of text files.
>
> Example
>
> I have directory X that contains files:
>
> file1.txt
> file2.txt
> file3.txt
>
> Each text file is similar in structure, and contains lines like:
>
> Some mumbo jumbo
> Uniqe ID: xyzpdq
> some other mumbo jumbo
> etc, etc, etc.
>
> In all cases, line 2 contains "Unique ID:" and then the unique
> identifier text, which is what I need to extract.
> In the above case, I need the "xyzpdq". Note that this is what
> changes in each test file.
>
> I want to run a script that will parse either this unique ID, or the
> entire 2nd line of text (in which case I can just trim it later) -
> from all .txt files within the folder.
>
> any help?
>
> Thanks.


Someone else can supply a batch file solution. Here is a VBScript solution:
==========
Option Explicit

Dim strFolder, objFSO, objFolder, objItem, objFile, strLine, strSearch,
strID

Const ForReading = 1

' Specify the directory.
strFolder = "c:\scripts"

' Specify the string to search for.
' Make all lower case for comparison (not spelling).
strSearch = "unique id:"

' Bind to the folder object.
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(strFolder)

' Enumerate all files in the folder.
For Each objItem in objFolder.Files
' Open the file with a textstream object.
Set objFile = objFSO.OpenTextFile(objItem.Path, ForReading)
' Read each line of the file.
Do Until objFile.AtEndOfStream
strLine = Trim(objFile.ReadLine)
' Check for ID search string.
If (InStr(LCase(strLine), strSearch) = 1) Then
' The ID is assumed to follow the first ":" in the line.
strID = Trim(Mid(strLine, InStr(strLine, ":") + 1))
Wscript.Echo strID
' No need to read any more of the file.
Exit Do
End If
Loop
' Close the file.
objFile.Close
Next

--
Richard Mueller
MVP Directory Services
Hilltop Lab - http://www.rlmueller.net
--


 
Reply With Quote
 
 
 
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Batch Script Text file parse Pegasus [MVP] Scripting 0 03-24-2009 04:35 PM
Re: Batch Script to parse lines in text file Al Dunbar Scripting 2 01-27-2009 09:51 PM
Re: Importing variables into Batch script from text file MMC-Michelle Scripting 2 03-05-2007 03:35 PM
Re: Script To Parse A Text File? Sam Low Scripting 0 08-12-2004 10:44 AM
Script To Parse A Text File? sbillard Scripting 0 08-10-2004 09:27 PM



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59