Difference between revisions of "FILE$ (function)"

From QB64 Wiki
Jump to navigation Jump to search
imported>Clippy
m
imported>Clippy
m
Line 91: Line 91:
 
     Last$(cnt%) = {{Cb|MID$}}(line$, 1, 20)     
 
     Last$(cnt%) = {{Cb|MID$}}(line$, 1, 20)     
 
     {{Cb|IF}} {{Cb|MID$}}(line$, 40, 1) <> {{Cb|SPACE$}}(1) {{Cb|THEN}}
 
     {{Cb|IF}} {{Cb|MID$}}(line$, 40, 1) <> {{Cb|SPACE$}}(1) {{Cb|THEN}}
       SHFN$(cnt%) = {{Cb|MID$}}(line$, 40, 12)  
+
       SHFN$(cnt%) = {{Cb|MID$}}(line$, 40, 12): LGFN$(cnt%) = {{Cb|MID$}}(line$, 53)
      LGFN$(cnt%) = {{Cb|MID$}}(line$, 53)
 
 
     {{Cb|ELSE}} : SHFN$(cnt%) = {{Cb|MID$}}(line$, 53): LGFN$(cnt%) = ""
 
     {{Cb|ELSE}} : SHFN$(cnt%) = {{Cb|MID$}}(line$, 53): LGFN$(cnt%) = ""
 
     {{Cb|END IF}}
 
     {{Cb|END IF}}
Line 168: Line 167:
 
{{Cb|LOOP}} {{Cb|UNTIL}} {{Cb|LEN}}(Key$)
 
{{Cb|LOOP}} {{Cb|UNTIL}} {{Cb|LEN}}(Key$)
 
{{Cb|_DELAY}} .4 'adjust delay for page scroll speed
 
{{Cb|_DELAY}} .4 'adjust delay for page scroll speed
{{Cb|DO}}: Key$ = LOOP UNTIL Key$ = ""  'clear long keypress
+
{{Cb|DO}}: Key$ = {{Cb|INKEY$}}: LOOP UNTIL Key$ = ""  'clear long keypresses
{{Cb|IF}} last = 0 {{Cb|THEN}} {{Cb|RETURN}}             'exit if file no more data
+
{{Cb|IF}} last = 0 {{Cb|THEN}} {{Cb|RETURN}}                 'exit if file has no more data
 
_FILE$ = "": Spec$ = "": LFN$ = ""
 
_FILE$ = "": Spec$ = "": LFN$ = ""
 
{{Cb|CLS}}: {{Cb|SCREEN (statement)|SCREEN}} mode&
 
{{Cb|CLS}}: {{Cb|SCREEN (statement)|SCREEN}} mode&

Revision as of 09:46, 10 August 2010

The _FILE$ function can be used in place of the unsupported 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!

_FILE$ Function Features

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! 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.

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.

Demo Code:

_TITLE "Demo of _FILE$ Function by Ted Weissgerber 2010" handle& = _NEWIMAGE(800, 600, 256) 'Demo code creates custom sized screen mode SCREEN handle& Align 15, 2, "SUPER " + QQ$("_FILE$") + " SEARCH" Align 10, 5, "The _FILE$ function locates files from any input. Enter or " + QQ$("") + " same as " + QQ$("*.*") Align 10, 6, "Shared variables Path$ and LFN$ return path and long name if available." DO LOCATE 10, 20: PRINT SPACE$(70) LINEPUT 14, 10, "Enter a filename Search spec: ", spec$ IF UCASE$(spec$) = "END" THEN EXIT DO file$ = _FILE$(spec$) LOCATE 13, 20: PRINT SPACE$(70) Align 11, 13, file$ LOCATE 15, 1: PRINT SPACE$(90) Align 15, 15, Path$ + spec$ Align 10, 17, "Note that your " + QQ$("spec$") + " also returns the selected file name." LOCATE 20, 20: PRINT SPACE$(50) Align 13, 20, "LFN$ = " + LFN$ + " LEN =" + STR$(LEN(LFN$)) Align 11, 23, "Enter " + QQ$("END") + " to quit" LOOP SYSTEM '----------------------------- End of Demo Code -------------------------------

