FILES - mkilgore/QB64pe GitHub Wiki
The FILES statement is used to print a list of files in the current directory using a file specification.
- FILES [fileSpec$]
- fileSpec$ is a string expression or variable containing a path when required.
-
fileSpec$ can use the * and ? wildcard specifications:
- * denotes one or more wildcard characters in a filename or path specification as any legal file name character(s).
- ? denotes one wildcard letter in a filename or path specification as any legal filename character.
- If fileSpec$ is omitted, it is assumed to be "*.*" (all files and folders in the current directory).
- Illegal filename characters in QB64 include * > < : " | \ / with any amount of dot extensions being allowed in Windows.
- FILES lists can make the screen roll up. Try using SHELL "DIR" with the /P option. DIR command.
- Illegal filename characters in QBasic included * ? , > < ; : " | \ / + [] and more than one dot extension in DOS.
Example 1: Finding a list of all BAS files in the current folder.
FILES "*.BAS" |
Alternative 1: The DIR$ function adapted from PDS (7.1) returns a filename or a list when more than one exist. The file spec can use a path and/or wildcards.
END FUNCTION DIR$ (spec$) CONST TmpFile$ = "DIR$INF0.INF", ListMAX% = 500 'change maximum to suit your needs SHARED DIRCount% 'returns file count if desired STATIC Ready%, Index%, DirList$() IF...THEN NOT Ready% THEN REDIM DirList$(ListMAX%): Ready% = -1 'DIM array first use IF...THEN 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...NEXT APPEND AS #ff% size& = LOF(ff%) CLOSE #ff% IF...THEN size& = 0 THEN KILL TmpFile$: EXIT FUNCTION OPEN TmpFile$ FOR (file statement) INPUT (file mode) AS #ff% DO...LOOP WHILE NOT EOF(ff%) AND (boolean) Index% < ListMAX% Index% = Index% + 1 LINE INPUT (file statement) #ff%, DirList$(Index%) LOOP DIRCount% = Index% 'SHARED variable can return the file count CLOSE #ff% KILL TmpFile$ ELSE IF...THEN Index% > 0 THEN Index% = Index% - 1 'no spec sends next file name END IF DIR$ = DirList$(Index%) END FUNCTION '' '' |
- Explanation: The function will verify that a file exists (even if it is empty) by returning its 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 as QB64 doesn't support optional parameters. The function does not delete empty files.
- The member-contributed FILELIST$ function uses the mouse and does not affect your program screens. It can verify that a file name exists or display a list of long and short file names to choose from. It also avoids program errors when a file name does not exist.
- SHELL, SCREEN (function) (See Example 3)
- CHDIR, MKDIR
- RMDIR, KILL
- _CWD$, _STARTDIR$
- _FILEEXISTS, _DIREXISTS
- Windows File Exist Library
- Windows Open and Save Dialog Boxes
- $CONSOLE
Navigation:
Go to Keyword Reference - Alphabetical
Go to Keyword Reference - By usage
Go to Main WIKI Page