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 derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Text file read and perform "if/then" - then write new file 1

Status
Not open for further replies.

jcw5107

Technical User
Jan 31, 2007
66
US
I have a text file that I need to add data into each line based on a if/then function...
Below is a sample of the text file... Position 8 is where I need to look and position 14 is where I want to put the result of the true part of the if/then function..
Sample:
3719 N256UP KAMA
4030 N676UP KUNK
4331 N462UP KAMA

So something like:
If left(strLine,8) = 2 then
left(strLine,14) = 1
elseif left(strLine,8) = 6 then
left(strLine,14) = 3

This is what I am working with - I keep striking out however..

Sub VisitTallyTest()
Dim db As DAO.Database
Dim tbl As DAO.TableDef
Dim strLine 'As Variant
Dim strType As Variant
Dim strImportFile As String
Dim strNewfile As String
Set db = CurrentDb
strImportFile = "D:\UPSDATA\VisitTallyMain.txt"
strNewfile = "D:\UPSDATA\VisitTally.txt"
Open "D:\UPSDATA\VisitTally.txt" For Input As #1
Open strImportFile For Output As #2
Do Until EOF(1)
Line Input #1, strLine
If Left(strLine, 8) Like "1" Then
strType = Left(strLine, 14) = 5
Print #2, Left(strType, 175)
End If
Loop
Close #1
Close #2
Kill strNewfile
'VisitTallyMainSpec
End Sub
 
A few notes:

Code:
Sub VisitTallyTest()
Dim strLine 'As Variant
Dim strType As Variant
Dim strImportFile As String
Dim strNewfile As String

strImportFile = "D:\UPSDATA\VisitTallyMain.txt" 'Read From
strNewfile = "D:\UPSDATA\VisitTally.txt"        'Write To

Open strImportFile For Input As #1
Open strNewfile For Output As #2
Do Until EOF(1)
    Line Input #1, strLine
    If Mid(strLine, 8, 1) = 6 Then
        Mid(strLine, 14, 1) = 5
        Print #2, strLine
    End If
Loop
Close #1
Close #2

End Sub
 
Per your first example of multiple conditions

Sub VisitTallyTest()
Dim strLine, strType as Variant
Dim strItem, strImportFile, strNewfile As String
strImportFile = "D:\UPSDATA\VisitTallyMain.txt"
strNewfile = "D:\UPSDATA\VisitTally.txt"

Open strImportFile For Input As #1
Open strNewfile For Output As #2

Do Until EOF(1)
Line Input #1, strLine
Select Case Mid(strLine, 8, 1)
Case "2"
Mid(strLine, 14, 1) = 1
Case "6"
Mid(strLine, 14, 1) = 3
End Select
Print #2, strLine
Loop
Close #1
Close #2
End Sub
 
Code:
If Mid(strLine, 8, 1) = 6 Then
    strLine = Left(strLine, 13) & 1
Else
    strLine = Left(strLine, 13) & 3
End If


Randy
 
All:

Thank you very much...!!
Just awesome..!!

I ended up using the example below. Works perfect...!!
Thank you..!!
jcw5107

Sub VisitTallyTest()
Dim strLine 'As Variant
Dim strType As Variant
Dim strImportFile As String
Dim strNewfile As String

strImportFile = "D:\UPSDATA\VisitTally.txt" 'Read From
strNewfile = "D:\UPSDATA\VisitTallyMain.txt" 'Write To

Open strImportFile For Input As #1
Open strNewfile For Output As #2
Do Until EOF(1)
Line Input #1, strLine
Select Case Mid(strLine, 8, 1)
Case "1"
Mid(strLine, 14, 1) = 5
Case "2"
Mid(strLine, 14, 1) = 7
Case "3"
Mid(strLine, 14, 1) = 4
Case "4"
Mid(strLine, 14, 1) = 3
Case "5"
Mid(strLine, 14, 1) = 2
Case "6"
Mid(strLine, 14, 1) = 2
Case "7"
Mid(strLine, 14, 1) = 6
Case "8"
Mid(strLine, 14, 1) = 6
Case "9"
Mid(strLine, 14, 1) = 1
End Select
Print #2, strLine
Loop
Close #1
Close #2

End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top