for LOOP reading files...

Blitz3D Forums/Blitz3D Beginners Area/for LOOP reading files...

Apollonius(Posted 2004) [#1]
Im trying to check if
001.ITM to 005.ITM exist but it wont work they say it doesnt exist..->[data]->[items]-> *.ITM

oh and is there a way to get how many .ITM there is in a directory?

For t = 1 To 5

file_num = 0 + 1
file_path = "data/items/00" + file_num + ".ITM"
filein = ReadFile (file_path)

If filein = False
	RuntimeError "Error could not read file"
Else
	Notify "File Reading Successful"
End If

CloseFile(filein)

Next



Warren(Posted 2004) [#2]
you should probably have:

file_num = t


Your current code will check for the existence of "001.ITM", 5 times.


Apollonius(Posted 2004) [#3]
Ok it still doesnt read the files it gives me an error what could be wrong.. cause 001.ITM, 002.ITM ... 003.ITM, 004.ITM, 005.ITM ... all exist... whats wrong?

For t = 1 To 5

file_num = t
file_path = "data/items/00" + file_num + ".ITM"
filein = ReadFile (file_path)

If filein = False
	RuntimeError "Error could not read file"
Else
	Notify "File Reading Successful"
End If

CloseFile(filein)

Next



Warren(Posted 2004) [#4]
You can always used the "FileType" function for this. It returns 0 if the file isn't there.


jhocking(Posted 2004) [#5]
Declare file_path as a string variable using $. Like this:
file_path$="data etc


Apollonius(Posted 2004) [#6]
jhocking, hmm.... What?! Transforme that into newb explication plz ;)


jhocking(Posted 2004) [#7]
In retrospect my post does look sort of like a cryptic Windows error message. I meant replace this line:
file_path = "data/items/00" + file_num + ".ITM"

with this:
file_path$ = "data/items/00" + file_num + ".ITM"

Note that the only change is adding $. I can never remember with complete certainty if you need to do that, but that is popping into my head.


WolRon(Posted 2004) [#8]
file_path is an integer variable.
file_path$ is a string variable.

In your program, file_path will contain the value of zero, not the string data "data/items/00" + file_num + ".ITM"

Make the change jhocking said, and it will work.


Warren(Posted 2004) [#9]
Grr ... I always miss the obvious error. :)


jhocking(Posted 2004) [#10]
Well, he also needed to make the change you noticed.