Getting filenames from a directory

Blitz3D Forums/Blitz3D Beginners Area/Getting filenames from a directory

Stevie G(Posted 2005) [#1]
Say I have a track editor and save tracks as "blah.mytrack" I want to have a track selection menu which displays all the .mytrack files available. Can it be done? Is there an easy way to do this?

Haven't really done very much file handling so any help would be appreciated.


Ross C(Posted 2005) [#2]
Me waits for Stevie to edit his post :o)


Ross C(Posted 2005) [#3]
Well, i got this code from...can't remember where, but:

; Define what folder to start with ...
folder$="images"

; Open up the directory, and assign the handle to myDir
myDir=ReadDir(folder$)

; Let's loop forever until we run out of files/folders to list!
Repeat
	; Assign the next entry in the folder to file$
	file$=NextFile$(myDir)
	
	; If there isn't another one, let's exit this loop
	If file$="" Then Exit
	
	; Use FileType to determine if it is a folder (value 2) or a file and print  results
	If FileType(folder$+"\"+file$) = 2 Then
		Print "Folder:" + file$
	Else
		If Lower(Right$(file$,3))="bmp" Then
			Print "File:" + file$
			file_count=file_count+1
		Else
			Print "File not supported:" + file$
		End If
	End If
Forever


file$=NextFile$(myDir)


Loads the next filename in the directory, into the string var file$. Just check the last three characters of the string, and match them against your file type. If they match, it's one of your files.


Stevie G(Posted 2005) [#4]
Perfect .. thanks V much Ross!!


BlackJumper(Posted 2005) [#5]
Just check the last three characters of the string, and match them against your file type. If they match, it's one of your files.


... is a reasonable assumption, but not rock solid. It is possible for three letter extension clashes to exist.

Your .mytrack files should start with a header section that you also check for when you attempt to load it. That way if someone has simly renamed their picture.bmp file to picture.mytrack you wont be trying to load invalid data. The header might include things like a version number, so that later developments in your code will recognise earlier (less feature rich !) versions and know what to do with them.


Ross C(Posted 2005) [#6]
Sorry, i should have clarified that :o)


big10p(Posted 2005) [#7]
That way if someone has simly renamed their picture.bmp file to picture.mytrack you wont be trying to load invalid data.


Sorry, but if anyone goes to that much trouble to throw a spanner in the works, then f*** 'em, I say. :)


BlackJumper(Posted 2005) [#8]
I agree with the comment about spanner-throwers, but my suggestion regarding version numbers still holds.

In particular you could do a check within your Version1 program for data files with a higher version number and pop up a warning that the datafile has been created with a newer version and may not work. Use this to give the user a chance to upgrade to the new version of the program. {This assumes that the purpose of this is to supply a level editor to your 'modding community' who will build tracks for you.}

I don't want to hijack this thread (but hopefully Ross C's answer has solved your problem) but I wonder what people think of this as part of a beta-release system - using version numbers in header files and have the loader functions refuse to run data files created with a newer version; then supply gold-code released datafiles with a higher version number ??


Stevie G(Posted 2005) [#9]
I agree also , who'd be daft enough to do that ?!... As for the Version control point - a great idea ... as the track file format evolves.

Thanks all


Snarty(Posted 2005) [#10]
A further option could be to allow "Import" options. This way you can build an index file and do all the file checking there and then.


Stevie G(Posted 2005) [#11]
Build an index file? I'm hoping that the current format will not evolve too much ..


Snarty(Posted 2005) [#12]
It wouldn't be anything to do with the actual format, just a file located in the directory which lists all imported custom tracks.


Stevie G(Posted 2005) [#13]
Yeah .. I planned on having [original] , [custom] folders to hold these. I see what you're getting at though .. when I save a new file .. the index file is also updates and then for the track select menu I just read the filenames listed in the Index file ...

The problem with this though is... say you wanted to download someone elses tracks and just copied them into the directory ... the index file would not recognise the file until it was updated with the details.


Snarty(Posted 2005) [#14]
Hence the import/export options :) This way you control what files it includes etc. And, you only have to check if the file does not exist to remove it from the index.