Author Topic: Hey Dav, what about this? - Routine to fetch file list  (Read 3578 times)

0 Members and 1 Guest are viewing this topic.

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Hey Dav, what about this? - Routine to fetch file list
« on: January 25, 2021, 03:48:07 pm »
Just make sure you don't have any files named $my_tmp.tmp or $my_tmp2.tmp before you try it.

It makes a dir/files list like...

..
[Dir 1]
[Dir 2]
File1.foo
File2.foo
File3.foo

etc.

I have to augment the code for my date and time additions to the files, but without that info, this is all you need.

You probably already have this or a similar solution. I just posted it, since we are working on the same process.

Oh, if you are not familiar with the >> use in the second SHELL, it indicates APPEND to file instead of just WRITE to file.

Code: QB64: [Select]
  1. SHELL _HIDE "dir /b /A:D>$my_tmp.tmp"
  2. ff1% = FREEFILE
  3. OPEN "$my_tmp.tmp" FOR BINARY AS #ff1%
  4. ff2% = FREEFILE
  5. OPEN "$my_tmp2.tmp" FOR OUTPUT AS #ff2%
  6. PRINT #ff2%, ".."
  7. DO UNTIL EOF(ff1%)
  8.     LINE INPUT #ff1%, a$
  9.     a$ = "[" + a$ + "]"
  10.     PRINT #ff2%, a$
  11. CLOSE #ff1%, #ff2%
  12.  
  13. SHELL _HIDE "dir /b /A:-D *.*>>$my_tmp2.tmp"
  14.  
  15.  
  16. ' If you want to remove the $my_tmp.tmp and $my_tmp2.tmp files from the list, include this this...
  17. ' Otherwise use: SHELL _DONTWAIT "notepad $my_tmp2.tmp" here and end the routine.
  18.  
  19. ' Parse out      $my_tmp.tmp and $my_tmp2.tmp files from the list.
  20. ff1% = FREEFILE
  21. OPEN "$my_tmp.tmp" FOR OUTPUT AS #ff1%: CLOSE #ff1%
  22. ff1% = FREEFILE
  23. OPEN "$my_tmp2.tmp" FOR BINARY AS #ff1%
  24. a$ = SPACE$(LOF(ff1%))
  25. GET #ff1%, , a$
  26. CLOSE #ff1%
  27.  
  28. DO UNTIL INSTR(a$, "$my_tmp.tmp") = 0
  29.     a$ = MID$(a$, 1, INSTR(a$, "$my_tmp.tmp") - 1) + MID$(a$, INSTR(a$, "$my_tmp.tmp") + 13)
  30. DO UNTIL INSTR(a$, "$my_tmp2.tmp") = 0
  31.     a$ = MID$(a$, 1, INSTR(a$, "$my_tmp2.tmp") - 1) + MID$(a$, INSTR(a$, "$my_tmp2.tmp") + 14)
  32.  
  33. ff1% = FREEFILE
  34. OPEN "$my_tmp.tmp" FOR BINARY AS #ff1%
  35. PUT #ff1%, , a$
  36. CLOSE #ff1%
  37. REM If you want to KILL the unneeded second temporary file, use: ''IF _FILEEXISTS("$my_tmp2.tmp") THEN KILL "$my_tmp2.tmp"
  38.  
  39. SHELL _DONTWAIT "notepad $my_tmp.tmp"

Pete
« Last Edit: January 25, 2021, 05:35:09 pm by odin »
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/

Offline Dav

  • Forum Resident
  • Posts: 792
    • View Profile
Re: Hey Dav, what about this?
« Reply #1 on: January 25, 2021, 03:59:16 pm »
Yep, That's about the way I'm doing it here.  But yours is working correctly, and mine was showing a duplicate entry for some reason. 

I completely forgot about >>! Thanks for reminding me. 

Edit:  Hey, @Pete, the way you're calling dir fixed my problem.  I was doing it wrong.  Thanks.  I'll update my code with the fix.

- Dav

« Last Edit: January 25, 2021, 04:09:13 pm by Dav »

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: Hey Dav, what about this?
« Reply #2 on: January 25, 2021, 04:15:26 pm »
Glad it helped.

I'll post what I'm going to try as soon as I expand it to pick up the mix of directories and files with date and time creation. Also keep in mind, DOS did provide a variety of sorting switches:

