|
|
|||||||||
|
|||||||||
|
|||||||||
| |
||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
Parsing multiple Text Files
I would like to parse out only the lines that have "FTG in them. Examples:
"579-617","FTG",0,190,0,190 "627-580","FTG",843,0,0,843 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, GJ |
|
#2
|
||||
|
||||
|
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: http://msdn.microsoft.com/library/d...rifunctions.asp Last edited by dcarva : September 24th, 2003 at 02:04 PM. |
|
#3
|
|||
|
|||
|
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) 'Wscript.Quit Else 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) ReadTotals Sub ReadTotals() Do While Not ts1.AtEndOfStream s1 = ts1.ReadLine() If InStr(s1, "FTG") > 0 Then 'here is were the loop goes ts2.WriteLine s1 End If Loop End Sub ts2.Close ts1.Close Set ts2 = Nothing Set ts1 = Nothing Set fs = Nothing End If |
|
#4
|
||||
|
||||
|
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? |
|
#5
|
|||
|
|||
|
579-617","FTG",0,190,0,190
"627-580","FTG",843,0,0,843 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. Thanks, GJ |
|
#6
|
||||
|
||||
|
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().
http://msdn.microsoft.com/library/d.../vsfctsplit.asp Here's a way to replace the quotes: Dim strVal strVal = Replace(strVal, chr(34), "") Hope this helps. |
|
#7
|
|||
|
|||
|
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.
Thanks, GJ |
|
#8
|
||||
|
||||
|
1. Replace
-------------- dim x x = "test" & chr(34) & "hello" reponse.write(x & "<br>") x = replace(x, chr(34), "x") reponse.write(x & "<br>") You should see: test"hello testxhello 2. Split --------- 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: 579-617 FTG 0 190 0 190 |
|
#9
|
|||
|
|||
|
Quote:
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>" Next This will give you the same result as above..... Last edited by AirRazr : December 9th, 2003 at 03:10 PM. |
![]() |
| Viewing: ASP Free Forums > Programming > HTML, JavaScript And CSS Help > Parsing multiple Text Files |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|