Inporting data from a group of .prn files

  • Thread starter Thread starter Raul
  • Start date Start date
R

Raul

I need to be able to select a group of .prn files and
then extract the name of the file, the date and time from
the [datetime] section, and all the data from the [data]
section. The number of rows in the data section can
vary. See the example of the .prn file contents below.

I haven't done this before and would really appreciate
any suggestions.

Thanks,
Raul

[datetime]
Date=04/01/04
Time=5:49:33 AM
zero_line=average
t_high=1
t_low=-1
r_high=.5
r_low=-.5
number_of_groups=1
begin_ignore=0
end_ignore=0
last_ignore=0
UOM=E

[FIELDS]

[data]
1=46.93
2=46.32
3=46.17
4=46.77
5=47.31
6=48.35
7=49.55
8=50.2
9=49.96
10=49.84
11=49.28
12=48.68
13=47.62
14=48.76
15=48.66
16=48.64
17=47.36
 
Raul,
The following code contains all the essential ingredients. You don't really
get to "pick" the files. The code reads all *.prn files from the specified
cPath directory. You can customize or elaborate to select the files
differently. You might get all the filenames first, display them in a
userform, allow the user to select some, then process the ones selected. The
code assumes the date come before the time which comes before the data. The
code also assumes that everything below the [data] label is data. It doesn't
look for any signal to stop taking in data, except if it hits the end of the
file. It assumes that all lines with data have a "=" character that
separates the line label from the data.
Bob

Option Explicit

Sub Main()
Dim sfname As String
Dim ifnum As Integer
Dim sLine As String
Dim col As New Collection
Dim v As Variant
Dim i As Long
Dim sData As String
Const cPath As String = "K:\_prn\"

'Get an available file number
ifnum = FreeFile
'Get the first file, if any.
sfname = Dir(cPath & "*.prn")

Do While Len(sfname) > 0
'Open the file
Open cPath & sfname For Input As ifnum
'Stash the file name and its key
Call col.Add(sfname, "file")
'get a line from the file
Line Input #ifnum, sLine

'Get the date and time
For Each v In Array("Date=", "Time=")
Do While Not EOF(ifnum)
If Left(sLine, Len(v)) = v Then
'Stash the date, time
'include their keys
Call col.Add(Mid(sLine, Len(v) + 1), _
Replace(v, "=", ""))
Exit Do
End If
Line Input #ifnum, sLine
Loop
Next v

'Find the beginning of the data
Do While Not EOF(ifnum)
Line Input #ifnum, sLine
If Left(sLine, Len("[data]")) = _
"[data]" Then Exit Do
Loop

'Get the data
Do While Not EOF(ifnum)
Line Input #ifnum, sLine
If InStr(1, sLine, "=") > 0 Then 'here's data!
sData = Mid(sLine, InStr(1, sLine, "=") + 1)
'Stash the data and give it a key, too
Call col.Add(sData, Left(sLine, _
Len(sLine) - Len(sData) - 1))
End If
Loop

'Close the file
Close ifnum

'Do something with the data
Debug.Print "----------------"
'Illustrate use of the keys
Debug.Print "File: " & col.Item("file")
Debug.Print "Date: " & col.Item("date")
Debug.Print "Time: " & col.Item("time")
For i = 4 To col.Count
Debug.Print col.Item(i)
Next i

'Toss out the data to start again.
Set col = Nothing

'Get the next file, if any
sfname = Dir()
Loop


End Sub


Raul said:
I need to be able to select a group of .prn files and
then extract the name of the file, the date and time from
the [datetime] section, and all the data from the [data]
section. The number of rows in the data section can
vary. See the example of the .prn file contents below.

I haven't done this before and would really appreciate
any suggestions.

Thanks,
Raul

[datetime]
Date=04/01/04
Time=5:49:33 AM
zero_line=average
t_high=1
t_low=-1
r_high=.5
r_low=-.5
number_of_groups=1
begin_ignore=0
end_ignore=0
last_ignore=0
UOM=E