N|-N - By name (alphabetical or reverse alphabetical).
E|-E - By extension ( alphabetical or reverse alphabetical).
D|-D - By date and time (chronologically or reverse).
S|-S - By size (increasing or decreasing).
C|-C - Sorts by DoubleSpace compression ratio lowest to highest or highest to lowest. (Version 6.0 only)
G|-G - Group directories (before, or after) other files.

From www.easydos.com

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

Offline Dav

  • Forum Resident
  • Posts: 792
    • View Profile
Re: Hey Dav, what about this?
« Reply #3 on: January 25, 2021, 04:16:54 pm »
Man...I've forgotten so much of dos commandline usage.  Gotta brush up on it again.  Thanks!

- Dav

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Hey Dav, what about this?
« Reply #4 on: January 25, 2021, 05:13:39 pm »
Here's how I do this type of thing -- I call one command, and have it sort things out for me into two arrays; one for directories and one for files.

Code: QB64: [Select]
  1.     FUNCTION load_dir& (s AS STRING)
  2.     FUNCTION has_next_entry& ()
  3.     SUB close_dir ()
  4.     SUB get_next_entry (s AS STRING, flags AS LONG, file_size AS LONG)
  5.  
  6. REDIM Dir(0) AS STRING, File(0) AS STRING
  7.  
  8.  
  9. GetFileList _CWD$, Dir(), File()
  10.  
  11. PRINT "SUBDIRECTORIES OF:"; _CWD$
  12. FOR i = 1 TO UBOUND(dir)
  13.     PRINT Dir(i),
  14. PRINT "FILES OF:"; _CWD$
  15. FOR i = 1 TO UBOUND(file)
  16.     PRINT File(i),
  17.  
  18. SUB GetFileList (SearchDirectory AS STRING, DirList() AS STRING, FileList() AS STRING)
  19.     CONST IS_DIR = 1
  20.     CONST IS_FILE = 2
  21.     DIM flags AS LONG, file_size AS LONG
  22.  
  23.     REDIM _PRESERVE DirList(100), FileList(100)
  24.     DirCount = 0: FileCount = 0
  25.  
  26.     IF load_dir(SearchDirectory + CHR$(0)) THEN
  27.         DO
  28.             length = has_next_entry
  29.             IF length > -1 THEN
  30.                 nam$ = SPACE$(length)
  31.                 get_next_entry nam$, flags, file_size
  32.                 IF _DIREXISTS(nam$) THEN
  33.                     DirCount = DirCount + 1
  34.                     IF DirCount > UBOUND(DirList) THEN REDIM _PRESERVE DirList(UBOUND(DirList) + 100)
  35.                     DirList(DirCount) = nam$
  36.                 ELSEIF _FILEEXISTS(nam$) THEN
  37.                     FileCount = FileCount + 1
  38.                     IF FileCount > UBOUND(filelist) THEN REDIM _PRESERVE FileList(UBOUND(filelist) + 100)
  39.                     FileList(FileCount) = nam$
  40.                 END IF
  41.             END IF
  42.         LOOP UNTIL length = -1
  43.         close_dir
  44.     END IF
  45.     REDIM _PRESERVE DirList(DirCount)
  46.     REDIM _PRESERVE FileList(FileCount)
  47.  
  48.  