'****** NOTE: _FILE$ Function can also be included as an external library(see below) ****** FUNCTION _FILE$ (Spec$) SHARED Path$, LFN$ 'values also accessable by program STATIC LGFN$(25), SHFN$(25), Last$(25), DIR$(25) ERASE LGFN$, SHFN$, Last$, DIR$ IF LEN(ENVIRON$("OS")) = 0 THEN EXIT FUNCTION 'DIR X cannot be used on Win 9X Spec$ = UCASE$(LTRIM$(RTRIM$(Spec$))) IF INSTR(Spec$, "/A:D") OR INSTR(Spec$, "/O:G") THEN DL$ = "DIR": BS$ = "\" 'directory searches only ELSE : DL$ = SPACE$(3): BS$ = "" END IF mode& = _COPYIMAGE(0) 'save previous screen value to restore if display is necessary. ' Get Specific file information if available SHELL _HIDE "cmd /c dir " + Spec$ + " /X > D0S-DATA.INF" 'get data f% = FREEFILE Head$ = " Short Name Long Name Last Modified " tmp$ = " \ \ \ \ \ \ \ \" OPEN "D0S-DATA.INF" FOR INPUT AS #f% 'read the data file DO UNTIL EOF(f%) LINE INPUT #f%, line$ IF INSTR(line$, ":\") THEN Path$ = MID$(line$, INSTR(line$, ":\") - 1) IF RIGHT$(Path$, 1) <> "\" THEN Path$ = Path$ + "\" END IF IF LEN(line$) >= 53 THEN filecode% = ASC(UCASE$(MID$(line$, 53, 1))): ELSE filecode% = 0 D1R$ = MID$(line$, 26, 3) 'returns directories only with Spec$ = "/A:D" or "/O:G" IF D1R$ <> "DIR" THEN D1R$ = SPACE$(3) 'change if anything else IF LEN(line$) AND MID$(line$, 1, 1) <> " " AND D1R$ = DL$ AND filecode% >= setcode% THEN cnt% = cnt% + 1 DIR$(cnt%) = D1R$ Last$(cnt%) = MID$(line$, 1, 20) IF MID$(line$, 40, 1) <> SPACE$(1) THEN SHFN$(cnt%) = MID$(line$, 40, 12): LGFN$(cnt%) = MID$(line$, 53) ELSE : SHFN$(cnt%) = MID$(line$, 53): LGFN$(cnt%) = "" END IF IF LEN(Spec$) AND (Spec$ = UCASE$(SHFN$(cnt%)) OR Spec$ = UCASE$(LGFN$(cnt%))) THEN Spec$ = SHFN$(cnt%) + BS$: _FILE$ = Spec$: LFN$ = LGFN$(cnt%) + BS$ GOSUB KILLdata ' verifies file exist query (no display) END IF IF page% > 0 THEN IF cnt% = 1 THEN ' pages after first LINE (0, 0)-(639, 479), 0, BF COLOR 14: LOCATE 2, 3: PRINT Head$ LINE (4, 4)-(636, 476), 13, B: LINE (5, 5)-(635, 475), 13, B END IF COLOR 11: LOCATE , 3: PRINT USING tmp$; DIR$(cnt%); SHFN$(cnt%); LGFN$(cnt%); Last$(cnt%) IF DIR$(cnt%) = "DIR" AND LEFT$(SHFN$(cnt%), 1) = "." THEN SHFN$(cnt%) = "": LGFN$(cnt%) = "" ELSE 'page% = 0 IF cnt% = 2 THEN 'only display to screen if 2 or more files are found SCREEN 12: LINE (0, 0)-(639, 479), 0, BF COLOR 14: LOCATE 2, 3: PRINT Head$ LINE (4, 4)-(636, 476), 13, B: LINE (5, 5)-(635, 475), 13, B COLOR 11: LOCATE , 3: PRINT USING tmp$; DIR$(1); SHFN$(1); LGFN$(1); Last$(1) IF DIR$(1) = "DIR" AND LEFT$(SHFN$(1), 1) = "." THEN SHFN$(1) = "": LGFN$(1) = "" END IF IF cnt% > 1 THEN COLOR 11: LOCATE , 3: PRINT USING tmp$; DIR$(cnt%); SHFN$(cnt%); LGFN$(cnt%); Last$(cnt%) IF DIR$(cnt%) = "DIR" AND LEFT$(SHFN$(cnt%), 1) = "." THEN SHFN$(cnt%) = "": LGFN$(cnt%) = "" END IF END IF IF cnt% MOD 25 = 0 THEN 'each page holds 25 file names COLOR 14: LOCATE 29, 24: PRINT "Select file or click here for next."; GOSUB pickfile page% = page% + 1: cnt% = 0: ERASE SHFN$, LGFN$, Last$, DIR$ 'reset next page END IF END IF LOOP CLOSE #f% last = 1: total% = cnt% + (page% * 25) IF total% = 0 THEN _FILE$ = "": Spec$ = "": LFN$ = "": GOSUB KILLdata: 'no files found (no display) IF total% = 1 THEN Spec$ = SHFN$(1) + BS$: _FILE$ = Spec$: LFN$ = LGFN$(1) + BS$: GOSUB KILLdata 'one file found (no display) END IF IF DL$ = SPACE$(3) THEN COLOR 10: LOCATE 28, 30: PRINT "Total files found =" + STR$(total%); ELSE: COLOR 10: LOCATE 28, 29: PRINT "Total folders found =" + STR$(total%); END IF COLOR 14: LOCATE 29, 25: PRINT "Select file or click here to Exit."; pickfile: DO: Key$ = UCASE$(INKEY$): _LIMIT 30 DO WHILE _MOUSEINPUT Trow% = (_MOUSEY \ 16) + 1 ' find mouse row position Pick = _MOUSEBUTTON(1) ' get left button selection click LOOP IF Trow% > 2 AND Trow% < cnt% + 3 THEN R% = Trow% - 2 IF P% = 0 OR P% > cnt% + 3 THEN P% = R% IF P% = R% THEN 'highlight row COLOR 15: LOCATE R% + 2, 3: PRINT USING tmp$; DIR$(R%); SHFN$(R%); LGFN$(R%); Last$(R%) ELSE ' recolor when off row COLOR 11: LOCATE P% + 2, 3: PRINT USING tmp$; DIR$(P%); SHFN$(P%); LGFN$(P%); Last$(P%) END IF P% = R% IF Pick THEN Spec$ = SHFN$(R%) IF LEN(Spec$) THEN COLOR 13: LOCATE R% + 2, 3: PRINT USING tmp$; DIR$(R%); SHFN$(R%); LGFN$(R%); Last$(R%) Spec$ = Spec$ + BS$: _FILE$ = Spec$ IF LEN(LGFN$(R%)) THEN LFN$ = LGFN$(R%) + BS$ ELSE LFN$ = "" _DELAY 1.5: CLS: SCREEN mode&: GOSUB KILLdata 'exit if user selection END IF END IF END IF IF LEN(Key$) THEN usercode% = ASC(Key$) ELSE usercode% = 0 IF usercode% > setcode% THEN setcode% = usercode% 'user can set leading filename character to jump to IF Pick AND Trow% > 27 THEN EXIT DO LOOP UNTIL LEN(Key$) _DELAY .4 'adjust delay for page scroll speed DO: Key$ = INKEY$: LOOP UNTIL Key$ = "" 'clear long keypresses IF last = 0 THEN RETURN 'exit if file has no more data _FILE$ = "": Spec$ = "": LFN$ = "" CLS: SCREEN mode& KILLdata: CLOSE #f%: KILL "D0S-DATA.INF" 'kill D0S-DATA.INF file and exit EXIT FUNCTION RETURN END FUNCTION

