Difference between revisions of "DIR$"

From QB64 Wiki
Jump to navigation Jump to search
Line 1: Line 1:
The '''DIR$''' function below is '''NOT A QB64 FUNCTION!''' This page supplies equivalent QB code:
The '''DIR$''' function below is '''NOT A QB64 FUNCTION!''' This page supplies equivalent QB code:

Revision as of 16:20, 5 May 2017

The DIR$ function below is NOT A QB64 FUNCTION! This page supplies equivalent QB code:


filename$ = DIR$({filespec$|""})

The DIR$ function used in PDS(7.1) returns a filename or a list when more than one exist. The file spec can use a path and/or wildcards.

FOR i = 1 TO 2 PRINT LINE INPUT "Enter a file spec: ", spec$ file$ = DIR$(spec$) 'use a file spec ONCE to find the last file name listed PRINT DIRCount%, file$, 'function can return the file count using SHARED variable IF DIRCount% > 1 THEN DO K$ = INPUT$(1) file$ = DIR$("") 'use an empty string parameter to return a list of files! PRINT file$, LOOP UNTIL LEN(file$) = 0 'file list ends with an empty string END IF NEXT END FUNCTION DIR$ (spec$) CONST TmpFile$ = "DIR$INF0.INF", ListMAX% = 500 'change maximum to suit your needs SHARED DIRCount% 'returns file count if desired. MAY CONFLICT with user's program code STATIC Ready%, Index%, DirList$() IF NOT Ready% THEN REDIM DirList$(ListMAX%): Ready% = -1 'DIM array first use IF spec$ > "" THEN 'get file names when a spec is given SHELL _HIDE "DIR " + spec$ + " /b > " + TmpFile$ Index% = 0: DirList$(Index%) = "": ff% = FREEFILE OPEN TmpFile$ FOR APPEND AS #ff% size& = LOF(ff%) CLOSE #ff% IF size& = 0 THEN KILL TmpFile$: EXIT FUNCTION OPEN TmpFile$ FOR INPUT AS #ff% DO WHILE NOT EOF(ff%) AND Index% < ListMAX% Index% = Index% + 1 LINE INPUT #ff%, DirList$(Index%) LOOP DIRCount% = Index% 'SHARED variable can return the file count CLOSE #ff% KILL TmpFile$ ELSE IF Index% > 0 THEN Index% = Index% - 1 'no spec sends next file name END IF DIR$ = DirList$(Index%) END FUNCTION

Code by Ted Weissgerber
Explanation: The function will verify that a file exists (even if it is empty) by returning it's name or it returns an empty string if no file exists. It can return a list of file names by using an empty string parameter("") after sending a wildcard spec to get the first file name. The number of file names found is returned by using the SHARED variable, DIRCount%. Unlike the PDS DIR$ function, it MUST use an empty string parameter until QB64 supports optional parameters! The function does NOT delete empty files.

See also:

Keyword Reference - Alphabetical
Keyword Reference - By Usage
Main Wiki Page