Just copy direntry.h and put it in your folder with your BAS file and you're good to go.  Once compiled, it doesn't need to be shared (just like you don't have to share the BAS file itself), if all you want to do is distribute the executable.

Works on Windows, Mac, and Linux systems.
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline Dav

  • Forum Resident
  • Posts: 792
    • View Profile
Re: Hey Dav, what about this?
« Reply #5 on: January 25, 2021, 05:25:57 pm »
That does work very well, Steve.  I can get the filesize too fairly easy, STR$(file_size).

- Dav

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Hey Dav, what about this?
« Reply #6 on: January 25, 2021, 05:27:17 pm »
And if you want a GUI, feel free to make use of this one for your needs:

Code: QB64: [Select]
  1. DECLARE CUSTOMTYPE LIBRARY "IDE Files\direntry"
  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.  
  9. SCREEN _NEWIMAGE(800, 600, 32)
  10.  
  11. PRINT SelectFile$("*.bas;*.bi;*.bm;*.txt", 50, 50)
  12.  
  13.  
  14.  
  15.  
  16. FUNCTION SelectFile$ (search$, x AS INTEGER, y AS INTEGER)
  17.     'save some old values
  18.     LoadFile_DC = _DEFAULTCOLOR: LoadFile_BG = _BACKGROUNDCOLOR
  19.     LoadFile_s = _SOURCE: LoadFile_d = _DEST
  20.     f = _FONT: _FONT 16
  21.     'some variables
  22.  
  23.     LoadFile_BoxColor = &HFFAAAAFF
  24.     LoadFile_FolderColor = &HFFFFFF00
  25.     LoadFile_FileColor = &HFFFFFFFF
  26.     IF INSTR(_OS$, "[WINDOWS]") THEN LoadFile_Slash$ = "\" ELSE LoadFile_Slash$ = "/"
  27.     LoadFile_Dir$ = SPACE$(FILE_current_dir_length)
  28.     FILE_get_current_dir LoadFile_Dir$
  29.     LoadFile_Dir$ = LoadFile_Dir$ + LoadFile_Slash$
  30.     'LoadFile_Dir$ = "." + LoadFile_Slash$
  31.     LoadFile_w = 639: LoadFile_h = 479
  32.     REDIM LoadFile_Label(0) AS STRING: LoadFile_Label(0) = "DIR"
  33.     REDIM LoadFile_DirList(-1 TO 9, -1 TO 9999) AS STRING
  34.     LoadFile_last = 1
  35.     FolderDeep = 1
  36.  
  37.     'some error checking
  38.     IF search$ = "" THEN EXIT SUB 'We can't search for nothing!
  39.  
  40.     'Copy background
  41.     PCOPY 0, 1
  42.     'set workscreen
  43.     LoadFile_ws = _NEWIMAGE(640, 480, 32)
  44.  
  45.     'Count our filetypes to display
  46.     LoadFile_TypeCount = 0
  47.     DO
  48.         LoadFile_TypeCount = LoadFile_TypeCount + 1
  49.         LoadFile_l = INSTR(LoadFile_l + 1, search$, ";") ' look for ; to denote more files
  50.         REDIM _PRESERVE LoadFile_Label(LoadFile_TypeCount) AS STRING
  51.         IF LoadFile_l > 0 THEN LoadFile_Label(LoadFile_TypeCount) = MID$(search$, LoadFile_last + 1, LoadFile_l - LoadFile_last - 1) ELSE LoadFile_Label(LoadFile_TypeCount) = MID$(search$, LoadFile_last + 1, LEN(search$) - LoadFile_last)
  52.         LoadFile_last = LoadFile_l + 1
  53.     LOOP UNTIL LoadFile_l = 0
  54.     LoadFile_l = 640 / (LoadFile_TypeCount + 1)
  55.     REDIM LoadFile_start(LoadFile_TypeCount), LoadFile_previous(LoadFile_TypeCount), LoadFile_more(LoadFile_TypeCount), LoadFile_Count(LoadFile_TypeCount)
  56.     FOR i = 0 TO LoadFile_TypeCount: LoadFile_start(i) = 1: NEXT
  57.  
  58.     _SOURCE LoadFile_ws: _DEST LoadFile_ws
  59.     DO
  60.         _LIMIT 30
  61.         FOR i = 0 TO LoadFile_TypeCount
  62.             LoadFile_Count(i) = 0
  63.             FOR j = 0 TO 9999
  64.                 LoadFile_DirList(i, j) = ""
  65.             NEXT
  66.         NEXT
  67.         'Generate our updated directory listings.
  68.  
  69.         IF FILE_load_dir&(LoadFile_Dir$ + CHR$(0)) THEN
  70.             DO
  71.                 LoadFile_length = FILE_has_next_entry 'Get length of next entry
  72.                 IF LoadFile_length > -1 THEN 'If we have a next entry
  73.                     LoadFile_nam$ = SPACE$(LoadFile_length) 'Set the size of our string
  74.                     FILE_get_next_entry LoadFile_nam$, LoadFile_flags, LoadFile_file_size 'Get the file's name, size, and 'flags'
  75.                     'Check if it's a file or a directory
  76.  
  77.                     IF _DIREXISTS(LoadFile_Dir$ + LoadFile_nam$) THEN
  78.                         IF LoadFile_nam$ <> "." THEN
  79.                             LoadFile_Count(0) = LoadFile_Count(0) + 1
  80.                             LoadFile_DirList(0, LoadFile_Count(0)) = LoadFile_nam$
  81.                         END IF
  82.                     ELSE 'We have a file
  83.                         FOR i = 1 TO LoadFile_TypeCount
  84.                             LoadFile_ext$ = RIGHT$(LoadFile_nam$, LEN(LoadFile_Label(i)))
  85.                             IF UCASE$(LoadFile_ext$) = UCASE$(LoadFile_Label(i)) THEN
  86.                                 LoadFile_Count(i) = LoadFile_Count(i) + 1
  87.                                 LoadFile_DirList(i, LoadFile_Count(i)) = LEFT$(LoadFile_nam$, LEN(LoadFile_nam$) - LEN(LoadFile_Label(i)))
  88.                                 EXIT FOR
  89.                             ELSEIF LoadFile_Label(i) = ".*" THEN
  90.                                 LoadFile_Count(i) = LoadFile_Count(i) + 1
  91.                                 LoadFile_DirList(i, LoadFile_Count(i)) = LoadFile_nam$
  92.                             END IF
  93.                         NEXT
  94.                     END IF
  95.                 END IF
  96.             LOOP UNTIL LoadFile_length = -1
  97.             FILE_close_dir
  98.         END IF
  99.  
  100.         updatelist:
  101.  
  102.  
  103.         CLS , &HFF005050 'Draw a nice display box
  104.         COLOR , 0
  105.         LINE (0, 0)-(LoadFile_w, LoadFile_h + 5 - 2 * 16), LoadFile_BoxColor, B
  106.         LINE (1, 1)-(LoadFile_w - 1, LoadFile_h + 6 - 2 * 16), LoadFile_BoxColor, B
  107.         LINE (0, 0)-(LoadFile_w, LoadFile_h), LoadFile_BoxColor, B
  108.         LINE (1, 1)-(LoadFile_w - 1, LoadFile_h - 1), LoadFile_BoxColor, B
  109.  
  110.         LINE (0, 16 + 3)-(LoadFile_w, 16 + 3), LoadFile_BoxColor
  111.         LINE (0, 16 + 4)-(LoadFile_w, 16 + 4), LoadFile_BoxColor
  112.         FOR i = 0 TO LoadFile_TypeCount
  113.             _PRINTSTRING (i * LoadFile_l + (LoadFile_l - 8 * LEN(LoadFile_Label(i))) / 2, 2), LoadFile_Label(i)
  114.             LINE (i * LoadFile_l, 0)-(i * LoadFile_l, LoadFile_h + 5 - 2 * 16), LoadFile_BoxColor
  115.         NEXT
  116.  
  117.         LINE (627, 2)-(637, 18), &HFFFF0000, BF
  118.         LINE (626, 2)-(637, 18), &HFF000000, B
  119.  
  120.         _PRINTSTRING (628, 2), "X"
  121.         IF selection > 0 THEN
  122.             IF LoadFile_Label(row) <> ".*" AND LoadFile_Label(row) <> "DIR" THEN temp$ = LoadFile_DirList(row, selection) + LoadFile_Label(row) ELSE temp$ = LoadFile_DirList(row, selection)
  123.             IF LoadFile_DirList(row, selection) = "" THEN temp$ = ""
  124.             selection = 0
  125.         END IF
  126.         _PRINTSTRING (10, 28 * 16 + 7), LoadFile_Dir$
  127.         _PRINTSTRING (630 - LEN(temp$) * 8, 28 * 16 + 7), temp$
  128.         IF temp$ = "" THEN oldselection = 0
  129.         IF oldselection > 0 THEN LINE (row * LoadFile_l, (oldselection + 1) * 16 + 5)-((row + 1) * LoadFile_l, (oldselection + 2) * 16 + 5), &HAAAAA000, BF
  130.  
  131.         FOR i = 0 TO UBOUND(LoadFile_label)
  132.             IF i = 0 THEN COLOR LoadFile_FolderColor ELSE COLOR LoadFile_FileColor
  133.             counter = 0
  134.             FOR j = LoadFile_start(i) TO LoadFile_start(i) + 24
  135.                 counter = counter + 1
  136.                 IF LoadFile_DirList(i, j) = "" THEN EXIT FOR
  137.                 _PRINTSTRING (i * LoadFile_l + 5, (counter + 1) * 16 + 7), LEFT$(LoadFile_DirList(i, j), LoadFile_l / 8 - 2)
  138.             NEXT
  139.             IF j = LoadFile_start(i) + 25 THEN LoadFile_more(i) = -1 ELSE LoadFile_more(i) = 0
  140.             IF LoadFile_start(i) > 1 THEN LoadFile_previous(i) = -1 ELSE LoadFile_previous(i) = 0
  141.             IF LoadFile_more(i) THEN
  142.                 LINE (i * LoadFile_l + 2, 27 * 16 + 5)-((i + 1) * LoadFile_l - 3, 28 * 16 + 3), &HFFFF0000, BF
  143.                 LINE (i * LoadFile_l + 2, 27 * 16 + 5)-((i + 1) * LoadFile_l - 3, 28 * 16 + 3), BoxColor, B
  144.                 COLOR &HFFFFFF00: _PRINTSTRING (i * LoadFile_l + (LoadFile_l - 8 * 11) / 2, 27 * 16 + 5), "SCROLL DOWN"
  145.                 COLOR LoadFile_FileColor
  146.             END IF
  147.             IF LoadFile_previous(i) THEN
  148.                 LINE (i * LoadFile_l + 2, 16 + 5)-((i + 1) * LoadFile_l - 3, 2 * 16 + 3), &HFFFF0000, BF
  149.                 LINE (i * LoadFile_l + 2, 16 + 5)-((i + 1) * LoadFile_l - 3, 2 * 16 + 3), BoxColor, B
  150.                 COLOR &HFFFFFF00: _PRINTSTRING (i * LoadFile_l + (LoadFile_l - 8 * 9) / 2, 16 + 5), "SCROLL UP"
  151.                 COLOR LoadFile_FileColor
  152.             END IF
  153.         NEXT
  154.  
  155.         _PUTIMAGE (0 + x, 0 + y)-(640 + x, 480 + y), LoadFile_ws, 0
  156.         _DISPLAY
  157.  
  158.         change = 0
  159.         DO
  160.             _LIMIT 30
  161.             LoadFile_LMB = 0 'This sets the left mouse button as unacceptable.
  162.             a = _KEYHIT
  163.             SELECT CASE a
  164.                 CASE 8 'backspace
  165.                     temp$ = LEFT$(temp$, LEN(temp$) - 1)
  166.                     change = -1
  167.                 CASE 13 'enter
  168.                     DO: LOOP UNTIL INKEY$ = "" 'Clear the keyboard buffer so it doesn't affect the main program.
  169.                     temp$ = LoadFile_Dir$ + temp$
  170.                     COLOR LoadFile_DC, LoadFile_BG: _SOURCE LoadFile_s: _DEST LoadFile_d: PCOPY 1, 0: _DISPLAY: SelectFile$ = temp$ 'Restore our old settings
  171.                     _FONT f
  172.                     EXIT SUB 'And leave
  173.                 CASE 27 'If ESC is pressed then...
  174.                     DO: LOOP UNTIL INKEY$ = "" 'Clear the keyboard buffer so it doesn't affect the main program.
  175.                     COLOR LoadFile_DC, LoadFile_BG: _SOURCE LoadFile_s: _DEST LoadFile_d: PCOPY 1, 0: _DISPLAY: SelectFile$ = "" 'Restore our old settings
  176.                     _FONT f
  177.                     EXIT SUB 'And leave
  178.                 CASE 32 TO 126
  179.                     temp$ = temp$ + CHR$(a)
  180.                     change = -1
  181.             END SELECT
  182.             DO
  183.                 IF _MOUSEBUTTON(1) = 0 THEN LoadFile_LMB = -1 'Only by lifting the mouse, will we count it as down
  184.                 'Note: we ignore LoadFile_LMB for the scroll bars, so we can just hold it down and scroll happily forever and ever...
  185.                 'or until we get to the limit of our file list.
  186.                 'We only check LoadFile_LMB when actually trying to select an item from our list.   No more "OOP!  I held it too long and did something I didn't want to do!"
  187.                 'Now we click once to select, click again to accept that selection.
  188.             LOOP WHILE _MOUSEINPUT
  189.             MX = _MOUSEX: MY = _MOUSEY
  190.             IF _MOUSEBUTTON(2) OR (LoadFile_LMB AND MX > 626 + x AND MX < 638 + x AND MY > 1 + y AND MY < 19 + y AND _MOUSEBUTTON(1)) THEN
  191.                 'restore those old values, and just exit.  Right mouse is an escape
  192.                 COLOR LoadFile_DC, LoadFile_BG: _SOURCE LoadFile_s: _DEST LoadFile_d: PCOPY 1, 0: _DISPLAY: SelectFile$ = ""
  193.                 _FONT f
  194.                 EXIT SUB
  195.             END IF
  196.             IF _MOUSEBUTTON(1) THEN 'Without the mouse being down, we don't need to check squat!
  197.                 'Check the 2 roLoadFile_ws for a click in the proper Y position
  198.                 IF MY >= 16 + 5 + y AND MY <= 2 * 16 + 3 + y THEN 'We're on the top row
  199.                     FOR j = 0 TO UBOUND(LoadFile_label)
  200.                         IF LoadFile_previous(j) AND MX >= j * LoadFile_l + 2 + x AND MX <= (j + 1) * LoadFile_l - 3 + x THEN
  201.                             LoadFile_start(j) = LoadFile_start(j) - 1
  202.                             change = -1: selection = 0: click = 0: temp$ = ""
  203.                             EXIT FOR
  204.                         END IF
  205.                     NEXT
  206.                 ELSEIF MY >= 27 * 16 + 5 + y AND MY <= 28 * 16 + 3 + y THEN 'We're on the bottom row
  207.                     FOR j = 0 TO UBOUND(LoadFile_label)
  208.                         IF LoadFile_more(j) AND MX >= j * LoadFile_l + 2 + x AND MX <= (j + 1) * LoadFile_l - 3 + x THEN
  209.                             LoadFile_start(j) = LoadFile_start(j) + 1
  210.                             change = -1: selection = 0: click = 0: temp$ = ""
  211.                             EXIT FOR
  212.                         END IF
  213.                     NEXT
  214.                 ELSEIF MY >= 37 + y AND MY <= 437 + y AND LoadFile_LMB THEN 'It's in a column somewhere.  Did someone click an item?!
  215.                     FOR j = 0 TO UBOUND(LoadFile_label)
  216.                         IF MX >= j * LoadFile_l + 2 + x AND MX <= (j + 1) * LoadFile_l - 3 + x THEN
  217.                             row = j
  218.                             oldselection = INT((MY - y - 37) / 16) + 1
  219.                             selection = LoadFile_start(j) + oldselection - 1
  220.                             change = -1
  221.                             click = -1
  222.                             EXIT FOR
  223.                         END IF
  224.                     NEXT
  225.                 END IF
  226.             END IF
  227.  
  228.             _DISPLAY
  229.         LOOP UNTIL change
  230.         IF click THEN 'we clicked something besides a scroll bar
  231.             IF LoadFile_Label(row) <> ".*" AND LoadFile_Label(row) <> "DIR" THEN temp1$ = LoadFile_DirList(row, selection) + LoadFile_Label(row) ELSE temp1$ = LoadFile_DirList(row, selection)
  232.             IF temp$ = temp1$ THEN
  233.                 'We picked one!
  234.                 SELECT CASE LoadFile_Label(row)
  235.                     CASE "DIR"
  236.                         IF LoadFile_DirList(row, selection) <> ".." THEN
  237.                             LoadFile_Dir$ = LoadFile_Dir$ + LoadFile_DirList(row, selection) + LoadFile_Slash$
  238.                         ELSE
  239.                             DO
  240.                                 LoadFile_Dir$ = LEFT$(LoadFile_Dir$, LEN(LoadFile_Dir$) - 1)
  241.                             LOOP UNTIL RIGHT$(LoadFile_Dir$, 1) = LoadFile_Slash$ OR LEN(LoadFile_Dir$) = 0
  242.                         END IF
  243.                         FOR i = 1 TO UBOUND(Loadfile_start)
  244.                             LoadFile_start(i) = 1
  245.                         NEXT
  246.                         selection = 0: temp$ = "": oldselection = 0
  247.                     CASE ".*": SelectFile$ = LoadFile_Dir$ + temp$: EXIT DO
  248.                     CASE ELSE: SelectFile$ = LoadFile_Dir$ + temp$: EXIT DO
  249.                 END SELECT
  250.             END IF
  251.             IF row > 0 THEN _DELAY .2: GOTO updatelist
  252.         ELSE
  253.             _DELAY .05
  254.             GOTO updatelist
  255.         END IF
  256.     LOOP
  257.     'restore those old values
  258.     COLOR LoadFile_DC, LoadFile_BG: _SOURCE LoadFile_s: _DEST LoadFile_d: PCOPY 1, 0: _DISPLAY
  259.     _FONT f
  260.  
  261. 'If you don't have a copy of direntry.h in your QB64 folder, then copy the following code into a new IDE window.
  262. 'Then remove the remarks.
  263. 'And save it as direntry.h
  264. 'direntry.h is required for this to work properly with the library files.
  265. 'I thought adding the code here would be a way to make certain that it'd be easy to recover the file
  266. 'in case something ever happened and it was accidently deleted off the drive for some reason.
  267.  
  268. '#include <dirent.h>
  269. '#include <sys/stat.h>
  270. '
  271. 'const int IS_DIR_FLAG = 1, IS_FILE_FLAG = 2;
  272. '
  273. 'DIR *pdir;
  274. 'struct dirent *next_entry;
  275. 'struct stat statbuf;
  276. '
  277. 'int load_dir (char * path) {
  278. 'struct dirent *pent;
  279. 'struct stat statbuf;
  280. '//Open current directory
  281. 'pdir = opendir(path);
  282. 'if (!pdir) {
  283. 'return 0; //Didn't open
  284. '}
  285. 'return -1;
  286. '}
  287. '
  288. 'int has_next_entry () {
  289. '  next_entry = readdir(pdir);
  290. '  if (next_entry == NULL) return -1;
  291. '
  292. '  stat(next_entry->d_name, &statbuf);
  293. '  return strlen(next_entry->d_name);
  294. '}
  295. '
  296. 'void get_next_entry (char * nam, int * flags, int * file_size) {
  297. '  strcpy(nam, next_entry->d_name);
  298. '  if (S_ISDIR(statbuf.st_mode)) {
  299. '    *flags = IS_DIR_FLAG;
  300. '  } else {
  301. '    *flags = IS_FILE_FLAG;
  302. '  }
  303. '  *file_size = statbuf.st_size;
  304. '  return ;
  305. '}
  306. '
  307. 'void close_dir () {
  308. '  closedir(pdir);
  309. '  pdir = NULL;
  310. '  return ;
  311. '}
  312.  

Works with the same direntry.h as above, so be certain to have that header file in the same folder as the BAS file which includes this.  Usage should be simple enough to sort out -- it's basically just a single line to call the routine, after the DECLARE LIBRARY is added.  In this case, it's a simple litte:

PRINT SelectFile$("*.bas;*.bi;*.bm;*.txt", 50, 50)
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline Dav

  • Forum Resident
  • Posts: 792
    • View Profile
Re: Hey Dav, what about this?
« Reply #7 on: January 25, 2021, 05:33:28 pm »
Nice one!  I'm saving it.  I think I'll add arrows to it, tab to switch panes.

Thanks for sharing!

- Dav

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: Hey Dav, what about this?
« Reply #8 on: January 25, 2021, 05:34:04 pm »
And here's how I do it, but I'm shy, so I only do it over at my forum...

https://www.tapatalk.com/groups/qbasic/my-latest-screen-0-project-word-processor-t39636-s40.html#p213469

Last post.

Hey Dav and Steve,

If you could both check out the link above, you will see Bob commented on Dav's two latest programs, posted a very old keyboard routine of his own) and it looks like with some QB64 encouragement, he might expand that puzzle project of his to include all jpgs, like Dav's. He just isn't up on the new stuff, which limited him from doing it with QBasic in the first place. I can't help him with any of that, because I don't use the graphics statements. Anyway, it would be great to see TheBOB actively programming again. Maybe I'm not reading this right, but I think if he got acquainted and comfortable with some of new tools, he might just get the urge to write some more apps.

@Steve - I'll have a look at it. I've got a couple of alternate ways, Windows API, the code you posted, which I've seen somewhere around here, before, and the good ol' SHELL method posted at QBF.

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

Offline Dav

  • Forum Resident
  • Posts: 792
    • View Profile
Re: Hey Dav, what about this? - Routine to fetch file list
« Reply #9 on: January 25, 2021, 06:02:03 pm »
Cool!  I would love to see TheBob active in QB64.  And I'm sure would be making some real pro stuff too!

Thanks for the link,@Pete.

- Dav