Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to extract line from text file

Status
Not open for further replies.
May 11, 2004
37
US
I need to extract a line from a text file and write that line to a new text file. I want to do this for each line that contains the string "AB". Does anyone know how to do this? Thanks, Rich
 
Well, for reading thr first file and writin the second, look at the FileSystemObjec. To determine if AB is in the line, look at the InStr function.

[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 
There's also the low tech solution of using the DOS find command:

find "AB" path\inputfile > path\outputfile

then use filesystemobject to open the outputfile for further processing if needed. If not, then you're done.

Jock

"For a list of all the ways technology has failed to improve the quality of life, please press three." - Alice Kahn
 
Thanks to you both. Do you have an example of how to use the InStr function? Looked at the MS script repository, but couldn't find an example...

 
From the WSH help file:
InStr Function
See Also
InStrRev Function
Requirements
Version 1
Returns the position of the first occurrence of one string within another.

InStr([start, ]string1, string2[, compare])
Arguments
start
Optional. Numeric expression that sets the starting position for each search. If omitted, search begins at the first character position. If start contains Null, an error occurs. The start argument is required if compare is specified.
string1
Required. String expression being searched.
string2
Required. String expression searched for.
compare
Optional. Numeric value indicating the kind of comparison to use when evaluating substrings. See Settings section for values. If omitted, a binary comparison is performed.
Settings
The compare argument can have the following values:

Constant Value Description
vbBinaryCompare 0 Perform a binary comparison.
vbTextCompare 1 Perform a textual comparison.

Return Values
The InStr function returns the following values:

If InStr returns
string1 is zero-length 0
string1 is Null Null
string2 is zero-length start
string2 is Null Null
string2 is not found 0
string2 is found within string1 Position at which match is found
start > Len(string2) 0

Remarks
The following examples use InStr to search a string:

Dim SearchString, SearchChar, MyPos
SearchString ="XXpXXpXXPXXP" ' String to search in.
SearchChar = "P" ' Search for "P".
MyPos = Instr(4, SearchString, SearchChar, 1) ' A textual comparison starting at position 4. Returns 6.
MyPos = Instr(1, SearchString, SearchChar, 0) ' A binary comparison starting at position 1. Returns 9.
MyPos = Instr(SearchString, SearchChar) ' Comparison is binary by default (last argument is omitted). Returns 9.
MyPos = Instr(1, SearchString, "W") ' A binary comparison starting at position 1. Returns 0 ("W" is not found).

[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 
If you know the exact text that you need to extract here is a simple way to search and find it in your text. Using this code you can easily adapt it to use the text for whatever you need. This script searches all files in the folder "C:\Logs\" for the the search string that you specify. I hope this helps. This is a little basic but hopefully you get what you need. If not let me know and I would be happy to help.

Thanks,

Ace

'=================================================
Const ForReading = 1
Const TristateFalse = 0
Dim strSearchThis
Dim objFSO
Dim objFolder
Dim objFile, objFileList

Folder = "C:\Logs\" ' change to the path of your files

Set objFSO = WScript.CreateObject("Scripting.FileSystemObject")

If objFSO.FolderExists(Folder) Then
Set objFolder = objFSO.GetFolder(Folder)
Set objFileList = objFolder.Files

For Each objFile in objFileList
strFileName = objFile.Name

Set objTS = objFile.OpenAsTextStream( ForReading, TristateFalse )

strSearchThis = objTS.READ( objFile.Size )

If InStr( strSearchThis, "Search String" ) Then 'Change "Search String" to what you need to search for
WScript.Echo "Found Search String!" ' you can do whatever you want with the string here
Else
WScript.Echo "Did not find Search String!" ' and again here
End If
Next
Else
Set objFSO = Nothing
WScript.Quit
End If
'=================================================
 
Thanks all, I should be able to get on w/ this now.

Cheers,

Rich
 
richardrekos, let me know if you need help writing the text to a new file. The script that I sent will do all files in a specific folder and you can make it write to a new text file easily. I'm happy to help.

Thanks,

Ace
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top