Author Topic: DIR$ function is missing  (Read 5483 times)

0 Members and 1 Guest are viewing this topic.

Offline sparjolly

  • Newbie
  • Posts: 5
    • View Profile
DIR$ function is missing
« on: October 26, 2020, 10:06:32 am »
My old BASIC code that was working with 32-bit QBASIC compiler now shows error in QB64 on the following line with DIR$ function. Is there any way to walk around this issue?

F$=DIR$("*.LL1")

Offline SpriggsySpriggs

  • Forum Resident
  • Posts: 1145
  • Larger than life
    • View Profile
    • GitHub
Re: DIR$ function is missing
« Reply #1 on: October 26, 2020, 10:18:42 am »
Is the code supposed to show an output of files that match that wildcard? If so, I have some code that could help you.
Shuwatch!

Marked as best answer by sparjolly on October 26, 2020, 08:45:28 am

Offline SpriggsySpriggs

  • Forum Resident
  • Posts: 1145
  • Larger than life
    • View Profile
    • GitHub
Re: DIR$ function is missing
« Reply #2 on: October 26, 2020, 10:26:15 am »
Here is some code that can help you. You need to download the attached "pipecom.h" file and put the DECLARE LIBRARY block in your program. The header contains code that pipes console output into the variable for printing, manipulating, etc.

Code: QB64: [Select]
  1. DECLARE LIBRARY "pipecom"
  2.     FUNCTION pipecom$ (cmd AS STRING)
  3.  
  4.  
  5. F = pipecom("dir *.BAS")
  6. 'F = pipecom("dir /b *.BAS") 'if you need just the filenames and not the rest of the output
  7.  

And a sample printout:

  [ You are not allowed to view this attachment ]    [ You are not allowed to view this attachment ]  
« Last Edit: October 26, 2020, 10:34:33 am by SpriggsySpriggs »
Shuwatch!

FellippeHeitor

  • Guest
Re: DIR$ function is missing
« Reply #3 on: October 26, 2020, 10:28:40 am »
QB64 is aimed at close to 100% compatibility with QBasic/QuickBASIC 4.5. DIR$() used to be available in PDS7. Here's an adaptation, from the wiki (although I'd go with Spriggsy's approach above):

Quote
The DIR$ function adapted from PDS (7.1) returns a filename or a list when more than one exist. The file spec can use a path and/or wildcards.
Code: QB64: [Select]
  1. FOR i = 1 TO 2
  2.   LINE INPUT "Enter a file spec: ", spec$
  3.   file$ = DIR$(spec$) 'use a file spec ONCE to find the last file name listed
  4.   PRINT DIRCount%, file$, 'function can return the file count using SHARED variable
  5.   IF DIRCount% > 1 THEN
  6.     DO
  7.       K$ = INPUT$(1)
  8.       file$ = DIR$("") 'use an empty string parameter to return a list of files!
  9.       PRINT file$,
  10.     LOOP UNTIL LEN(file$) = 0 'file list ends with an empty string
  11.   END IF
  12.  
  13.  
  14. FUNCTION DIR$ (spec$)
  15. CONST TmpFile$ = "DIR$INF0.INF", ListMAX% = 500 'change maximum to suit your needs
  16. SHARED DIRCount% 'returns file count if desired
  17. STATIC Ready%, Index%, DirList$()
  18. IF NOT Ready% THEN REDIM DirList$(ListMAX%): Ready% = -1 'DIM array first use
  19. IF spec$ > "" THEN 'get file names when a spec is given
  20.   SHELL _HIDE "DIR " + spec$ + " /b > " + TmpFile$
  21.   Index% = 0: DirList$(Index%) = "": ff% = FREEFILE
  22.   OPEN TmpFile$ FOR APPEND AS #ff%
  23.   size& = LOF(ff%)
  24.   CLOSE #ff%
  25.   IF size& = 0 THEN KILL TmpFile$: EXIT FUNCTION
  26.   OPEN TmpFile$ FOR INPUT AS #ff%
  27.   DO WHILE NOT EOF(ff%) AND Index% < ListMAX%
  28.     Index% = Index% + 1
  29.     LINE INPUT #ff%, DirList$(Index%)
  30.   LOOP
  31.   DIRCount% = Index% 'SHARED variable can return the file count
  32.   CLOSE #ff%
  33.   KILL TmpFile$
  34. ELSE IF Index% > 0 THEN Index% = Index% - 1 'no spec sends next file name
  35. DIR$ = DirList$(Index%)

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: DIR$ function is missing
« Reply #4 on: October 26, 2020, 10:53:26 am »
There is a non-Windows solution too but a little more work.

