Visual Basic Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming Languages - MoreVisual Basic Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
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  
Old September 18th, 2003, 08:40 AM
jacksonbilly jacksonbilly is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2003
Posts: 15 jacksonbilly User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 9 m 28 sec
Reputation Power: 0
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

Reply With Quote
  #2  
Old September 18th, 2003, 10:45 AM
Doug G Doug G is offline
Grumpier Old Moderator
Dev Shed God 12th Plane (10500 - 10999 posts)
 
Join Date: Jun 2003
Posts: 10,717 Doug G User rank is Brigadier General (60000 - 70000 Reputation Level)Doug G User rank is Brigadier General (60000 - 70000 Reputation Level)Doug G User rank is Brigadier General (60000 - 70000 Reputation Level)Doug G User rank is Brigadier General (60000 - 70000 Reputation Level)Doug G User rank is Brigadier General (60000 - 70000 Reputation Level)Doug G User rank is Brigadier General (60000 - 70000 Reputation Level)Doug G User rank is Brigadier General (60000 - 70000 Reputation Level)Doug G User rank is Brigadier General (60000 - 70000 Reputation Level)Doug G User rank is Brigadier General (60000 - 70000 Reputation Level)Doug G User rank is Brigadier General (60000 - 70000 Reputation Level)Doug G User rank is Brigadier General (60000 - 70000 Reputation Level)Doug G User rank is Brigadier General (60000 - 70000 Reputation Level)Doug G User rank is Brigadier General (60000 - 70000 Reputation Level) 
Time spent in forums: 1 Month 40 m 34 sec
Reputation Power: 688
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.

Reply With Quote
  #3  
Old September 18th, 2003, 11:12 AM
jacksonbilly jacksonbilly is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2003
Posts: 15 jacksonbilly User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 9 m 28 sec
Reputation Power: 0
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.

Reply With Quote
  #4  
Old September 18th, 2003, 02:24 PM
jacksonbilly jacksonbilly is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2003
Posts: 15 jacksonbilly User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 9 m 28 sec
Reputation Power: 0
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

Reply With Quote
  #5  
Old September 18th, 2003, 06:34 PM
Doug G Doug G is offline
Grumpier Old Moderator
Dev Shed God 12th Plane (10500 - 10999 posts)
 
Join Date: Jun 2003
Posts: 10,717 Doug G User rank is Brigadier General (60000 - 70000 Reputation Level)Doug G User rank is Brigadier General (60000 - 70000 Reputation Level)Doug G User rank is Brigadier General (60000 - 70000 Reputation Level)Doug G User rank is Brigadier General (60000 - 70000 Reputation Level)Doug G User rank is Brigadier General (60000 - 70000 Reputation Level)Doug G User rank is Brigadier General (60000 - 70000 Reputation Level)Doug G User rank is Brigadier General (60000 - 70000 Reputation Level)Doug G User rank is Brigadier General (60000 - 70000 Reputation Level)Doug G User rank is Brigadier General (60000 - 70000 Reputation Level)Doug G User rank is Brigadier General (60000 - 70000 Reputation Level)Doug G User rank is Brigadier General (60000 - 70000 Reputation Level)Doug G User rank is Brigadier General (60000 - 70000 Reputation Level)Doug G User rank is Brigadier General (60000 - 70000 Reputation Level) 
Time spent in forums: 1 Month 40 m 34 sec
Reputation Power: 688
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

Reply With Quote
  #6  
Old September 19th, 2003, 12:20 AM
cleverpig cleverpig is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Jul 2003
Posts: 1,152 cleverpig User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 7
Send a message via MSN to cleverpig
Well,Doug G!Nice work!...

Reply With Quote
  #7  
Old September 19th, 2003, 12:10 PM
jacksonbilly jacksonbilly is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2003
Posts: 15 jacksonbilly User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 9 m 28 sec
Reputation Power: 0
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

Reply With Quote
  #8  
Old September 19th, 2003, 12:23 PM
Wingman Wingman is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2002
Location: Bavaria, Germany
Posts: 140 Wingman User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 40 m 41 sec
Reputation Power: 6
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()

Reply With Quote
  #9  
Old September 19th, 2003, 12:39 PM
jacksonbilly jacksonbilly is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2003
Posts: 15 jacksonbilly User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 9 m 28 sec
Reputation Power: 0
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()

Reply With Quote
  #10  
Old September 19th, 2003, 02:37 PM
jacksonbilly jacksonbilly is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2003
Posts: 15 jacksonbilly User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 9 m 28 sec
Reputation Power: 0
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

Reply With Quote
  #11  
Old September 19th, 2003, 02:42 PM
Doug G Doug G is offline
Grumpier Old Moderator
Dev Shed God 12th Plane (10500 - 10999 posts)
 
Join Date: Jun 2003
Posts: 10,717 Doug G User rank is Brigadier General (60000 - 70000 Reputation Level)Doug G User rank is Brigadier General (60000 - 70000 Reputation Level)Doug G User rank is Brigadier General (60000 - 70000 Reputation Level)Doug G User rank is Brigadier General (60000 - 70000 Reputation Level)Doug G User rank is Brigadier General (60000 - 70000 Reputation Level)Doug G User rank is Brigadier General (60000 - 70000 Reputation Level)Doug G User rank is Brigadier General (60000 - 70000 Reputation Level)Doug G User rank is Brigadier General (60000 - 70000 Reputation Level)Doug G User rank is Brigadier General (60000 - 70000 Reputation Level)Doug G User rank is Brigadier General (60000 - 70000 Reputation Level)Doug G User rank is Brigadier General (60000 - 70000 Reputation Level)Doug G User rank is Brigadier General (60000 - 70000 Reputation Level)Doug G User rank is Brigadier General (60000 - 70000 Reputation Level) 
Time spent in forums: 1 Month 40 m 34 sec
Reputation Power: 688
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.

Reply With Quote
  #12  
Old September 19th, 2003, 02:47 PM
jacksonbilly jacksonbilly is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2003
Posts: 15 jacksonbilly User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 9 m 28 sec
Reputation Power: 0
Yes the line is one, it does not overlay to the next line. Are all the double quotes correct?

Reply With Quote
  #13  
Old September 20th, 2003, 11:05 AM
cleverpig cleverpig is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Jul 2003
Posts: 1,152 cleverpig User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 7
Send a message via MSN to cleverpig
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

Reply With Quote
  #14  
Old September 22nd, 2003, 07:52 AM
jacksonbilly jacksonbilly is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2003
Posts: 15 jacksonbilly User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 9 m 28 sec
Reputation Power: 0
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

Reply With Quote