FILES: Difference between revisions

From QB64 Phoenix Edition Wiki
Jump to navigation Jump to search
(Created page with "The FILES statement is used to print a list of files in the current directory using a file specification. {{PageSyntax}} : FILES [{{Parameter|fileSpec$}}] {{PageDescription}} * {{Parameter|fileSpec$}} is a string expression or variable containing a path when required. * {{Parameter|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)...")
 
No edit summary
Line 24: Line 24:
{{CodeStart}}{{Cl|FILES}} "*.BAS"
{{CodeStart}}{{Cl|FILES}} "*.BAS"
{{CodeEnd}}
{{CodeEnd}}
<!-- broken link: <center>'''[http://i301.photobucket.com/albums/nn53/burger2227/FILESss.jpg Screenshot shows only the end of a long list of files]'''</center> -->


<!-- function obsoleted by _FILEEXISTS; function doesn't use the FILES statement and is not relevant in this context; may be moved to an exclusive page if desired;
{{Parameter|Example 2:'' A function that verifies that a file exists if it is not empty. Note: This function will delete empty files.
{{CodeStart}} '' ''
{{Cl|INPUT}} "Enter a file name: ", file$
{{Cl|IF}} Exist%(file$) {{Cl|THEN}} {{Cl|OPEN}} file$ {{Cl|FOR (file statement)|FOR}} {{Cl|INPUT (file mode)|INPUT}} {{Cl|AS}} #1: found% = -1  'function call demo
{{Cl|CLOSE}} #1
{{Cl|IF}} found% THEN {{Cl|PRINT}} "File exists!" {{Cl|ELSE}} {{Cl|PRINT}} "File not found!"
{{Cl|END}}
{{Cl|FUNCTION}} Exist% (filename$)
f% = {{Cl|FREEFILE}}
{{Cl|OPEN}} filename$ {{Cl|FOR (file statement)|FOR}} {{Cl|APPEND}} {{Cl|AS}} #f%
{{Cl|IF}} {{Cl|LOF}}(f%) {{Cl|THEN}} Exist% = -1 {{Cl|ELSE}} Exist% = 0: {{Cl|CLOSE}} #f%: {{Cl|KILL}} filename$ 'delete empty files
{{Cl|CLOSE}} #f%
{{Cl|END FUNCTION}} '' ''
{{CodeEnd}}{{small|Code by Ted Weissgerber}}}}
-->


==Alternative file list solutions==
==Alternative file list solutions==
Line 97: Line 77:
* 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. <!-- broken link: [http://i301.photobucket.com/albums/nn53/burger2227/FILE-ss2.jpg FILELIST$ function screenshot] -->
* 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. <!-- broken link: [http://i301.photobucket.com/albums/nn53/burger2227/FILE-ss2.jpg FILELIST$ function screenshot] -->


<!-- The referenced library is not present in this link anymore ''See Library:'' File Exist C++ Function that does not create a temp file. [http://qb64.net/wiki/index.php?title=C_Libraries#File_Exist FileExist Function] -->


{{PageSeeAlso}}
{{PageSeeAlso}}

Revision as of 15:51, 17 July 2022

The FILES statement is used to print a list of files in the current directory using a file specification.


Syntax

FILES [fileSpec$]


Description

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


QBasic/QuickBASIC

  • Illegal filename characters in QBasic included * ? , > < ; : " | \ / + [ ] and more than one dot extension in DOS.


Examples

Example 1: Finding a list of all BAS files in the current folder.

FILES "*.BAS"


Alternative file list solutions

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.

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


Alternative 2:

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


See also



Navigation:
Main Page with Articles and Tutorials
Keyword Reference - Alphabetical
Keyword Reference - By usage
Report a broken link