QB64.org Forum

Active Forums => QB64 Discussion => Topic started by: Mad Axeman on January 24, 2019, 11:58:44 am

Title: Listing files in a directory
Post by: Mad Axeman on January 24, 2019, 11:58:44 am
Hi All

Is there a simple way to load files with a certain extension into an array? I've looked through the wiki and can't find anything. Basically I want to load all files with the extension .fmt into an array. I could use SHELL "dir *.fmt > temp.fil /B"  and then read in 'temp.fil' into an array but I really don't like shelling to DOS. Can you still shell to dos in Win10?
Title: Re: Listing files in a directory
Post by: Pete on January 24, 2019, 12:15:33 pm
You mean something as simple as this?

Code: QB64: [Select]
  1. REDIM array$(1000)
  2. ext$ = ".fmt"
  3.  
  4. ' Caution. Makes a text file called myfile~1.tst in your local directory. Don't run this if you have a file, by some weird chance, named that already.
  5.  
  6. OPEN "myfile~1.tst" FOR OUTPUT AS #1
  7. PRINT #1, "foo.txt"
  8. PRINT #1, "foo.bas"
  9. PRINT #1, "foo.fmt"
  10. PRINT #1, "apple.wmv"
  11. PRINT #1, "format.fmt"
  12. PRINT #1, "cow.txt"
  13. PRINT #1, "three.fmt"
  14.  
  15. OPEN "myfile~1.tst" FOR INPUT AS #1
  16.     LINE INPUT #1, a$
  17.     a$ = LCASE$(a$)
  18.     IF INSTR(a$, ext$) <> 0 THEN
  19.         arraynumber = arraynumber + 1
  20.         array$(arraynumber) = a$
  21.     END IF
  22.  
  23. FOR i = 1 TO arraynumber
  24.     PRINT array$(i)

Yes, Win 10 still allows SHELL commands. To get just those files, use this instead of the above code...

SHELL _HIDE "dir /b /A:-D *.fmt>myfile~1.tst"
SHELL _DONTWAIT "notepad myfile~1.tst"