Offline SpriggsySpriggs

  • Forum Resident
  • Posts: 1145
  • Larger than life
    • View Profile
    • GitHub
Re: DIR$ function is missing
« Reply #5 on: October 26, 2020, 10:54:30 am »
There is a non-Windows solution too but a little more work.
@bplus Guess what? Mine is cross-platform ;) Works on Linux, Mac, and Windows. All the same code.
Shuwatch!

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: DIR$ function is missing
« Reply #6 on: October 26, 2020, 10:56:09 am »
@bplus Guess what? Mine is cross-platform ;) Works on Linux, Mac, and Windows. All the same code.

Oh cool!

@SpriggsySpriggs  had you posted this? I must have missed.
« Last Edit: October 26, 2020, 11:01:39 am by bplus »

Offline luke

  • Administrator
  • Seasoned Forum Regular
  • Posts: 324
    • View Profile
Re: DIR$ function is missing
« Reply #7 on: October 26, 2020, 11:14:10 am »
Obligatory friendly reminder that Linux is case sensitive for file names, so "dir *.BAS" won't match "foo.bas".

Also on Linux and MacOS the default output is plain filenames, "ls -l" is needed for a full listing with metadata.

Offline SpriggsySpriggs

  • Forum Resident
  • Posts: 1145
  • Larger than life
    • View Profile
    • GitHub
Re: DIR$ function is missing
« Reply #8 on: October 26, 2020, 11:15:49 am »
Obligatory friendly reminder that Linux is case sensitive for file names, so "dir *.BAS" won't match "foo.bas".

Also on Linux and MacOS the default output is plain filenames, "ls -l" is needed for a full listing with metadata.
@luke Yes, of course. I didn't mean that "dir /b *.BAS" was cross-platform. Only that the pipecom library was :)
Shuwatch!

Offline sparjolly

  • Newbie
  • Posts: 5
    • View Profile
Re: DIR$ function is missing
« Reply #9 on: October 26, 2020, 12:17:57 pm »
Thanks for your suggestion. The issue is resolved.

Offline SpriggsySpriggs

  • Forum Resident
  • Posts: 1145
  • Larger than life
    • View Profile
    • GitHub
Re: DIR$ function is missing
« Reply #10 on: October 26, 2020, 12:19:49 pm »
Thanks for your suggestion. The issue is resolved.
@sparjolly Glad to hear!
Shuwatch!

Offline SpriggsySpriggs

  • Forum Resident
  • Posts: 1145
  • Larger than life
    • View Profile
    • GitHub
Re: DIR$ function is missing
« Reply #11 on: October 26, 2020, 12:22:04 pm »
Oh cool!

@SpriggsySpriggs  had you posted this? I must have missed.
@bplus I hadn't posted it. I have been keeping it on the Discord server only while I've been testing it across platforms. Although, I reckon I could make a post for it.
Shuwatch!

Offline sparjolly

  • Newbie
  • Posts: 5
    • View Profile
Re: DIR$ function is missing
« Reply #12 on: October 26, 2020, 07:57:01 pm »
I am back again. While the "pipecom.h" library provides a solution, it captures all the file names in the folder meeting the specified criterion. Having a folder with hundreds of files will require creating a huge string array to store all the file names in the program.

The original code using DIR$ function of BASIC fetches only one name at a time, which is a more elegant solution.

I wonder if the "while (!feof(stream))" statement be pulled out of the library so that it provides only one file name at a time and returns a nul when all the files are covered.

Thanks


F$=DIR$("*.LL1")

