BBC BASIC for Windows
General >> General Board >> How can I read the filenames from a folder
http://bb4w.conforums.com/index.cgi?board=general&action=display&num=1268996508

How can I read the filenames from a folder
Post by Danny72 on Mar 19th, 2010, 11:01am

Hello Everyone

Could someone let me know how I can read (into some kind of variables) the filenames from a particular folder in Windows.

Many thanks

Danny
Re: How can I read the filenames from a folder
Post by Malcolm on Mar 19th, 2010, 3:00pm


Google "MSDN findfirstfile"

or search the group files for FindFirstFile or FindNextFile. That's the API's that you need.




Re: How can I read the filenames from a folder
Post by Malcolm on Mar 19th, 2010, 3:06pm

Or here it is already done for you!

http://bb4w.wikispaces.com/Scanning+a+Directory+%28Reading+Directory+Entries%29
Re: How can I read the filenames from a folder
Post by Danny72 on Mar 21st, 2010, 7:19pm

Thanks for the information.

Danny
unable to get the code working
Post by Danny72 on Mar 22nd, 2010, 5:32pm

Hi

Running this results in the error Not in a FN or PROC
Could someone let me know what I need to do to run this. I just want to be able to read filenames into a string variable. Thanks.




LOCAL dir%, sh%, res%
DIM dir% LOCAL 317
SYS "FindFirstFile", "*", dir% TO sh%
IF sh% <> -1 THEN
REPEAT
PRINT $$(dir%+44)
SYS "FindNextFile", sh%, dir% TO res%
UNTIL res% = 0
SYS "FindClose", sh%
ENDIF
ENDPROC

Re: unable to get the code working
Post by admin on Mar 23rd, 2010, 09:28am

on Mar 22nd, 2010, 5:32pm, Danny72 wrote:
Running this results in the error Not in a FN or PROC

The code has evidently been lifted from a PROC, because of the LOCAL, DIM...LOCAL and ENDPROC:

Code:
      LOCAL dir%, sh%, res%
      DIM dir% LOCAL 317
      ...
      ENDPROC 

Therefore the easiest thing to do would be to use it in a PROC or FN yourself.

Trying to run the code outside a PROC or FN would risk a memory leak, because of the DIM. You could change the memory block at dir% to a structure; that would make the code more elegant and avoid the memory leak, but it's a lot of work when the alternative of putting it in a PROC or FN is so much easier.

Richard.
Re: How can I read the filenames from a folder
Post by admin on Mar 23rd, 2010, 09:43am

on Mar 19th, 2010, 3:06pm, Guest-Malcolm wrote:
Or here it is already done for you!
http://bb4w.wikispaces.com/Scanning+a+Directory+%28Reading+Directory+Entries%29

It's in the main help documentation too:

http://www.bbcbasic.co.uk/bbcwin/manual/bbcwine.html#dirlist

Richard.
Re: How can I read the filenames from a folder
Post by admin on Mar 23rd, 2010, 3:26pm

I have written a Wiki article listing a function FNfiles which reads the contents of a disk directory into a string array (not dissimilar to Liberty Basic's FILES statement):

http://bb4w.wikispaces.com/Listing+the+contents+of+a+directory

This may be useful when you want to avoid using the Windows API directly.

Richard.
Re: How can I read the filenames from a folder
Post by Danny72 on Mar 23rd, 2010, 4:19pm

Thanks for the code.

Danny