[FIELDS]

[data]
1=46.93
2=46.32
3=46.17
4=46.77
5=47.31
6=48.35
7=49.55
8=50.2
9=49.96
10=49.84
11=49.28
12=48.68
13=47.62
14=48.76
15=48.66
16=48.64
17=47.36
 
Bob,
Wow! This is really incredible. I was expecting some
suggestions and ended up getting the whole kit and
kaaboodle instead. The code looks pretty elegant to me
and is also well documented. This is a great learning
experience for me. I can't thank you enough.

Thanks again,
Raul
-----Original Message-----
Raul,
The following code contains all the essential ingredients. You don't really
get to "pick" the files. The code reads all *.prn files from the specified
cPath directory. You can customize or elaborate to select the files
differently. You might get all the filenames first, display them in a
userform, allow the user to select some, then process the ones selected. The
code assumes the date come before the time which comes before the data. The
code also assumes that everything below the [data] label is data. It doesn't
look for any signal to stop taking in data, except if it hits the end of the
file. It assumes that all lines with data have a "=" character that
separates the line label from the data.
Bob

Option Explicit

Sub Main()
Dim sfname As String
Dim ifnum As Integer
Dim sLine As String
Dim col As New Collection
Dim v As Variant
Dim i As Long
Dim sData As String
Const cPath As String = "K:\_prn\"

'Get an available file number
ifnum = FreeFile
'Get the first file, if any.
sfname = Dir(cPath & "*.prn")

Do While Len(sfname) > 0
'Open the file
Open cPath & sfname For Input As ifnum
'Stash the file name and its key
Call col.Add(sfname, "file")
'get a line from the file
Line Input #ifnum, sLine

'Get the date and time
For Each v In Array("Date=", "Time=")
Do While Not EOF(ifnum)
If Left(sLine, Len(v)) = v Then
'Stash the date, time
'include their keys
Call col.Add(Mid(sLine, Len(v) + 1), _
Replace(v, "=", ""))
Exit Do
End If
Line Input #ifnum, sLine
Loop
Next v

'Find the beginning of the data
Do While Not EOF(ifnum)
Line Input #ifnum, sLine
If Left(sLine, Len("[data]")) = _
"[data]" Then Exit Do
Loop

'Get the data
Do While Not EOF(ifnum)
Line Input #ifnum, sLine
If InStr(1, sLine, "=") > 0 Then 'here's data!
sData = Mid(sLine, InStr(1, sLine, "=") + 1)
'Stash the data and give it a key, too
Call col.Add(sData, Left(sLine, _
Len(sLine) - Len(sData) - 1))
End If
Loop

'Close the file
Close ifnum

'Do something with the data
Debug.Print "----------------"
'Illustrate use of the keys
Debug.Print "File: " & col.Item("file")
Debug.Print "Date: " & col.Item("date")
Debug.Print "Time: " & col.Item("time")
For i = 4 To col.Count
Debug.Print col.Item(i)
Next i

'Toss out the data to start again.
Set col = Nothing

'Get the next file, if any
sfname = Dir()
Loop


End Sub


I need to be able to select a group of .prn files and
then extract the name of the file, the date and time from
the [datetime] section, and all the data from the [data]
section. The number of rows in the data section can
vary. See the example of the .prn file contents below.

I haven't done this before and would really appreciate
any suggestions.

Thanks,
Raul

[datetime]
Date=04/01/04
Time=5:49:33 AM
zero_line=average
t_high=1
t_low=-1
r_high=.5
r_low=-.5
number_of_groups=1
begin_ignore=0
end_ignore=0
last_ignore=0
UOM=E

[FIELDS]

[data]
1=46.93
2=46.32
3=46.17
4=46.77
5=47.31
6=48.35
7=49.55
8=50.2
9=49.96
10=49.84
11=49.28
12=48.68
13=47.62
14=48.76
15=48.66
16=48.64
17=47.36


.
 
