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

reading from text file running a for loop..HELP!!

Status
Not open for further replies.

impulse24

IS-IT--Management
Jul 13, 2001
167
US
Hi,

I have Ascii text files that have many lines(more than 50,000). I want to open the ASCII text File, and for every 2000 lines create a new file, with a sequential name. I am stuck

Here's where I am so far:

Open "C:\test\index.txt" For Input As #1
Do Until EOF(1)
Line Input #1, Data
For i = 0 to 2000
Open "C:\test\" & i & ".txt" For Append As #2
Print #2, Data
Next i
Close #2
Loop
Close #1

Obviously the above code, just places each line from the input file 2000 times, into a new file. Im stuck and don't know where to include the loop..Can someone please help me.
 
INPULSE24,

There are many problems with your code. First, you are creating 2000 files. Not putting 2000 lines in each file. Second, you are reassigning file #2 every time you use the loop.

Based on what you said, I'm guessing you want to do something like the following.

---------------------------------------
dim data as string
dim linecount as integer
dim filecount as integer
dim i as integer

linecount = 0
filecount = 1
Open "C:\test\index.txt" For Input As #1
Open "C:\test\1.txt" for Append as #2

while not EOF(1)
Line Input #1, Data
Print #2, Data
linecount = linecount + 1

if linecount >= 2000 then
close #2
filecount = filecount + 1
open "C:\test\" & filecount & ".txt" for Append as #2
linecount = 0
endif
wend
Close #1

---------------------------------------

basic algorithm:

while the file is not empty
read a line
write the line
incrememnt the line counter

if the lines output is 2000
close the output file
increment the output file counter
open the next file
reset the line count
endif
wend

The loop you have in your original code is not a good idea. For a couple of reasons. First, it is not accomplishing what you want. And second, if the file is not an even multiple of 2000 lines, it will error out.

Hope that helps a bit.

-crater
 
You've got most of the pieces of the puzzle, you've just put them together in the wrong order. You also need to have two counter variables, one for the number of new files you're creating and one to count to 2000 lines per file. As it is, you have one variable trying to do the work of both of them. It's an easy fix.

Starting from the top, you've opened the source file. Now create a variable (NumOfNewFiles) and set it to 0. Then put your Do statement.

Increase NumOfNewFiles. Your Open #2 statement goes here, except you'd use NumOfNewFiles in the filename. Then put your For statement, so you can read 2000 lines. NOW put your Input statement. (The object being, of course, to read 2000 times.) You should probably put an If statement that says "if we've gotten to the end of the file then exit the for/next loop".

Put your Print #2 statement here, followed by your Next statement. Since you've read all the lines for the new file, put your Close #2 statement here. Follow that with your Loop statement and your Close #1 statement.

OK, here's all that in code:
Code:
Open "C:\test\index.txt" For Input As #1
NumOfNewFiles = 0
Do Until EOF(1)
  NumOfNewFiles = NumOfNewFiles + 1
  Open "C:\test\" & NumOfNewFiles & ".txt" for Output As #2
  For i = 1 To 2000
    If EOF(1) Then
      Exit For
    End If
    Line Input #1, Data
    Print #2, Data
  Next i
  Close #2
Loop
Close #1

I haven't tried this code, so you might have to futz with the "If EOF(1)" statement. Other than that you should be OK.
 
thanks smokingcrater's code worked like a charm, and legoartist's worked to. Appreciate the help
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top