Author Topic: DIR$ function from Microsoft Professional Basic  (Read 5965 times)

0 Members and 1 Guest are viewing this topic.

Offline alexpro1ny

  • Newbie
  • Posts: 5
    • View Profile
DIR$ function from Microsoft Professional Basic
« on: February 23, 2018, 03:23:51 pm »
Hi,

I used to write my code using Professional Basic (QBX) 7.1

It was a nice program since you could test and debug your code within the compiler without having to make a .exe file first.

I have been using QB64 to convert some of my programs so they would work in Windows 10 and love that it is available.  Without QB64, I'd have to run everything in my Virtual XP window.

I installed your latest version 1.2 and found that it doesn't like the DIR$ command that is a valid function within QBX.   It looks like _DIR$ is in QB64 but it doesn't work the same as DIR$.

For example, if I want to see a list of text files, I can do the following in QBX

DIM dskfile$(200)

filespec$ = "*.txt"
a$ = _DIR$(filespec$)

IF LEN(a$) = 0 THEN GOTO nofilesexist

dskfile = 1
dskfile$(dskfile) = a$

DO
    b$ = DIR$
    IF LEN(b$) = 0 THEN GOTO filelistdone
    dskfile = dskfile + 1
    dskfile$(dskfile) = b$
LOOP

GOTO filelistdone

nofilesexist:

STOP

filelistdone:
FOR n = 1 TO dskfile
    PRINT dskfile$(n)
NEXT n

PRINT dskfile; "files exist"

Using this method I can store into my array all the files that are named .txt

Is there any simple way for me to do the same in QB64?

Thanks,
Alex

FellippeHeitor

  • Guest
Re: DIR$ function from Microsoft Professional Basic
« Reply #1 on: February 23, 2018, 03:46:31 pm »
There's a DIR$() function written by Ted at http://qb64.org/wiki/FILES - a fine workaround.

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: DIR$ function from Microsoft Professional Basic
« Reply #2 on: February 23, 2018, 03:47:12 pm »
DIM dskfile$(200)
filespec$ = "*.txt"
' Caution. If you already have a file named tmp.txt, this next statement will overwrite a file named tmp.txt in your current directory.
SHELL _HIDE "dir /b " + filespec$ + ">tmp.txt"
' The /b switch returns only the file names.
OPEN "tmp.txt" FOR INPUT AS #1
DO UNTIL EOF(1)
    LINE INPUT #1, a$
    dskfile = dskfile + 1
    dskfile$(dskfile) = a$
LOOP

FOR i = 1 TO dskfile
    PRINT i; dskfile$(i)
NEXT
-----------------------------------

Also, check out _FILEESIST in the WIKI: https://qb64.org/wiki

Pete :)
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/

Offline Petr

  • Forum Resident
  • Posts: 1720
  • The best code is the DNA of the hops.
    • View Profile
Re: DIR$ function from Microsoft Professional Basic
« Reply #3 on: February 23, 2018, 06:33:08 pm »
Next solution is adding as .BI and .BM files this program https://www.qb64.org/forum/index.php?topic=87.0  - then array named ShortNameFile return file names in format 8.3 and array FileNames return all files in long format. Use SUB named Filtruj for using mask. If you want, I will write to you tomorrow how to use it without selecting __Files$ for .txt files in actual directory. This program is solution for Unicode uncompability, because if you use SHELL and DIR for files named in unicode, then is returned unusable result for you.

Offline alexpro1ny

  • Newbie
  • Posts: 5
    • View Profile
Re: DIR$ function from Microsoft Professional Basic
« Reply #4 on: February 23, 2018, 07:27:39 pm »
There's a DIR$() function written by Ted at http://qb64.org/wiki/FILES - a fine workaround.

Thanks.. that looks like the ticket I've been looking for.

Offline alexpro1ny

  • Newbie
  • Posts: 5
    • View Profile
Re: DIR$ function from Microsoft Professional Basic
« Reply #5 on: February 23, 2018, 07:29:40 pm »
DIM dskfile$(200)
filespec$ = "*.txt"
' Caution. If you already have a file named tmp.txt, this next statement will overwrite a file named tmp.txt in your current directory.
SHELL _HIDE "dir /b " + filespec$ + ">tmp.txt"
' The /b switch returns only the file names.
OPEN "tmp.txt" FOR INPUT AS #1
DO UNTIL EOF(1)
    LINE INPUT #1, a$
    dskfile = dskfile + 1
    dskfile$(dskfile) = a$
LOOP

FOR i = 1 TO dskfile
    PRINT i; dskfile$(i)
NEXT
-----------------------------------

Also, check out _FILEESIST in the WIKI: https://qb64.org/wiki

Pete :)

Thanks Pete... I used to do it like this before the DIR$ function was added.  It worked but I try to avoid using SHELL as much as possible.  Thanks though.

Offline zaadstra

  • Newbie
  • Posts: 78
    • View Profile
