Code Bank
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
 
User Name:
Password:
Remember me
Go Back   ASP Free ForumsProgrammingCode Bank

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 ASP Free Forums Sponsor:
  #1  
Old August 19th, 2004, 02:27 AM
Lehane Lehane is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Aug 2004
Location: South Africa
Posts: 6 Lehane User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via ICQ to Lehane Send a message via MSN to Lehane Send a message via Yahoo to Lehane
Question Joining Text files with a twist

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
Attached Files
File Type: txt test_file_agent_read.txt (198 Bytes, 229 views)
File Type: txt test_file_prod_read.txt (299 Bytes, 269 views)
File Type: txt test_file_write.txt (2 Bytes, 133 views)
File Type: txt This what the result must look like.txt (1.2 KB, 128 views)

Reply With Quote
  #2  
Old August 24th, 2004, 06:41 AM
selwonk's Avatar
selwonk selwonk is offline
Contributing User
ASP Free Frequenter (2500 - 2999 posts)
 
Join Date: Jun 2004
Posts: 2,942 selwonk User rank is Second Lieutenant (5000 - 10000 Reputation Level)selwonk User rank is Second Lieutenant (5000 - 10000 Reputation Level)selwonk User rank is Second Lieutenant (5000 - 10000 Reputation Level)selwonk User rank is Second Lieutenant (5000 - 10000 Reputation Level)selwonk User rank is Second Lieutenant (5000 - 10000 Reputation Level)selwonk User rank is Second Lieutenant (5000 - 10000 Reputation Level)selwonk User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 6 Days 9 h 49 m 28 sec
Reputation Power: 62
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
%>
The output looks like this:
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

Reply With Quote
Reply

Viewing: ASP Free ForumsProgrammingCode Bank > Joining Text files with a twist


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway
Stay green...Green IT