DO WHILE F$ <> ""

    CNT$ = LEFT$(F$, 4)

    OPEN CNT$ + ".LL1" FOR INPUT AS #1
    OPEN CNT$ + ".LL" FOR OUTPUT AS #2

    F$ = DIR$

LOOP

Offline SpriggsySpriggs

  • Forum Resident
  • Posts: 1145
  • Larger than life
    • View Profile
    • GitHub
Re: DIR$ function is missing
« Reply #13 on: October 26, 2020, 08:28:30 pm »
@sparjolly Here you go. This uses a method of splitting a string up into an array based on a delimiter. You can see that I have split the string into a new string array called filelist. You can then take it and do your operations on the files within. I would have to write up a new function for the library that uses the original stream and I don't think it would work the way you are wanting it to. For now, try the below code.

Code: QB64: [Select]
  1. DECLARE LIBRARY "pipecom"
  2.     FUNCTION pipecom$ (cmd AS STRING)
  3.  
  4.  
  5. F = pipecom("dir /b *.BAS")
  6.  
  7. REDIM filelist(0) AS STRING
  8.  
  9. String.Split F, CHR$(10), filelist(), 0
  10.  
  11. FOR x = 0 TO UBOUND(filelist) - 1
  12.     PRINT filelist(x)
  13.  
  14.  
  15. SUB String.Split (Expression AS STRING, delimiter AS STRING, StorageArray() AS STRING, preserve AS INTEGER)
  16.     DIM copy AS STRING, p AS LONG, curpos AS LONG, arrpos AS LONG, dpos AS LONG
  17.     copy = Expression
  18.     IF delimiter = " " THEN
  19.         copy = RTRIM$(LTRIM$(copy))
  20.         p = INSTR(copy, "  ")
  21.         WHILE p > 0
  22.             copy = MID$(copy, 1, p - 1) + MID$(copy, p + 1)
  23.             p = INSTR(copy, "  ")
  24.         WEND
  25.     END IF
  26.     curpos = 1
  27.     IF preserve <> 0 THEN
  28.         arrpos = UBOUND(StorageArray)
  29.         dpos = INSTR(curpos, copy, delimiter)
  30.         DO UNTIL dpos = 0
  31.             StorageArray(UBOUND(StorageArray)) = MID$(copy, curpos, dpos - curpos)
  32.             REDIM _PRESERVE StorageArray(UBOUND(StorageArray) + 1) AS STRING
  33.             curpos = dpos + LEN(delimiter)
  34.             dpos = INSTR(curpos, copy, delimiter)
  35.         LOOP
  36.         StorageArray(UBOUND(StorageArray)) = MID$(copy, curpos)
  37.         REDIM _PRESERVE StorageArray(UBOUND(StorageArray) + 1) AS STRING
  38.     ELSEIF preserve = 0 THEN
  39.         arrpos = 0
  40.         dpos = INSTR(curpos, copy, delimiter)
  41.         DO UNTIL dpos = 0
  42.             StorageArray(arrpos) = MID$(copy, curpos, dpos - curpos)
  43.             arrpos = arrpos + 1
  44.             IF arrpos > UBOUND(StorageArray) THEN REDIM _PRESERVE StorageArray(UBOUND(StorageArray) + 1) AS STRING
  45.             curpos = dpos + LEN(delimiter)
  46.             dpos = INSTR(curpos, copy, delimiter)
  47.         LOOP
  48.         StorageArray(arrpos) = MID$(copy, curpos)
  49.         REDIM _PRESERVE StorageArray(arrpos) AS STRING
  50.     END IF
« Last Edit: October 26, 2020, 08:34:52 pm by SpriggsySpriggs »
Shuwatch!

Offline doppler

  • Forum Regular
  • Posts: 241
    • View Profile
Re: DIR$ function is missing
« Reply #14 on: October 26, 2020, 08:57:44 pm »
@SpriggsySpriggs Looking at best answer it's no different than

DIM F AS STRING
CHDIR "c:\qb64\my programs"

shell "dir /b *.bas | clip"
f=_clipboard$

PRINT F

But which way would be faster (by code execution time) ?  Which one could hold more data ? (me thinks windows clipboard)