Re: DIR$ function from Microsoft Professional Basic
« Reply #6 on: February 25, 2018, 05:06:01 pm »
This is one of my old wishes ... doing it in memory with a built-in function instead of using temp files on disk. So much memory to use these days!

Offline Cromaglious

  • Newbie
  • Posts: 1
    • View Profile
Re: DIR$ function from Microsoft Professional Basic
« Reply #7 on: November 10, 2018, 12:09:01 am »
FOUND IT!!!!   Kernel32 DLL access using findFirstFile

I used basically the same code in QB45 until I bought QB7.1 Pro. 12 years ago I bought Power Basic 4 and it has the DIR$ ala QB7.1.  I want to use system calls if possible and not temp files.  It can be adapted to OSx (BSD) and Linux as well.  Just add conditionals for compiling.

Cromaglious
Robi

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: DIR$ function from Microsoft Professional Basic
« Reply #8 on: November 10, 2018, 02:03:30 am »
http://qb64.freeforums.net/thread/63/file-directory-listing-arrays -- An easy method to get both file and directory listings into arrays.
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

FellippeHeitor

  • Guest
Re: DIR$ function from Microsoft Professional Basic
« Reply #9 on: November 10, 2018, 02:20:09 am »
FOUND IT!!!!   Kernel32 DLL access using findFirstFile

I used basically the same code in QB45 until I bought QB7.1 Pro. 12 years ago I bought Power Basic 4 and it has the DIR$ ala QB7.1.  I want to use system calls if possible and not temp files.  It can be adapted to OSx (BSD) and Linux as well.  Just add conditionals for compiling.

Cromaglious
Robi

Thanks for sharing, Cromaglious. I didn't know that existed in the API.

Welcome to the forum!

Fellippe.

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: DIR$ function from Microsoft Professional Basic
« Reply #10 on: November 10, 2018, 07:36:03 am »
There is one thing to be careful of with FindFirstFile: https://docs.microsoft.com/en-us/windows/desktop/api/fileapi/nf-fileapi-findfirstfilea

Quote
If you are writing a 32-bit application to list all the files in a directory and the application may be run on a 64-bit computer, you should call the Wow64DisableWow64FsRedirection function before calling FindFirstFile and call Wow64RevertWow64FsRedirection after the last call to FindNextFile.

QB64 is a 32-bit application.  If you're going to share your code with others, they might get run it on 64-bit computers, so be certain to make the additional function calls so you won't generate errors.
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
Re: DIR$ function from Microsoft Professional Basic
« Reply #11 on: November 10, 2018, 01:56:41 pm »
Hi Guys
just to grab an information...
in DOS/WindowSes  you must use FindFirstFile API function, and in Mac Oses and in Linux (different distroes) is there  a similar API ?
It goes around some my wishes for the future QB64 enpowerment https://www.qb64.org/forum/index.php?topic=714.msg6001#msg6001
Quote
My wishlist:
* full compatibility of QB64 with local language of user
* completed set of functions/keywords to manage files
* a debug session with interpeter (real or emulated like in TurboBasic)--> can be used vWatch to get this?
* a well documented set of functions/keywords to manage hardware at low level
* a more integrated GUIFormer and EventDriven editor
* a set of webSubscript functions
* a database system....
and more and more....

Great the QB64 community!

Thanks to read

PS
 I know nothing about MacOs (except that is the first windowed OS if you don't remember GEM on old 5.25 FDD, and it manages in different mode the RAM allocation for calculation of offset) and Linux (except that it is the realm of serious C programmers and some C++ programmers) also if I imagine that also these OSes  have their API.
« Last Edit: November 10, 2018, 06:02:41 pm by TempodiBasic »
Programming isn't difficult, only it's  consuming time and coffee

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: DIR$ function from Microsoft Professional Basic
« Reply #12 on: November 11, 2018, 08:37:08 am »
Hi Guys
just to grab an information...
in DOS/WindowSes  you must use FindFirstFile API function, and in Mac Oses and in Linux (different distroes) is there  a similar API ?
It goes around some my wishes for the future QB64 enpowerment https://www.qb64.org/forum/index.php?topic=714.msg6001#msg6001
Quote
My wishlist:
* full compatibility of QB64 with local language of user
* completed set of functions/keywords to manage files
* a debug session with interpeter (real or emulated like in TurboBasic)--> can be used vWatch to get this?
* a well documented set of functions/keywords to manage hardware at low level
* a more integrated GUIFormer and EventDriven editor
* a set of webSubscript functions
* a database system....
and more and more....

Great the QB64 community!

Thanks to read

PS
 I know nothing about MacOs (except that is the first windowed OS if you don't remember GEM on old 5.25 FDD, and it manages in different mode the RAM allocation for calculation of offset) and Linux (except that it is the realm of serious C programmers and some C++ programmers) also if I imagine that also these OSes  have their API.

Look above, reply #8.  It uses POSIX for reading files and directories, which is used for any system that QB64 is used on.  Linux, Mac, Windows....  You're good to good!
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!