Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Manipulating the date in a text file 1

Status
Not open for further replies.

proximity

Technical User
Sep 19, 2002
132
GB
Hi,

I have a text file in which the following line appears at irregular intervals:

Details For Earliest Pick Date : 21/11/06

After this line, records appear in tabular form until the above line appears with a different date on the end. What I want to do is take the date and place it at the end of every line and change it to the next date when it changes.

The code below works on text files where the first line is simply a date: 21/11/06. However, it does not work when the date is preceded by the 'Details for earliest pick date' text. How can I best adapt the code to work with the above scenario?

Dim InFile As Integer
Dim OutFile As Integer
Dim strOneLine As String
Dim strDate As String

InFile = FreeFile

Open "P:\TextFile.txt" For Input As #InFile
OutFile = FreeFile
Open "P:\TextFile2.txt" For Output As #OutFile

Do
Line Input #InFile, strOneLine
If IsDate(strOneLine) Then
strDate = strOneLine
Print #OutFile, strOneLine
Else
Print #OutFile, strOneLine, strDate
End If
Loop Until EOF(InFile)

Close #InFile
Close #OutFile

--
Thanks!
 
proximity,
the reason it does not work with 'Details For Earliest Pick Date : 21/11/06' is because this string will not evaluate to a date ('Details For Earliest Pick Date' is not a date).
what you would need to do is parse the string.
does the date always start at character 15 (for example) and go for a length of 8 characters?
or you could just check to see for some of the words in the string, if they don't appear elsewhere.
regards,
longhair
 
Hi,

Thanks for your reply.

Yes, the date always starts at the same position and is always 8 characters. I just can't figure out how to parse the string???
 
proximity,
take a look at the right function.
something akin to:
Code:
 If IsDate(right(strOneLine,8) Then
regards,
longhair
 
proximity,
Code:
[b]Const cHeader As String = "Details For Earliest Pick Date :"[/b]
Dim InFile As Integer
Dim OutFile As Integer
Dim strOneLine As String
Dim strDate As String

InFile = FreeFile

Open "P:\TextFile.txt" For Input As #InFile
OutFile = FreeFile
Open "P:\TextFile2.txt" For Output As #OutFile

Do
    Line Input #InFile, strOneLine
    If [b]InStr(1, [/b]strOneLine[b], cHeader) > 0[/b] Then
        strDate = [b]Trim(Mid([/b]strOneLine[b], Len(cHeader) + 1))[/b]
        Print #OutFile, strOneLine
    Else
        Print #OutFile, strOneLine, strDate
    End If
Loop Until EOF(InFile)

Close #InFile
Close #OutFile

Hope this helps,
CMP

[small]For the best results do what I'm thinking, not what I'm saying.[/small]
(GMT-07:00) Mountain Time (US & Canada)
 
Hi,

Thank you - that works brilliantly!

--
Steven
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top