Raul,
Thanks for the thanks. Sometimes, I just don't know where to stop with my
"suggestions." I know from experience that working examples are most
informative. One thing leads to another and if the task is small enough,
I've written a small program. Thanks for your concise problem definition and
sample data. Skill in posing the question is an often under-rated
contribution to a solution.
Bob

Raul said:
Bob,
Wow! This is really incredible. I was expecting some
suggestions and ended up getting the whole kit and
kaaboodle instead. The code looks pretty elegant to me
and is also well documented. This is a great learning
experience for me. I can't thank you enough.

Thanks again,
Raul
-----Original Message-----
Raul,
The following code contains all the essential ingredients. You don't really
get to "pick" the files. The code reads all *.prn files from the specified
cPath directory. You can customize or elaborate to select the files
differently. You might get all the filenames first, display them in a
userform, allow the user to select some, then process the ones selected. The
code assumes the date come before the time which comes before the data. The
code also assumes that everything below the [data] label is data. It doesn't
look for any signal to stop taking in data, except if it hits the end of the
file. It assumes that all lines with data have a "=" character that
separates the line label from the data.
Bob

Option Explicit

Sub Main()
Dim sfname As String
Dim ifnum As Integer
Dim sLine As String
Dim col As New Collection
Dim v As Variant
Dim i As Long
Dim sData As String
Const cPath As String = "K:\_prn\"

'Get an available file number
ifnum = FreeFile
'Get the first file, if any.
sfname = Dir(cPath & "*.prn")

Do While Len(sfname) > 0
'Open the file
Open cPath & sfname For Input As ifnum
'Stash the file name and its key
Call col.Add(sfname, "file")
'get a line from the file
Line Input #ifnum, sLine

'Get the date and time
For Each v In Array("Date=", "Time=")
Do While Not EOF(ifnum)
If Left(sLine, Len(v)) = v Then
'Stash the date, time
'include their keys
Call col.Add(Mid(sLine, Len(v) + 1), _
Replace(v, "=", ""))
Exit Do
End If
Line Input #ifnum, sLine
Loop
Next v

'Find the beginning of the data
Do While Not EOF(ifnum)
Line Input #ifnum, sLine
If Left(sLine, Len("[data]")) = _
"[data]" Then Exit Do
Loop

'Get the data
Do While Not EOF(ifnum)
Line Input #ifnum, sLine
If InStr(1, sLine, "=") > 0 Then 'here's data!
sData = Mid(sLine, InStr(1, sLine, "=") + 1)
'Stash the data and give it a key, too
Call col.Add(sData, Left(sLine, _
Len(sLine) - Len(sData) - 1))
End If
Loop

'Close the file
Close ifnum

'Do something with the data
Debug.Print "----------------"
'Illustrate use of the keys
Debug.Print "File: " & col.Item("file")
Debug.Print "Date: " & col.Item("date")
Debug.Print "Time: " & col.Item("time")
For i = 4 To col.Count
Debug.Print col.Item(i)
Next i

'Toss out the data to start again.
Set col = Nothing

'Get the next file, if any
sfname = Dir()
Loop


End Sub


I need to be able to select a group of .prn files and
then extract the name of the file, the date and time from
the [datetime] section, and all the data from the [data]
section. The number of rows in the data section can
vary. See the example of the .prn file contents below.

I haven't done this before and would really appreciate
any suggestions.

Thanks,
Raul

[datetime]
Date=04/01/04
Time=5:49:33 AM
zero_line=average
t_high=1
t_low=-1
r_high=.5
r_low=-.5
number_of_groups=1
begin_ignore=0
end_ignore=0
last_ignore=0
UOM=E

[FIELDS]

[data]
1=46.93
2=46.32
3=46.17
4=46.77
5=47.31
6=48.35
7=49.55
8=50.2
9=49.96
10=49.84
11=49.28
12=48.68
13=47.62
14=48.76
15=48.66
16=48.64
17=47.36


.
 
Back
Top