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