Author Topic: File Listing  (Read 2000 times)

0 Members and 1 Guest are viewing this topic.

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3572
    • Steve’s QB64 Archive Forum
File Listing
« 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.
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline bplus

  • Forum Resident
  • Posts: 6843
  • b = b + ...
Re: File Listing
« Reply #1 on: July 27, 2017, 09:22:25 AM »
Thanks that is one I have been currently wanting!

Offline bplus

  • Forum Resident
  • Posts: 6843
  • b = b + ...
Re: File Listing
« Reply #2 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.

Offline Petr

  • Forum Resident
  • Posts: 1580
  • The best code is the DNA of the hops.
Re: File Listing
« Reply #3 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.


Offline FellippeHeitor

  • QB64 Developer
  • Forum Resident
  • Posts: 2935
  • Let it go, this too shall pass.
    • QB64.org
Re: File Listing
« Reply #4 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. 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
« Last Edit: July 27, 2017, 11:36:56 AM by FellippeHeitor »

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3572
    • Steve’s QB64 Archive Forum
Re: File Listing
« Reply #5 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.)  ;)
« Last Edit: July 27, 2017, 11:40:55 AM by SMcNeill »
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline bplus

  • Forum Resident
  • Posts: 6843
  • b = b + ...
Re: File Listing
« Reply #6 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

Offline FellippeHeitor

  • QB64 Developer
  • Forum Resident
  • Posts: 2935
  • Let it go, this too shall pass.
    • QB64.org
Re: File Listing
« Reply #7 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.

Offline bplus

  • Forum Resident
  • Posts: 6843
  • b = b + ...
Re: File Listing
« Reply #8 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...
« Last Edit: July 27, 2017, 12:12:00 PM by bplus »

Offline Petr

  • Forum Resident
  • Posts: 1580
  • The best code is the DNA of the hops.
Re: File Listing
« Reply #9 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.

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3572
    • Steve’s QB64 Archive Forum
Re: File Listing
« Reply #10 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.)
« Last Edit: July 27, 2017, 12:12:40 PM by SMcNeill »
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline bplus

  • Forum Resident
  • Posts: 6843
  • b = b + ...
Re: File Listing
« Reply #11 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.

Offline FellippeHeitor

  • QB64 Developer
  • Forum Resident
  • Posts: 2935
  • Let it go, this too shall pass.
    • QB64.org
Re: File Listing
« Reply #12 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.

Offline bplus

  • Forum Resident
  • Posts: 6843
  • b = b + ...
Re: File Listing
« Reply #13 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.
« Last Edit: July 27, 2017, 12:38:06 PM by bplus »

Offline FellippeHeitor

  • QB64 Developer
  • Forum Resident
  • Posts: 2935
  • Let it go, this too shall pass.
    • QB64.org
Re: File Listing
« Reply #14 on: July 27, 2017, 12:42:51 PM »
Doesn't matter.