QB64.org Forum

Active Forums => Programs => Topic started by: SMcNeill on July 27, 2017, 09:00:57 am

Title: File Listing
Post by: SMcNeill on July 27, 2017, 09:00:57 am
Lots of folks like to ask for a way to easily grab the contents of a directory and import a list of the contents of that directory into their programs, so for them, I introduce my little File Grab Library.

Code: QB64: [Select]
  1. '$INCLUDE:'Filegrab.BI'
  2.  
  3. REDIM Results(0) AS STRING
  4. SCREEN _NEWIMAGE(1280, 720, 32)
  5.  
  6. SelectFile _CWD$, "*.*", Results()
  7. FOR i = 1 TO UBOUND(results)
  8.     PRINT Results(i)
  9.  
  10.  
  11. '$INCLUDE:'Filegrab.BM'

As you can see, usage is fairly straight forward.   
Extract to your QB64 folder. 
Include the header. 
Include the footer. 
REDIM an array to hold the results.
Call the routine with SelectFile _CWD$, "*.*", Results(),

SelectFile is the name of the routine. 
First Parameter _CWD$ can be replaced with whatever directory you want the contents of.
Second Parameter "*.*" is the filter of whatever you want for that directory.  Use "*.txt" for a listing of all text files, for example.
Third Parameter Results() is the REDIM array which you use to store the results of the file listing.

Works on Windows, Linux, and Mac.
Title: Re: File Listing
Post by: bplus on July 27, 2017, 09:22:25 am
Thanks that is one I have been currently wanting!
Title: Re: File Listing
Post by: bplus on July 27, 2017, 11:17:53 am
May I ask a maybe dumb question? ...well I can ask ;-))

What is an .h and .BM file?

I do remember .BI files.
Title: Re: File Listing
Post by: Petr on July 27, 2017, 11:31:32 am
Hi Steve. Its works good, but could you still add output in 8.3 format? I certainly do not have to explain why. Thank you for answer.

Title: Re: File Listing
Post by: FellippeHeitor on July 27, 2017, 11:33:19 am
A .h file is an external C++ header file you attach to your QB64 programs via /wiki/index.php/DECLARE_LIBRARY.html]DECLARE LIBRARY (https://qb64.org/wiki/www.[abandoned, outdated and now likely malicious qb64 dot net website - don’t go there). It'll contain procedures and functions that'll extend QB64's capabilities.

A .BM or a .BI (or whatever other extension you desire, really) is a plain text QB64 source code file. Convention has it [citation needed] that if your library requires SHARED variables, CONSTs, TYPES, external helper libraries - that is, code that needs to be in the beginning of a program, you'll add these to a .BI module to be included at the top. Then your SUBs and FUNCTIONs will be in the .BM (M for methods, I guess?) that the end-user will $INCLUDE at the bottom.

This is especially so because you can't have stray code after your block of SUB/FUNCTIONs begins.

Code: QB64: [Select]
  1. 'include .BI
  2. 'your code
  3. 'include .BM
  4. 'your subs
Title: Re: File Listing
Post by: SMcNeill on July 27, 2017, 11:39:07 am
May I ask a maybe dumb question? ...well I can ask ;-))

What is an .h and .BM file?

I do remember .BI files.

QB64 requires SUB/FUNCTION statements to come at the end of our code (whereas QB45 would allow them anywhere), so include files need to be broken into two parts -- the part which goes at the top of the code (DIM statements, TYPEs, etc), and the part that goes at the end of the code (the SUB and FUNCTION/).

BI files are just an extension to say, "Include this one first" and BM files are just a convention to say, "Include this last, at the end of the file."

*******************

As for .h files,they're C-code which we can use to expand C-routines for use in QB64, which usually has to be linked to with DECLARE LIBRARY.

(Felippe types faster than me and beat me to the answer.)  ;)
Title: Re: File Listing
Post by: bplus on July 27, 2017, 11:49:03 am
Thanks Steve and Fellippe, this is very helpful for me getting up to speed. So that is why the other (BM) is below! nice mnemonic ;)

OK I am trying to test the demo and have error about finding the .BI but, as I show, it is in the expected directory/folder, I think.

Any ideas the problem?

see screen shot
Title: Re: File Listing
Post by: FellippeHeitor on July 27, 2017, 11:54:52 am
Make the path relative to the .BI file:

In the DECLARE LIBRARY block inside Filegrab.BI add .\ before the name of the library.
Title: Re: File Listing
Post by: bplus on July 27, 2017, 12:09:27 pm
Hi Fellippe and all,

Nope, ./ actually messes up the pathed filename that QB64 says it can't find in the error message.

Does QB64 have any problems with spaces in pathed filename or length of the pathed filename?


Append:

"In the declare library bi" oh, in the BI file...
Title: Re: File Listing
Post by: Petr on July 27, 2017, 12:10:02 pm
Quote
In the DECLARE LIBRARY block inside Filegrab.BI add .\ before the name of the library.

or copy .BM and .BI files direct to directory with your QB64 installation.
Title: Re: File Listing
Post by: SMcNeill on July 27, 2017, 12:10:37 pm
Direntry.h needs to be in the root qb64 folder with qb64.exe to work properly.  The error is probably there.

(I'd suggest extracting to the main QB64 folder once for testing to make certain it works as advertised on your system.  Everything after that is just adjusting paths to point to the correct places.)
Title: Re: File Listing
Post by: bplus on July 27, 2017, 12:21:08 pm
Thanks all,

Yep! I copied everything to QB64.exe folder and the demo .bas runs fine from there AND it now runs fine from the grabfile Folder as well.

Maybe just the .h file? I will try combos and learn a lesson! :) of where what is needed.
Title: Re: File Listing
Post by: FellippeHeitor on July 27, 2017, 12:21:32 pm
Just checked: if you make the path relative to the .BI file, direntry.h can be in the same folder as the .BI, even if this is not qb64's main folder:

Code: QB64: [Select]
  1. 'Filegrab.BI:
  2.     FUNCTION FILE_load_dir& ALIAS load_dir (s AS STRING)
  3.     FUNCTION FILE_has_next_entry& ALIAS has_next_entry ()
  4.     SUB FILE_close_dir ALIAS close_dir ()
  5.     SUB FILE_get_next_entry ALIAS get_next_entry (s AS STRING, flags AS LONG, file_size AS LONG)
  6.     SUB FILE_get_current_dir ALIAS get_current_dir (s AS STRING)
  7.     FUNCTION FILE_current_dir_length& ALIAS current_dir_length ()
  8.  

@Steve: looks like a simple enough patch to your lib to prevent future issues for others who decide to try it.
Title: Re: File Listing
Post by: bplus on July 27, 2017, 12:31:05 pm
I can confirm Fellippe's fix.

I deleted all the grab files in QB64.exe folder, made the change in the .BI  ./ and all worked! :)

Append:

hmm... ./ or .\   ?? in Windows I don't think it matters, but I see ./ in Windows paths.
Title: Re: File Listing
Post by: FellippeHeitor on July 27, 2017, 12:42:51 pm
Doesn't matter.