'------------------------------- Other Demo SUBs ---------------------------------- SUB Align (c0l0r%, Tr0w%, t3xt$) Tc0l% = (_WIDTH \ 16) + 1 - (LEN(t3xt$) \ 2) IF Tc0l% <= 0 THEN Tc0l% = 2 COLOR c0l0r%: LOCATE Tr0w%, Tc0l%: PRINT t3xt$; END SUB SUB LINEPUT (c0l0r%, Tr0w%, t3xt$, r3turn$) Tc0l% = ((_WIDTH \ 16) - 10) - (LEN(t3xt$) \ 2) IF 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 <<<<<<<<<<

Refresh browser if full code is not displayed!


CREATING A _FILE$ $INCLUDE LIBRARY
The Function can be also be added to any program as a text library by saving it as a text BI file!
Just $INCLUDE the BI text file (grey area above) at the end of your program AFTER the SUB procedures.
To create a BI file use Notepad or another text editor, copy code in grey area and Save as ALL FILES using anyname.BI

_FILE$(Spec$) OPTIONS (/X already used!) Syntax: _FILE$([drive:][path][filename] [/A[[:]attrib]] [/L] [/O[[:]sortorder]] [/Q] [/T[[:]timefield]]) NOTE: When path is specified, that path is returned to the SHARED Path$ variable. The _FILE$ 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. /T time field displayed or used for sorting. Syntax: /T:timefield :A Last Access :C Creation :W Last Written /Q will display only the computer name, partial owner and filenames! DIR commands that don't work: /B, /C, /D, /N, /P, /S, /W, and /X is already used. These options return nothing or do not change a search!

NOTE: The data file name "D0S-DATA.INF" uses a zero instead of an "O" in D0S and is deleted after a search!


See also:

FILES, SHELL

_HIDE, _COPYIMAGE

$INCLUDE (Metacommand)



Navigation:
Go to Keyword Reference - Alphabetical
Go to Keyword Reference - By usage
Go to Main WIKI Page