File Listing To Arrays
#1
direntry.h  -- Grab this file.  It's a C header which QB64 will need to make use of.  Without it, the following won't run.

Copied, more or less  verbatim, from my post over at QB64.net:


Random1 asked elsewhere:
Quote:Is  there a way to save the output of FILES to a string or text file?

FILES "C:\MyFile\*.TXT"  to a string or  .txt file
So, I worked up this quick little routine to do just that...

Basic usage is rather simple:  Copy the header and put it up top in your program.  Copy the routine, put it at the end of your program.  Make certain "direntry.h" is in your QB64 directory when compiling...

Call the routine with:

GetFileList _CWD$, Dir(), File()

In this case, I'm just getting the file and subdirectory listings for the Current Working Directory (_CWD$), but you can get them for any directory that exists on your system...




Here's a way you can do what you want to, which is compatible for both Linux and Windows users (whereas SHELL may not always work across various platforms).

Code: (Select All)
DECLARE CUSTOMTYPE LIBRARY ".\direntry"
    FUNCTION load_dir& (s AS STRING)
    FUNCTION has_next_entry& ()
    SUB close_dir ()
    SUB get_next_entry (s AS STRING, flags AS LONG, file_size AS LONG)
END DECLARE

REDIM Dir(0) AS STRING, File(0) AS STRING


GetFileList _CWD$, Dir(), File()

PRINT "SUBDIRECTORIES"
FOR i = 1 TO UBOUND(dir)
    PRINT Dir(i),
NEXT
PRINT
SLEEP

PRINT "FILES": PRINT: PRINT
FOR i = 1 TO UBOUND(file)
    PRINT File(i),
NEXT
PRINT

SUB GetFileList (SearchDirectory AS STRING, DirList() AS STRING, FileList() AS STRING)
    CONST IS_DIR = 1
    CONST IS_FILE = 2
    DIM flags AS LONG, file_size AS LONG

    REDIM _PRESERVE DirList(100), FileList(100)
    DirCount = 0: FileCount = 0

    IF load_dir(SearchDirectory) THEN
        DO
            length = has_next_entry
            IF length > -1 THEN
                nam$ = SPACE$(length)
                get_next_entry nam$, flags, file_size
                IF flags AND IS_DIR THEN
                    DirCount = DirCount + 1
                    IF DirCount > UBOUND(DirList) THEN REDIM _PRESERVE DirList(UBOUND(DirList) + 100)
                    DirList(DirCount) = nam$
                ELSEIF flags AND IS_FILE THEN
                    FileCount = FileCount + 1
                    IF FileCount > UBOUND(filelist) THEN REDIM _PRESERVE FileList(UBOUND(filelist) + 100)
                    FileList(FileCount) = nam$
                END IF
            END IF
        LOOP UNTIL length = -1
        close_dir
    ELSE
    END IF
    REDIM _PRESERVE DirList(DirCount)
    REDIM _PRESERVE FileList(FileCount)
END SUB

Note, I set this up to separate the search results into files and subdirectories separately.

Note 2. You'll need to download "direntry.h" from the attachment below and copy it into your QB64 folder for this to work. It doesn't need to be with the EXE once you compile it, but it does need to be in the QB64 folder for compiling.

Useage is rather simple (even if it doesn't look it at first).

First, Copy the library declarations into the top of the program where you'd like to make use of this routine.

DECLARE CUSTOMTYPE LIBRARY ".\direntry"
    FUNCTION load_dir& (s AS STRING)
    FUNCTION has_next_entry& ()
    SUB close_dir ()
    SUB get_next_entry (s AS STRING, flags AS LONG, file_size AS LONG)
END DECLARE


Then set two arrays to hold the directory and file information for you:

REDIM Dir(0) AS STRING, File(0) AS STRING

Then when you're ready, call the routine to get that directory's subdirectory list and file list updated:

GetFileList _CWD$, Dir(), File()

At this point, you now have the listing of whichever directory you wanted stored in those two arrays, which you can use for whatever purpose you needed.

In this case, I just cheesily printed them to the screen:

PRINT "SUBDIRECTORIES"
FOR i = 1 TO UBOUND(dir)
    PRINT Dir(i),
NEXT
PRINT
SLEEP

PRINT "FILES": PRINT: PRINT
FOR i = 1 TO UBOUND(file)
    PRINT File(i),
NEXT
PRINT


And, at the end of your code, don't forget to include the routine itself:

Code: (Select All)
SUB GetFileList (SearchDirectory AS STRING, DirList() AS STRING, FileList() AS STRING)
    CONST IS_DIR = 1
    CONST IS_FILE = 2
    DIM flags AS LONG, file_size AS LONG

    REDIM _PRESERVE DirList(100), FileList(100)
    DirCount = 0: FileCount = 0

    IF load_dir(SearchDirectory) THEN
        DO
            length = has_next_entry
            IF length > -1 THEN
                nam$ = SPACE$(length)
                get_next_entry nam$, flags, file_size
                IF flags AND IS_DIR THEN
                    DirCount = DirCount + 1
                    IF DirCount > UBOUND(DirList) THEN REDIM _PRESERVE DirList(UBOUND(DirList) + 100)
                    DirList(DirCount) = nam$
                ELSEIF flags AND IS_FILE THEN
                    FileCount = FileCount + 1
                    IF FileCount > UBOUND(filelist) THEN REDIM _PRESERVE FileList(UBOUND(filelist) + 100)
                    FileList(FileCount) = nam$
                END IF
            END IF
        LOOP UNTIL length = -1
        close_dir
    ELSE
    END IF
    REDIM _PRESERVE DirList(DirCount)
    REDIM _PRESERVE FileList(FileCount)
END SUB


direntry.h   -- Remember, you need this file.  It's a C header which QB64 will need to make use of.  Without it, the following won't run.
Reply


Messages In This Thread
File Listing To Arrays - by SMcNeill - 04-20-2022, 02:22 AM
RE: File Listing To Arrays - by mpgcan - 05-14-2022, 04:44 PM
RE: File Listing To Arrays - by SMcNeill - 05-14-2022, 05:15 PM
RE: File Listing To Arrays - by mnrvovrfc - 03-23-2023, 07:39 PM
RE: File Listing To Arrays - by SpriggsySpriggs - 03-23-2023, 08:16 PM
RE: File Listing To Arrays - by SMcNeill - 03-23-2023, 08:18 PM
RE: File Listing To Arrays - by mnrvovrfc - 03-23-2023, 08:58 PM
RE: File Listing To Arrays - by Petr - 03-25-2023, 07:55 PM



Users browsing this thread: 5 Guest(s)