FILELIST$ (function) - mkilgore/QB64pe GitHub Wiki
The FILELIST$ function can be used in place of the old QBasic FILES statement to view a list of files. Displays long and short(8.3) DOS filenames with the last modified information. Function name returns the short filename while SHARED string variables LFN$ and Path$ return the long name and absolute path from the current root drive. The leading underscore should make it compatible with most older QBasic program code run in QB64. The function allows mouse click file selection, letter press searches, file name exist verification without a screen display, and many of the DIR options available in DOS. Including sorting by name, extension, attributes and more! Your program screens are not changed after a display and the function can be integrated into a program using Putimage!
1) '''Only displays a file list when necessary.''' Otherwise one or no files found work "silently". Can be used to check if a file exists without an error! 2) '''Completely restores the program screen after a file list display!''' It is not necessary to create a new screen like you did after FILES. See Demo. 3) Users can jump to the leading filename letter by pressing a letter key. 4) Selection of files is done by a mouse click anywhere on a filename row. Clicking near the bottom or keypress can advance to the next page list. 5) Does not require '''*.*'''! Press Enter to display all files in current folder. No program typing errors! Just press Enter after a valid path for list! 6) If it doesn't find a file, it returns a 0 length string. '''NO FILE ERRORS!''' Just check for empty or zero length string returns in your program. 7) Includes various filename options for sorting as described below the code. 8) To view the current sub-directories, use Spec$ = '''"/A:D"''' in any lettercase. 9) Update: Display now '''highlights''' the mouse selection rows for clarity. Also Displays the present search path. Option '''/S''' will search all sub-directories. The file's actual '''Path$''' will be returned even if it was not displayed! 10) The newest version sets up the entire process using two position parameters so that it can be viewed on your program screen area. See '''[[FILELIST$]]''' |
The function is NOT a QB64 authorized function! This program code is free to use in your programs! No claims are made as to the suitability of the code and the user is responsible for any damages. You are free to alter the code as you see fit.
NOTE: IF $STATIC Metacommand is required, change function Arrays to STATIC and ERASE them at start and every loop!
SUB Align (c0l0r%, Tr0w%, t3xt$) 'auto centers printed text Tc0l% = (_WIDTH (function) \ 16) + 1 - (LEN(t3xt$) \ 2) IF...THEN Tc0l% <= 0 THEN Tc0l% = 2 COLOR c0l0r%: LOCATE Tr0w%, Tc0l%: PRINT t3xt$; END SUB SUB LINEPUT (c0l0r%, Tr0w%, t3xt$, r3turn$) 'Auto centering LINE INPUT entry allows quotation marks around filenames with spaces Tc0l% = ((_WIDTH (function) \ 16) - 10) - (LEN(t3xt$) \ 2) IF...THEN Tc0l% <= 0 THEN Tc0l% = 2 COLOR c0l0r%: LOCATE Tr0w%, Tc0l%: PRINT t3xt$; LINE INPUT ; "", r3turn$ 'semicolon in case bottom 2 rows END SUB FUNCTION QQ$ (t3xt$) 'Quote unQuote text in quotes QQ$ = CHR$(34) + t3xt$ + CHR$(34) END FUNCTION ' >>>>>>>>> Place INCLUDE file reference here when used as library <<<<<<<<< |
'****** NOTE: FILELIST$ Function can also be included as an external library(see below) ****** [[FUNCTION|FUNCTION]] FILELIST$ (Spec$) [[SHARED|SHARED]] Path$, LFN$ 'values also accessable by main program [[REDIM|REDIM]] LGFN$(25), SHFN$(25), Last$(25), DIR$(25), Paths$(25) '<<< [[$DYNAMIC|$DYNAMIC]] only [[IF...THEN|IF]] [[LEN|LEN]]([[ENVIRON$|ENVIRON$]]("OS")) = 0 [[THEN|THEN]] [[EXIT FUNCTION|EXIT FUNCTION]] 'DIR X cannot be used on Win 9X f% = [[FREEFILE|FREEFILE]] [[SHELL|SHELL]] [[_HIDE|_HIDE]] "CD > D0S-DATA.INF" '********************** Optional TITLE <<<<<<<<<< [[OPEN|OPEN]] "D0S-DATA.INF" [[FOR (file statement)|FOR]] [[INPUT (file mode)|INPUT]] [[AS|AS]] #f% [[LINE INPUT (file statement)|LINE INPUT]] #f%, current$ [[CLOSE|CLOSE]] #f% ' ******************************************** [[END|END]] TITLE(see [[_TITLE|_TITLE]] below) Spec$ = [[UCASE$|UCASE$]]([[LTRIM$|LTRIM$]]([[RTRIM$|RTRIM$]](Spec$))) [[IF...THEN|IF]] [[INSTR|INSTR]](Spec$, "/A:D") [[OR (boolean)|OR]] [[INSTR|INSTR]](Spec$, "/O:G") [[THEN|THEN]] DL$ = "DIR": BS$ = "\" 'directory searches only [[ELSE|ELSE]]: DL$ = [[SPACE$|SPACE$]](3): BS$ = "" [[END IF|END IF]] mode& = [[_COPYIMAGE|_COPYIMAGE]](0) 'save previous screen value to restore if files displayed. ' Get Specific file information if available [[SHELL|SHELL]] [[_HIDE|_HIDE]] "cmd /c dir " + Spec$ + " /X > D0S-DATA.INF" 'get data Head$ = " Short Name Long Name Last Modified " tmp$ = " \ \ \ \ \ \ \ \" [[OPEN|OPEN]] "D0S-DATA.INF" [[FOR (file statement)|FOR]] [[INPUT (file mode)|INPUT]] [[AS|AS]] #f% 'read the data file [[DO...LOOP|DO]] [[UNTIL|UNTIL]] [[EOF|EOF]](f%) [[LINE INPUT (file statement)|LINE INPUT]] #f%, line$ [[IF...THEN|IF]] [[INSTR|INSTR]](line$, ":\") [[THEN|THEN]] Path$ = [[MID$|MID$]](line$, [[INSTR|INSTR]](line$, ":\") - 1) [[IF...THEN|IF]] [[RIGHT$|RIGHT$]](Path$, 1) <> "\" [[THEN|THEN]] Path$ = Path$ + "\" setcode% = 0: filecode% = 0 [[END IF|END IF]] [[IF...THEN|IF]] [[LEN|LEN]](line$) > 25 [[AND (boolean)|AND]] [[MID$|MID$]](line$, 1, 1) <> " " [[THEN|THEN]] 'don't read other info [[IF...THEN|IF]] format% = 0 [[THEN|THEN]] [[IF...THEN|IF]] [[MID$|MID$]](line$, 20, 1) = "M" [[OR (boolean)|OR]] [[INSTR|INSTR]](line$, "<") = 25 [[THEN|THEN]] Sst% = 40: Lst% = 53: Dst% = 26: format% = 1 ' XP [[ELSE|ELSE]]: Sst% = 37: Lst% = 50: Dst% = 23: format% = 2 'VISTA [[END IF|END IF]] [[END IF|END IF]] [[IF...THEN|IF]] [[LEN|LEN]](line$) >= Lst% [[THEN|THEN]] filecode% = [[ASC|ASC]]([[UCASE$|UCASE$]]([[MID$|MID$]](line$, Lst%, 1))) [[ELSE|ELSE]] filecode% = 0 D1R$ = [[MID$|MID$]](line$, Dst%, 3) 'returns directories only with Spec$ = "/A:D" or "/O:G" [[IF...THEN|IF]] D1R$ <> "DIR" [[THEN|THEN]] D1R$ = [[SPACE$|SPACE$]](3) 'change if anything else [[IF...THEN|IF]] D1R$ = DL$ [[AND (boolean)|AND]] filecode% >= setcode% [[THEN|THEN]] cnt% = cnt% + 1 DIR$(cnt%) = D1R$: Paths$(cnt%) = Path$ Last$(cnt%) = [[MID$|MID$]](line$, 1, 20) [[IF...THEN|IF]] [[MID$|MID$]](line$, Sst%, 1) <> [[SPACE$|SPACE$]](1) [[THEN|THEN]] SHFN$(cnt%) = [[MID$|MID$]](line$, Sst%, [[INSTR|INSTR]](Sst%, line$, " ") - Sst%) LGFN$(cnt%) = [[MID$|MID$]](line$, Lst%) [[ELSE|ELSE]]: SHFN$(cnt%) = [[MID$|MID$]](line$, Lst%): LGFN$(cnt%) = "" [[END IF|END IF]] [[IF...THEN|IF]] [[LEN|LEN]](Spec$) [[AND (boolean)|AND]] (Spec$ = [[UCASE$|UCASE$]](SHFN$(cnt%)) [[OR (boolean)|OR]] Spec$ = [[UCASE$|UCASE$]](LGFN$(cnt%))) [[THEN|THEN]] Spec$ = SHFN$(cnt%) + BS$: FILELIST$ = Spec$: LFN$ = LGFN$(cnt%) + BS$ noshow = -1: [[GOSUB|GOSUB]] KILLdata ' verifies file exist query (no display) [[END IF|END IF]] [[IF...THEN|IF]] page% > 0 [[THEN|THEN]] ' pages after first [[IF...THEN|IF]] cnt% = 1 [[THEN|THEN]] [[GOSUB|GOSUB]] NewScreen [[COLOR|COLOR]] 11: [[LOCATE|LOCATE]] , 3: [[PRINT USING|PRINT USING]] tmp$; DIR$(cnt%); SHFN$(cnt%); LGFN$(cnt%); Last$(cnt%) [[IF...THEN|IF]] DIR$(cnt%) = "DIR" [[AND (boolean)|AND]] [[LEFT$|LEFT$]](SHFN$(cnt%), 1) = "." [[THEN|THEN]] SHFN$(cnt%) = "": LGFN$(cnt%) = "" [[ELSE|ELSE]] 'first page = 0 [[IF...THEN|IF]] cnt% = 2 [[THEN|THEN]] 'only display to screen if 2 or more files are found FList& = [[_NEWIMAGE|_NEWIMAGE]](640, 480, 256) [[SCREEN (statement)|SCREEN]] FList&: [[_TITLE|_TITLE]] current$: [[GOSUB|GOSUB]] NewScreen '********* Optional TITLE <<<<<<< [[COLOR|COLOR]] 11: [[LOCATE|LOCATE]] , 3: [[PRINT USING|PRINT USING]] tmp$; DIR$(1); SHFN$(1); LGFN$(1); Last$(1) [[_DISPLAY|_DISPLAY]] [[IF...THEN|IF]] DIR$(1) = "DIR" [[AND (boolean)|AND]] [[LEFT$|LEFT$]](SHFN$(1), 1) = "." [[THEN|THEN]] SHFN$(1) = "": LGFN$(1) = "" [[END IF|END IF]] [[IF...THEN|IF]] cnt% > 1 [[THEN|THEN]] [[COLOR|COLOR]] 11: [[LOCATE|LOCATE]] , 3: [[PRINT USING|PRINT USING]] tmp$; DIR$(cnt%); SHFN$(cnt%); LGFN$(cnt%); Last$(cnt%) [[IF...THEN|IF]] DIR$(cnt%) = "DIR" [[AND (boolean)|AND]] [[LEFT$|LEFT$]](SHFN$(cnt%), 1) = "." [[THEN|THEN]] SHFN$(cnt%) = "": LGFN$(cnt%) = "" [[END IF|END IF]] [[END IF|END IF]] 'page% [[IF...THEN|IF]] cnt% [[MOD|MOD]] 25 = 0 [[THEN|THEN]] 'each page holds 25 file names [[COLOR|COLOR]] 14: [[LOCATE|LOCATE]] 28, 24: [[PRINT|PRINT]] "Select file or click here for next."; '<<<< Prompt text can be changed [[GOSUB|GOSUB]] pickfile page% = page% + 1: cnt% = 0 [[REDIM|REDIM]] LGFN$(25), SHFN$(25), Last$(25), DIR$(25), Paths$(25) '<<< [[$DYNAMIC|$DYNAMIC]] only [[END IF|END IF]] 'mod 25 [[END IF|END IF]] 'DIR = DL$ [[END IF|END IF]] 'len line$ > 25 [[LOOP|LOOP]] [[CLOSE|CLOSE]] #f% last = 1: total% = cnt% + (page% * 25) [[IF...THEN|IF]] total% = 0 [[THEN|THEN]] FILELIST$ = "": Spec$ = "": LFN$ = "": noshow = -1: [[GOSUB|GOSUB]] KILLdata: 'no files(no display) [[IF...THEN|IF]] total% = 1 [[THEN|THEN]] 'one file(no display) Spec$ = SHFN$(1) + BS$: FILELIST$ = Spec$: LFN$ = LGFN$(1) + BS$: noshow = -1: [[GOSUB|GOSUB]] KILLdata [[END IF|END IF]] [[IF...THEN|IF]] DL$ = [[SPACE$|SPACE$]](3) [[THEN|THEN]] [[COLOR|COLOR]] 10: [[LOCATE|LOCATE]] 28, 65: [[PRINT|PRINT]] total%; "Files" [[ELSE|ELSE]]: [[COLOR|COLOR]] 10: [[LOCATE|LOCATE]] 28, 65: [[PRINT|PRINT]] total%; "Folders" [[END IF|END IF]] [[COLOR|COLOR]] 14: [[LOCATE|LOCATE]] 28, 24: [[PRINT|PRINT]] "Select file or click here to Exit. "; '<<< Prompt text can be changed pickfile: [[_DEST|_DEST]] FList& ShowPath$ = [[RIGHT$|RIGHT$]](Path$, 78) [[COLOR|COLOR]] 15: [[LOCATE|LOCATE]] 29, 41 - ([[LEN|LEN]](ShowPath$) \ 2): [[PRINT|PRINT]] ShowPath$; [[_DISPLAY|_DISPLAY]] DO: Key$ = [[UCASE$|UCASE$]]([[INKEY$|INKEY$]]): [[_LIMIT|_LIMIT]] 30 [[DO...LOOP|DO]] [[WHILE|WHILE]] [[_MOUSEINPUT|_MOUSEINPUT]] Tcol% = (([[_MOUSEX|_MOUSEX]] - xpos%) \ 8) + 1 'mouse column with Putimage offset Trow% = (([[_MOUSEY|_MOUSEY]] - ypos%) \ 16) + 1 'mouse row with offset Pick = [[_MOUSEBUTTON|_MOUSEBUTTON]](1) ' get left button selection click [[LOOP|LOOP]] [[IF...THEN|IF]] Trow% > 2 [[AND (boolean)|AND]] Trow% < cnt% + 3 [[AND (boolean)|AND]] Tcol% > 0 [[AND (boolean)|AND]] Tcol% < 80 [[THEN|THEN]] 'when mouse in area R% = Trow% - 2 [[IF...THEN|IF]] P% = 0 [[OR (boolean)|OR]] P% > cnt% + 3 [[THEN|THEN]] P% = R% [[IF...THEN|IF]] P% = R% [[THEN|THEN]] [[COLOR|COLOR]] 15: [[LOCATE|LOCATE]] R% + 2, 3: [[PRINT USING|PRINT USING]] tmp$; DIR$(R%); SHFN$(R%); LGFN$(R%); Last$(R%) [[ELSE|ELSE]] [[COLOR|COLOR]] 11: [[LOCATE|LOCATE]] P% + 2, 3: [[PRINT USING|PRINT USING]] tmp$; DIR$(P%); SHFN$(P%); LGFN$(P%); Last$(P%) [[END IF|END IF]] [[_DISPLAY|_DISPLAY]] P% = R% [[IF...THEN|IF]] Pick [[THEN|THEN]] Spec$ = SHFN$(R%) [[IF...THEN|IF]] [[LEN|LEN]](Spec$) [[THEN|THEN]] [[COLOR|COLOR]] 13: [[LOCATE|LOCATE]] R% + 2, 3: [[PRINT USING|PRINT USING]] tmp$; DIR$(R%); SHFN$(R%); LGFN$(R%); Last$(R%) [[_DISPLAY|_DISPLAY]] Spec$ = Spec$ + BS$: FILELIST$ = Spec$: Path$ = Paths$(R%) [[IF...THEN|IF]] [[LEN|LEN]](LGFN$(R%)) [[THEN|THEN]] LFN$ = LGFN$(R%) + BS$ [[ELSE|ELSE]] LFN$ = "" [[_DELAY|_DELAY]] 1.5: [[CLS|CLS]]: [[SCREEN (statement)|SCREEN]] mode&: [[GOSUB|GOSUB]] KILLdata 'exit if user selection [[END IF|END IF]] [[END IF|END IF]] 'len spec [[END IF|END IF]] 'pick [[IF...THEN|IF]] [[LEN|LEN]](Key$) [[THEN|THEN]] usercode% = [[ASC|ASC]](Key$) [[ELSE|ELSE]] usercode% = 0 [[IF...THEN|IF]] usercode% > setcode% [[THEN|THEN]] setcode% = usercode% 'user can press letter to jump to [[IF...THEN|IF]] Pick [[AND (boolean)|AND]] Trow% > 27 [[THEN|THEN]] [[EXIT DO|EXIT DO]] [[LOOP|LOOP]] [[UNTIL|UNTIL]] [[LEN|LEN]](Key$) [[_DELAY|_DELAY]] .4 'adjust delay for page scroll speed DO: Key$ = [[INKEY$|INKEY$]]: [[LOOP|LOOP]] [[UNTIL|UNTIL]] Key$ = "" [[IF...THEN|IF]] last = 0 [[THEN|THEN]] [[RETURN|RETURN]] 'exit if file no more data FILELIST$ = "": Spec$ = "": LFN$ = "" [[CLS|CLS]]: [[SCREEN (statement)|SCREEN]] mode& 'resets program screen to previous condition KILLdata: [[CLOSE|CLOSE]] #f%: [[KILL|KILL]] "D0S-DATA.INF" 'kill D0S-DATA.INF file and exit [[IF...THEN|IF]] FList& < -1 [[THEN|THEN]] [[_FREEIMAGE|_FREEIMAGE]] FList& [[IF...THEN|IF]] noshow = -1 AND mode& < -1 THEN [[_FREEIMAGE|_FREEIMAGE]] mode& [[_AUTODISPLAY|_AUTODISPLAY]] 'reset default settings [[EXIT FUNCTION|EXIT FUNCTION]] [[RETURN|RETURN]] NewScreen: 'clear screen and set display format [[LINE|LINE]] (0, 0)-(639, 499), 0, BF [[COLOR|COLOR]] 14: [[LOCATE|LOCATE]] 2, 3: [[PRINT|PRINT]] Head$ [[LINE|LINE]] (4, 4)-(636, 476), 13, B: [[LINE|LINE]] (5, 5)-(635, 475), 13, B [[RETURN|RETURN]] [[END FUNCTION|END FUNCTION]] '' '' |
'''FILELIST$(Spec$) OPTIONS''' (/X already used!) Syntax: FILELIST$([drive:][path][filename][/A[[:]attrib]][/L][/O[[:]sortorder]][/S][/T[[:]timefield]]) NOTE: When path is specified, that path is returned to the SHARED Path$ variable. The FILELIST$ function can use the following options: '''/A''' List by attribute. Syntax: /A:attribute :D Directories (use /A:D after any path or by itself) :R Read-only files :H Hidden files :A Files ready for archiving :S System files - Prefix means not '''/L''' Use lowercase. '''/O''' List of files in sorted order. Syntax: /O:sortorder :N By name (alphabetical is default) :S By size (smallest first) :E By extension (alphabetical) :D By date/time (oldest first) :G Group directories only (/O:G) - Prefix to reverse order NOTE: Alphabetical keypress is disabled when using /O. '''/S''' Searches all directory and subdirectory files '''/T''' Time field displayed or used for sorting. Syntax: /T:timefield :A Last Access :C Creation :W Last Written DIR commands that don't work: /B, /C, /D, /N, /P, /Q, and /W. (/X already used) These options return nothing or do not change a search! '''To Search filenames with spaces, add CHR$(34) quote to each side of the spec file name!''' filename$ = FILELIST$(CHR$(34) + Spec$ + CHR$(34)) '''NOTE: [[LINE INPUT]] allows quoted entries such as: "free cell.ico" for filenames with spaces.''' |
- NOTE: The data file name "D0S-DATA.INF" uses a zero instead of an "O" in D0S and is deleted after a search!
Navigation:
Go to Keyword Reference - Alphabetical
Go to Keyword Reference - By usage
Go to Main WIKI Page