September 24th, 2003, 11:11 AM
Parsing multiple Text Files
I would like to parse out only the lines that have "FTG in them. Examples:
The two lines above should be placed into a new file, where I get the chance to save that file were ever I want.
I also need the last two numbers deleted and the FTG deleted, and all the double quotes.
I hope this is possible, and if it is, I sure hope someone helps me out.
Thanks in advance,
September 24th, 2003, 02:02 PM
What language? VBScript? If so:
Use The FileSystemObject to open and read the file and create a new file.
Use Instr() to determine if FTG exists in the line.
Use Mid(), Left() and Right() to extract the code you want as you step through each line.
Use Replace() to remove the double quotes.
You could even split the individual items (delimited by commas) in the line, using the split function, into an array.
This is not hard, but can be tedious. These functions are at:
Last edited by dcarva; September 24th, 2003 at 02:04 PM.
September 24th, 2003, 02:06 PM
Yes, very tedious. As for your advice, thank you, I have pretty much decided that I would have to use Instr(), and the Left & Right(). I also thought about the Replace() but was sure how. I just can't figure out how to do this. I have a script allready that at least pulls out the FTG lines and puts it into a new file. But, it doesn't give you the saveas option nor does it parse out everything I mentioned above. Here is that script:
Dim fs, ts1, sf, s1, ts2, ProjectDir, WshShell, filename, FinalMsg, ErrorMessage, FirstPrompt
Dim strFilter, strInputFileName, Mystring
Const ForReading = 1, ForWriting = 2, ForAppending = 8
FirstPrompt = "Enter Project Directory:"
ErrorMessage = "GoodBye"
FinalMsg = "Map Totals operation complete!"
ProjectDir = InputBox(FirstPrompt,"Map Totals")
Set WshShell = WScript.CreateObject("WScript.Shell")
Set fs = CreateObject("Scripting.FileSystemObject")
'Set ofileSys=CreateObject("Scripting.FileSystemObject" )
If ProjectDir = "" Then
Dummy = WshShell.Popup (ErrorMessage,0,"Map Totals",16)
WshShell.Run "Cmd.exe /C Copy C:\Cablview\" & ProjectDir & "\Dgn\*.txt C:\Cablview\" & ProjectDir & "\Temp\Totals.txt", 2,True
Set ts1 = fs.OpenTextFile("C:\Cablview\" + ProjectDir + "\Temp\Totals.txt", ForReading)
Set ts2 = fs.OpenTextFile("C:\Cablview\" + ProjectDir + "\Temp\Miles.txt", ForWriting, True)
Do While Not ts1.AtEndOfStream
s1 = ts1.ReadLine()
If InStr(s1, "FTG") > 0 Then
'here is were the loop goes
Set ts2 = Nothing
Set ts1 = Nothing
Set fs = Nothing
September 24th, 2003, 02:20 PM
I think that you have to use CreateTextFile to do a Save As. If not, ts.close will save whatever you wrote to it.
"nor does it parse out everything I mentioned above"
What is getting parsed out and what isn't?
September 24th, 2003, 02:45 PM
I need the last two numbers, and their commas parsed out. Also, the FTG column needs to be parsed out, and the all of the double quotes need to be parsed out.
Also, I don't know exactly where in my code that CreateTextFile would be inserted. Sorry for being so ignorant, I'm learning as I go.
September 24th, 2003, 02:54 PM
No problem. To make it easy, you could take each item separated by a comma and have it inserted into an array using Split(). This way, you can reference each item in the array and you don't have to worry about parsing each item out using Mid() or Instr().
Here's a way to replace the quotes:
strVal = Replace(strVal, chr(34), "")
Hope this helps.
September 24th, 2003, 04:12 PM
OK, I have discovered that chr(34) stands for the double quote. I have inserted this line in several places and the script runs, with no errors, yet the double quotes still exist. Also, Not sure how to use Split in my file, considering, lets say in the two row example above, that the 579-617 doesn't always stay three numbers on each side of the dash - it could be 2 numbers on either side, also, the number after the FTG column could be a 2 or three digit number.
September 24th, 2003, 04:33 PM
x = "test" & chr(34) & "hello"
reponse.write(x & "<br>")
x = replace(x, chr(34), "x")
reponse.write(x & "<br>")
You should see:
Say that s1 contains "579-617","FTG",0,190,0,190"
Dim MyString, MyArray
s1 = Replace(s1, chr(34), "")
MyArray = Split(s1, ",", -1, 1)
Response.write ( MyArray(0) & "<br>" )
Response.write ( MyArray(1) & "<br>" )
Response.write ( MyArray(2) & "<br>" )
Response.write ( MyArray(3) & "<br>" )
Response.write ( MyArray(4) & "<br>" )
Response.write ( MyArray(5) & "<br>" )
You will see:
December 9th, 2003, 03:06 PM
Don't know if this problem has been solved or not, but why not just use a For...Loop to step through the items in the array. If you use this method, it will cut down on the amount of coding you have to do:
MyArray = Split(s1, ",")
For i = 0 to UBound(MyArray)
response.write MyArray(i) & "<br>"
This will give you the same result as above.....
Last edited by AirRazr; December 9th, 2003 at 03:10 PM.