| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
Hi all,
I need to -Read the two files line by line into a variable. -the first file contains agent codes, the second file contains the products. -I need to join each agent code to all the products of the second text file...till there is no more records -and save the output into a result text file. Here's my code: Dim InputFileNum1 As Integer Dim InputFileNum2 As Integer Dim OutputFileNum As Integer Dim Buffer1 As String Dim Buffer2 As String Private Sub Command1_Click() Dim line_f1 As String Dim line_f2 As String Dim final_line As String ' Get the input file handle and open for input-only InputFileNum1 = FreeFile InputFileNum2 = FreeFile Open "C:\Documents and Settings\Administrator\Desktop\New Folder\my test\test_file_agent_read.txt" For Input As InputFileNum1 ** Open "C:\Documents and Settings\Administrator\Desktop\New Folder\my test\test_file_prod_read.txt" For Input As InputFileNum2 ' Get the output file handle and open/create the file OutputFileNum = FreeFile Open "C:\Documents and Settings\Administrator\Desktop\New Folder\my test\test_file_write.txt" For Output As OutputFileNum ' Read each line in the input file one line at a time While Not EOF(InputFileNum1) ' Reads one line of text and assigns it to the buffer and then to string Line Input #InputFileNum1, Buffer1 line_f1 = Buffer1 While Not EOF(InputFileNum2) Line Input #InputFileNum2, Buffer2 line_f2 = Buffer2 Wend final_line = line_f1 & Space(5) & line_f2 Print #OutputFileNum, Buffer1, Buffer2 Wend Close #OutputFileNum Close #InputFileNum1 Close #InputFileNum2 End Sub (NOTE I used ** to display where debugger gives error) ** It gives me a Runtime error '55': File already open Can you please help me...What am i doing wrong? Is there another way? ==Please find attached the sample text files. Thanx in advance! L |
|
#2
|
||||
|
||||
|
Hi
I've simplified it a bit and it seems to work: Code:
<%
' Functions
Function WriteToFile( str_FileName, str_Message )
Set obj_FSO = Server.CreateObject("Scripting.FileSystemObject")
Set obj_OutputFile = obj_FSO.OpenTextFile(str_FileName, 2, True, 0)
obj_OutputFile.WriteLine(str_Message)
obj_OutputFile.Close
Set obj_OutputFile = Nothing
Set obj_FSO = Nothing
End Function
Function ReadTextFile( str_FileName )
Set obj_FSO = Server.CreateObject("Scripting.FileSystemObject")
Set obj_InputFile = obj_FSO.GetFile(str_FileName)
Set obj_TextStream = obj_InputFile.OpenAsTextStream(1, TristateFalse)
str_Text = obj_TextStream.ReadAll
Set obj_InputFile = Nothing
Set obj_FSO = Nothing
ReadTextFile = str_Text
End Function
%>
<%
' Read input files into arrays
InputFile1 = Server.MapPath("test_file_agent_read.txt")
InputFile2 = Server.MapPath("test_file_prod_read.txt")
OutputFile = Server.MapPath("output.txt")
str_InputFile1 = ReadTextFile(InputFile1)
str_InputFile2 = ReadTextFile(InputFile2)
ary_InputFile1 = Split(str_InputFile1,vbcrlf)
ary_InputFile2 = Split(str_InputFile2,vbcrlf)
%>
<%
' Read arrays and concatenate contents
str_Output = ""
For int_Counter = 0 TO UBound(ary_InputFile1)
If Trim(ary_InputFile1(int_Counter)) <> "" Then
For int_InnerCounter = 0 TO UBound(ary_InputFile2)
If Trim(ary_InputFile2(int_InnerCounter)) <> "" Then
str_Output = str_Output & ary_InputFile1(int_Counter) & " " & ary_InputFile2(int_InnerCounter) & vbcrlf
End If
Next
End If
str_Output = str_Output & vbcrlf & vbcrlf
Next
%>
<textarea cols="50" rows="25"><%= str_Output %></textarea>
<%
' Write output file
WriteToFile OutputFile,str_Output
%>
Code:
AQ/01/000 ALE 1 5 10 15 AQ/01/000 GAP 2 6 12 13 AQ/01/000 GRO 3 7 14 15.36 AQ/01/000 HPP 5.36 9.33 15.65 10.33 AQ/01/000 HVP 14.88 87 23.11 5.99 AQ/01/000 MBB 20.33 19 25.66 14.66 AQ/01/000 MFB 18.55 7.99 5.99 36.09 AQ/01/000 PRO 14.22 25.46 23.55 14.88 AQ/01/000 RFM 5.33 65.22 15.22 7.88 AQ/01/000 RHP 32.44 32.66 25.33 14.77 AQ/01/000 RPG 12.33 46.44 48.69 17.24 AQ/01/000 RPV 45.38 75.99 109.38 40.59 AQ/01/001 ALE 1 5 10 15 AQ/01/001 GAP 2 6 12 13 AQ/01/001 GRO 3 7 14 15.36 AQ/01/001 HPP 5.36 9.33 15.65 10.33 AQ/01/001 HVP 14.88 87 23.11 5.99 AQ/01/001 MBB 20.33 19 25.66 14.66 AQ/01/001 MFB 18.55 7.99 5.99 36.09 AQ/01/001 PRO 14.22 25.46 23.55 14.88 AQ/01/001 RFM 5.33 65.22 15.22 7.88 AQ/01/001 RHP 32.44 32.66 25.33 14.77 AQ/01/001 RPG 12.33 46.44 48.69 17.24 AQ/01/001 RPV 45.38 75.99 109.38 40.59 AQ/01/002 ALE 1 5 10 15 AQ/01/002 GAP 2 6 12 13 AQ/01/002 GRO 3 7 14 15.36 AQ/01/002 HPP 5.36 9.33 15.65 10.33 AQ/01/002 HVP 14.88 87 23.11 5.99 AQ/01/002 MBB 20.33 19 25.66 14.66 AQ/01/002 MFB 18.55 7.99 5.99 36.09... MK |
![]() |
| Viewing: ASP Free Forums > Programming > Code Bank > Joining Text files with a twist |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|