|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Stop making mediocre tutorials.The best tutorials are video! Camtasia Studio makes it easy to create engaging, buzz-building screen videos at any size, in any popular format. Download the free trial!
|
|
#1
|
|||
|
|||
|
Parsing multiple Text Files
I have many text files in the same directory that all looks the same. The only thing that is identical is the fourth line, the one that has "FTG" in it.:
"579-617","HCRES",0,0,4,0,4,0 "579-617","HCMDU",0,0,0,0,0,0 "579-617","HCCOM",0,0,0,0,0,0 "579-617","FTG",0,190,0,190 "579-617","CELL","PED0",1 "579-617","CELL","FU",2 "579-617","CELL","UTIL4",1 I would like to be able to parse all the FTG lines out of all the text files in this directory and put them into a new directory. Also, I would love for a dialog box to open and ask, where do you want to save this file (speeking of the new file). Thanks in advance, George Jackson |
|
#2
|
|||
|
|||
|
In generalities, you can use the filesystem scripting object (make a reference to Microsoft Scripting Runtime), set up a loop to iterate through the files one by one.
Do your testing on each file by reading the lines from the file & test for whatever you need to find, & make any changes you need to make. Then pop up a msgbox or use the common dialog control to present a save dialog to save each of the tested files. |
|
#3
|
|||
|
|||
|
Well, Doug, I'm not sure exactly were to begin even with your info.. I mean I have the "Windows 2000 Scripting Bible" but I am still very new at this and am having difficulties trying to get the basics down.
|
|
#4
|
|||
|
|||
|
OK, I have this so far:
Option Explicit Dim FinalMsg, ProjectDir, WshShell, destlocation, NOTICE FinalMsg = "Map Totals complete!" Set WshShell = WScript.CreateObject("WScript.Shell") projectdir = inputbox( "Please Enter the Project:", "MAP TOTALS", , 400,400) If ProjectDir = "" Then Msgbox "Good Bye" Wscript.Quit Else WshShell.Run "Cmd.exe /C Copy C:\Cablview\" & ProjectDir & "\Dgn\*.txt C:\Cablview\" & ProjectDir & "\Temp\Totals.txt", 2,True NOTICE = WshShell.Popup (FinalMsg, 5,"Map Totals",64) End if The results are in a file with this in it: "579-617","HCRES",0,0,4,0,4,0 "579-617","HCMDU",0,0,0,0,0,0 "579-617","HCCOM",0,0,0,0,0,0 "579-617","FTG",0,190,0,190 "579-617","CELL","PED0",1 "579-617","CELL","FU",2 "579-617","CELL","UTIL4",1 "627-580","HCRES",0,0,0,0,0,0 "627-580","HCMDU",0,0,0,0,0,0 "627-580","HCCOM",0,0,0,0,0,0 "627-580","FTG",843,0,0,843 "627-580","CELL","PLETP",4 "627-580","CELL","TITLE",1 I would like to parse just the FTG lines into another text file. And then I would love for a save as dialog box to open asking where do you want to save it. Thanks for any further help. G. Jackson |
|
#5
|
|||
|
|||
|
Your code is in VBScript. Here is a sample that will pull the FTG lines out to another file (VB6). This should give you some ideas, it would be pretty easy to convert to VBScript.
Code:
Private Sub Command1_Click()
'
Dim fs As Scripting.FileSystemObject
Dim ts1 As Scripting.TextStream, ts2 As Scripting.TextStream
Dim sf As String
Dim s1 As String
'
Set fs = New Scripting.FileSystemObject
Set ts1 = fs.OpenTextFile("<c:\aaa\file_in.txt>", ForReading)
Set ts2 = fs.OpenTextFile("<c:\aaa\file_out.txt>", ForWriting, True)
Do While Not ts1.AtEndOfStream
s1 = ts1.ReadLine()
If InStr(s1, "FTG") > 0 Then
ts2.WriteLine s1
End If
Loop
'
ts2.Close
ts1.Close
Set ts2 = Nothing
Set ts1 = Nothing
Set fs = Nothing
'
End Sub
|
|
#6
|
|||
|
|||
|
Well,Doug G!Nice work!...
|
|
#7
|
|||
|
|||
|
Thanks, Doug, but I can't seem to get it to work. It always errors at the "AS" part of the line saying its an "Expected End of Statement"
So, I think I need to ask, what code did you wright the above in? It looks very similar to VBSCRIPT, is it JScript? Also, my computer doesn't like the "Scripting" part saying that it's undefined, so I try to add a DIM statement, which I have a feeling is wrong to do, and then it errors again with "Class Not Defined". I am doing all of this in Notepad. Not sure were to go with it now. Thanks again, GJ |
|
#8
|
|||
|
|||
|
The above code fragment is VB (not VBScript), so it won't work with wscript/cscript.exe. I don't get exactly what you want but maybe this code snippit will help you:
Code:
Option Explicit
' Some FSO constants
Const ForReading = 1, _
ForWriting = 2, _
ForAppending = 8
' Reads a file
Public Function FileRead(pFilename)
Dim lFSO
Set lFSO = CreateObject("Scripting.FileSystemObject")
Dim lStream
Set lStream = lFSO.OpenTextFile(pFilename, ForReading)
File_ReadAll = lStream.ReadAll
lStream.Close
End Function
' Writes a file
Public Function FileWrite(pFilename, pContent)
Dim lFSO
Set lFSO = CreateObject("Scripting.FileSystemObject")
Dim lStream
Set lStream = lFSO.OpenTextFile(pFilename, ForAppending)
lStream.Write pContent
lStream.Close
End Function
' Filters all line with "FTG"
Public Function MyFilter()
Dim lInput, lOutput
' this should be...
' lInput = FileRead("YourInput.txt")
' ... but for easier testing its just an internal
' variable
lInput = """579-617"",""HCRES"",0,0,4,0,4,0" & vbCrLf _
& """579-617"",""HCMDU"",0,0,0,0,0,0" & vbCrLf _
& """579-617"",""HCCOM"",0,0,0,0,0,0" & vbCrLf _
& """579-617"",""FTG"",0,190,0,190" _
Dim lLine
For Each lLine In Split(lInput, vbCrLf)
Dim lTokens
lTokens = Split(lLine, ",")
If (UBound(lTokens) > 1) Then
If (Trim(lTokens(1)) = """FTG""") Then
lOutput = lOutput & lLine & vbCrLf
End If
End If
Next
' this should be...
' FileWrite "YourOutput.txt", lOutput
' ... but for easier testing its just a msgbos
MsgBox lOutput
End Function
Call MyFilter()
|
|
#9
|
|||
|
|||
|
OK, wingman, I don't know how to convert these scipts to .vbs files. To wingman, I am needing to pull all the lines with "FTG" in it, in multiple text files, out and into one single file.
It is erroring out as it is being saved as a .vbs file. This is what I have with your script: Option Explicit '=================================== ' Some FSO constants '=================================== Const ForReading = 1, _ ForWriting = 2, _ ForAppending = 8 Dim projectdir, FinalMsg, WshShell FinalMsg = "Map Totals complete!" projectdir = inputbox( "Please Enter the Project:", "MAP TOTALS", , 400,400) If ProjectDir = "" Then Msgbox "Good Bye" Wscript.Quit Else '=================================== ' Reads a file '=================================== Public Function FileRead(pFilename) Dim lFSO Set lFSO = CreateObject("Scripting.FileSystemObject") Dim lStream Set lStream = lFSO.OpenTextFile("C:\Cablview\" + ProjectDir + "\Temp\Totals.txt", ForReading) File_ReadAll = lStream.AtEndOfStream lStream.Close End Function '=================================== ' Writes a file '=================================== Public Function FileWrite(pFilename, pContent) Dim lFSO Set lFSO = CreateObject("Scripting.FileSystemObject") Dim lStream Set lStream = lFSO.OpenTextFile("C:\Cablview\" + ProjectDir + "\Temp\Miles.txt", ForAppending) lStream.Write pContent lStream.Close End Function '=================================== ' Filters all line with "FTG" '=================================== Public Function MyFilter() Dim lInput, lOutput ' this should be... lInput = FileRead("C:\Cablview\" + ProjectDir + "\Temp\Miles.txt") ' ... but for easier testing its just an internal ' variable 'lInput = """579-617"",""HCRES"",0,0,4,0,4,0" & vbCrLf _ '& """579-617"",""HCMDU"",0,0,0,0,0,0" & vbCrLf _ '& """579-617"",""HCCOM"",0,0,0,0,0,0" & vbCrLf _ '& """579-617"",""FTG"",0,190,0,190" _ Dim lLine For Each lLine In Split(lInput, vbCrLf) Dim lTokens lTokens = Split(lLine, ",") If (UBound(lTokens) > 1) Then If (Trim(lTokens(1)) = """FTG""") Then lOutput = lOutput & lLine & vbCrLf End If End If Next ' this should be... ' FileWrite "YourOutput.txt", lOutput ' ... but for easier testing its just a msgbos MsgBox lOutput End Function End If Call MyFilter() |
|
#10
|
|||
|
|||
|
This is what I have:
Dim fs, ts1, sf, s1, ts2, ProjectDir Const ForReading = 1, ForWriting = 2, ForAppending = 8 FirstPrompt = "Enter Project Directory:" ErrorMessage = "Operation failed or was canceled!" & vbCrLf & vbCrLf &_ "Please repeat the operation." FinalMsg = "Map Totals operation complete!" ProjectDir = InputBox(FirstPrompt,"Map Totals") If ProjectDir = "" Then Dummy = WshShell.Popup (ErrorMessage,0,"Map Totals",16) Wscript.Quit Else 'Dim fs As Scripting.FileSystemObject 'Dim ts1 As Scripting.TextStream, ts2 As Scripting.TextStream 'Dim sf As String 'Dim s1 As String ' WshShell.Run "Cmd.exe /C Copy "C:\Cablview\" & ProjectDir & "\Dgn\*.lst" "C:\Cablview\" & ProjectDir & "\Temp\Totals.txt", 2,True" Set fs = CreateObject("Scripting.FileSystemObject") Set ts1 = fs.OpenTextFile("C:\Cablview\" + ProjectDir + "\Dgn\Tables.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 ts2.WriteLine s1 End If Loop ' ts2.Close ts1.Close Set ts2 = Nothing Set ts1 = Nothing Set fs = Nothing ' End If And it gives a Windows SCript Host error # 800A0401 "Expected End of statement" on Line 28 and Character 37. I can't seem to get this to work. Please, will someone help. Thanks. GJ |
|
#11
|
|||
|
|||
|
It looks like you have an extra line break in this:
WshShell.Run "Cmd.exe /C Copy "C:\Cablview\" & ProjectDir & "\Dgn\*.lst" "C:\Cablview\" & ProjectDir & "\Temp\Totals.txt", 2,True" This should be on one line if it's not. |
|
#12
|
|||
|
|||
|
Yes the line is one, it does not overlay to the next line. Are all the double quotes correct?
|
|
#13
|
|||
|
|||
|
U try it!I modified it!
WshShell.Run "Cmd.exe /C Copy C:\Cablview\" & ProjectDir & "\Dgn\*.lst C:\Cablview\" & ProjectDir & "\Temp\Totals.txt", 2,True |
|
#14
|
|||
|
|||
|
OK, I will post my code for the record. cleverpig, you seem to have fixed one problem, but there is still errors and I am still not getting the final result with the parsed txt file. The error I'm getting now pertains to that row, starting with WshShell. It says there is an "object required: "" - being the double quote is the required object. I don't know. I really hope someone can help me finish this or give me a link that shows how to parse or something. Here is my code (and I do not have word wrap checked in my notepad):
Dim fs, ts1, sf, s1, ts2, ProjectDir, WshShell Const ForReading = 1, ForWriting = 2, ForAppending = 8 FirstPrompt = "Enter Project Directory:" ErrorMessage = "Operation failed or was canceled!" & vbCrLf & vbCrLf &_ "Please repeat the operation." FinalMsg = "Map Totals operation complete!" ProjectDir = InputBox(FirstPrompt,"Map Totals") If ProjectDir = "" Then Dummy = WshShell.Popup (ErrorMessage,0,"Map Totals",16) Wscript.Quit Else 'Dim fs As Scripting.FileSystemObject 'Dim ts1 As Scripting.TextStream, ts2 As Scripting.TextStream 'Dim sf As String 'Dim s1 As String ' WshShell.Run "Cmd.exe /C Copy C:\Cablview\" & ProjectDir & "\Dgn\*.lst C:\Cablview\" & ProjectDir & "\Temp\Totals.txt", 2,True Set fs = CreateObject("Scripting.FileSystemObject") Set ts1 = fs.OpenTextFile("C:\Cablview\" + ProjectDir + "\Dgn\Tables.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 ts2.WriteLine s1 End If Loop ' ts2.Close ts1.Close Set ts2 = Nothing Set ts1 = Nothing Set fs = Nothing ' End If |