Where /b gives just the file names (bare), /A:-D omits sub-directories (Not actually needed here, but it's a cool switch.), and > pipes those search findings to a file, which we then can use another shell statement to view.

Let us know if this isn't what you are looking for or if you have any other questins.

Pete


Title: Re: Listing files in a directory
Post by: SMcNeill on January 24, 2019, 12:28:07 pm
What you're looking for is probably this simple little routine:  http://qb64.freeforums.net/thread/63/file-directory-listing-arrays

It gets the information for both files and folders and sorts them into 2 quick and simple string arrays for reference.
Title: Re: Listing files in a directory
Post by: Mad Axeman on January 24, 2019, 02:06:53 pm
Reading the directory and loading it into an array via shell I can already do. I was hoping there was a way to do it without using shell, perhaps a command that I had missed in the wiki. I saw _DIR$ and I got my hopes up but nope. How about a new command in the next version, named something like _readdir$, which will read the directory? Just a suggestion.  I really don't like using SHELL.
Title: Re: Listing files in a directory
Post by: bplus on January 24, 2019, 02:28:43 pm
eoredson had made a bunch of DOS utilities here: https://www.qb64.org/forum/index.php?topic=205.msg1056#msg1056

Well those say Windows but he was doing DOS too I think, check his links.

Here is more on Steve's lookup: https://www.qb64.org/forum/index.php?topic=321.0

For me, the most direct method is the Shell to make a temp file.

Oops! can't recommend eoredson's xdir, it is an overloaded Swiss-army knife! Yikes!
Title: Re: Listing files in a directory
Post by: Mad Axeman on January 24, 2019, 03:03:50 pm
What I've used at the moment is :-

Code: QB64: [Select]
  1. tem$ = "temp" + TIME$ + ".tmp"
  2. MID$(tem$, 7, 1) = "-"
  3. MID$(tem$, 10, 1) = "-"
  4.  
  5. SHELL "dir *.fmt > " + tem$ + " /B"
  6.  
  7. d = 1
  8. OPEN tem$ FOR INPUT AS #5
  9.     LINE INPUT #5, x$
  10.     ex% = INSTR(x$, ".")
  11.     x$ = LEFT$(x$, ex% - 1)
  12.     di$(d) = x$
  13.     d = d + 1
  14. KILL tem$
  15.  
  16.  

I've put the time$ in to make sure I get a unique file name for the temp file. 'd-1' gives the number of files with the .fmt extension and the array di$(x) holds the first part of the file name (without the .fmt extension)
Title: Re: Listing files in a directory
Post by: Pete on January 24, 2019, 03:19:57 pm
I'm a big SHELL fan, too. With various other switches you can output by date/time, alphabetize the order, etc. Easydos.com is a great place to look up SHELL commands like dir.

Before I used SHELL to get directory lists, I used the old QB FILES with a screen reading routine. A serious drawback is it only displays the MS-DOS8.3 names. This means 8-charater names plus a 3 character extension. So myawesomeprogram.bas gets truncated to something like: myawes~1.bas. It is also limited to what appears on the screen and if you have a lot of files, the screen scrolls and there goes you screen read when displayed option.

For fun, try...

Code: QB64: [Select]
  1. WIDTH 150, 42
  2. FILES "*.bas"
  3.  

It should give you a list of just the BASIC files in your QB64 directory.

I also remember there were some directory utilities at The QBasic Forum that used one of the QB libraries, maybe CALL ABSOLUTE, CALL INTERRUPT or maybe it was INTERRUPTX to get a directory list without using SHELL. Some of those library functions are still supported in QB64. A search for them is possible at: https://www.tapatalk.com/groups/qbasic/memberlist.php?mode=admin_member

a _DIR keyword that works like SHELL DIR would certainly be a nice addition to QB64, but instead, for some reason, we got that weird _DIR$ statement, which apparently only returns the pathway of a specified folder, like "desktop".

Ah, while I was writing this, I see you replied. Your code will work fine. Thi does the same thing, I just optimized your code a bit...

Code: QB64: [Select]
  1. tem$ = "temp" + TIME$ + ".tmp"
  2. MID$(tem$, 7, 1) = "-"
  3. MID$(tem$, 10, 1) = "-"
  4.  
  5. SHELL _HIDE "dir /b *.fmt>" + tem$ ' _HIDE gets rid of the console flashing open and closed.
  6.  
  7. REDIM di$(1000) ' 1000 or as many records as you need.
  8. d = 0
  9. OPEN tem$ FOR INPUT AS #5
  10.     LINE INPUT #5, x$
  11.     d = d + 1
  12.     di$(d) = LEFT$(x$, INSTR(x$, ".") - 1)
  13.  

Also, if you want a faster read for a huge directory of files, use FOR BINARY instead of FOR INPUT.

Pete
Title: Re: Listing files in a directory
Post by: Mad Axeman on January 24, 2019, 04:00:52 pm
I like the _HIDE. I hadn't found that in the command list. Gets rid of that annoying flash open/closed. Thanks for that :-)
Title: Re: Listing files in a directory
Post by: Petr on January 24, 2019, 04:14:36 pm
Hi. Try this https://www.qb64.org/forum/index.php?topic=321.msg2126#msg2126
Title: Re: Listing files in a directory
Post by: _vince on January 25, 2019, 11:08:04 am
I was hoping there was a way to do it without using shell [...]  I really don't like using SHELL.

Steve presented a solution for exactly that in the second reply to your post.

One of the issues with SHELL, and why it should always be avoided, is that it's not multiplatform. I believe Steve's dirent.h solution should compile in mingw but it's not something that I have personally tested.  FreeBASIC has a built-in multiplatform solution with its DIR function, https://www.freebasic.net/wiki/wikka.php?wakka=KeyPgDir (https://www.freebasic.net/wiki/wikka.php?wakka=KeyPgDir).  This is a good model for a potential QB64 variant if anyone's willing to implement it.
Title: Re: Listing files in a directory
Post by: Pete on January 25, 2019, 11:20:05 am
You just seconded my request.

Pete +1
Title: Re: Listing files in a directory
Post by: Petr on January 25, 2019, 01:07:35 pm
Quote
Steve presented a solution for exactly that in the second reply to your post.

One of the issues with SHELL, and why it should always be avoided, is that it's not multiplatform. I believe Steve's dirent.h solution should compile in mingw but it's not something that I have personally tested.  FreeBASIC has a built-in multiplatform solution with its DIR function, https://www.freebasic.net/wiki/wikka.php?wakka=KeyPgDir.  This is a good model for a potential QB64 variant if anyone's willing to implement it.

Hi Vince. Yes, Steve library works under Linux. Second... Upgrading the FILES command promised Galleon. But the promise was forgotten. So i am glad for Steve helped us. Also Eoredson do very good and useful DIR / Files browsing program, but this is based on Windows libraries, so work not under Linux.
Title: Re: Listing files in a directory
Post by: _vince on January 25, 2019, 01:51:25 pm
Hi Vince. Yes, Steve library works under Linux. Second... Upgrading the FILES command promised Galleon. But the promise was forgotten. So i am glad for Steve helped us. Also Eoredson do very good and useful DIR / Files browsing program, but this is based on Windows libraries, so work not under Linux.

Yes it should work on linux and mac but I was wondering if it works on windows too. Most likely a separate implementation of DIR/FILES/etc will be required for each platform.
Title: Re: Listing files in a directory
Post by: SMcNeill on January 25, 2019, 02:05:43 pm
Hi Vince. Yes, Steve library works under Linux. Second... Upgrading the FILES command promised Galleon. But the promise was forgotten. So i am glad for Steve helped us. Also Eoredson do very good and useful DIR / Files browsing program, but this is based on Windows libraries, so work not under Linux.

Yes it should work on linux and mac but I was wondering if it works on windows too. Most likely a separate implementation of DIR/FILES/etc will be required for each platform.

QB64 comes with posix, so the little routines I worked up work just fine for Linux, Mac, and Windows all.  ;)