Difference between revisions of "DIR$"

From QB64 Wiki
Jump to navigation Jump to search
imported>Clippy
m
(Added missing ")" in example #1)
(7 intermediate revisions by 3 users not shown)
Line 1: Line 1:
The '''DIR$''' function below is '''NOT A QB64 FUNCTION!''' This page supplies equivalent QB code:
{{DISPLAYTITLE:_DIR$}}
The [[_DIR$]] function returns common paths in '''Windows''' only such as My Documents, My Pictures, My Music, Desktop.




{{PageSyntax}}
{{PageSyntax}}
::: filename$ = '''DIR$('''{filespec$|""}''')'''
: {{Parameter|d$}} = [[_DIR$]]("{{Parameter|folderspecification}}")




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.
{{PageParameters}}
{{CodeStart}} '' ''
* ''folderspecification'' may be "desktop", "download", "documents", "music", "video", "pictures", "appdata", "program data", "local data".
{{Cl|FOR...NEXT|FOR}} i = 1 {{Cl|TO}} 2
* Some variation is accepted for the folder specification:
  {{Cl|PRINT}}
:: MY DOCUMENTS, TEXT, DOCUMENT, DOCUMENTS, DOWNLOAD, DOWNLOADS
  {{Cl|LINE INPUT}} "Enter a file spec: ", spec$
:: MY MUSIC, MUSIC, AUDIO, SOUND, SOUNDS
  file$ = DIR$(spec$) 'use a file spec ONCE to find the last file name listed
:: MY PICTURES, PICTURE, PICTURES, IMAGE, IMAGES, PHOTO, PHOTOS, DCIM, CAMERA, CAMERA ROLL
  {{Cl|PRINT}} DIRCount%, file$, 'function can return the file count using {{Cl|SHARED}} variable
:: MY VIDEOS, VIDEO, VIDEOS, MOVIE, MOVIES,
  {{Cl|IF...THEN|IF}} DIRCount% > 1 {{Cl|THEN}}
:: DATA, APPDATA, APPLICATION DATA, PROGRAM DATA, LOCAL DATA, LOCALAPPDATA, LOCAL APPLICATION DATA, LOCAL PROGRAM DATA
    DO
      K$ = {{Cl|INPUT$}}(1)
      file$ = DIR$("") ''''use an empty string parameter to return a list of files!'''
      {{Cl|PRINT}} file$,
    {{Cl|LOOP}} {{Cl|UNTIL}} {{Cl|LEN}}(file$) = 0 'file list ends with an empty string
  {{Cl|END IF}}
{{Cl|NEXT}}


{{Cl|END}}


{{Cl|FUNCTION}} DIR$ (spec$)
{{PageDescription}}
{{Cl|CONST}} TmpFile$ = "DIR$INF0.INF", ListMAX% = 500 'change maximum to suit your needs
* The path returned ends with a backslash (Windows).
{{Cl|SHARED}} '''DIRCount% 'returns file count if desired. MAY CONFLICT with user's program code'''
* A nonexistent folder specification usually defaults to the Desktop folder path.
{{Cl|STATIC}} Ready%, Index%, DirList$()
* In Linux and macOS the function always returns '''"./"'''
{{Cl|IF...THEN|IF}} {{Cl|NOT}} Ready% {{Cl|THEN}} {{Cl|REDIM}} DirList$(ListMAX%): Ready% = -1 '{{Cl|DIM}} array first use
 
{{Cl|IF...THEN|IF}} spec$ > "" {{Cl|THEN}} 'get file names when a spec is given
 
  {{Cl|SHELL}} {{Cl|_HIDE}} "DIR " + spec$ + " /b > " + TmpFile$
{{PageExamples}}
  Index% = 0: DirList$(Index%) = "": ff% = {{Cl|FREEFILE}}
Example: Displaying default paths in Windows only.
  {{Cl|OPEN}} TmpFile$ {{Cl|FOR...NEXT|FOR}} {{Cl|APPEND}} {{Cl|AS}} #ff%
{{CodeStart}}{{Cl|PRINT}} "DESKTOP=" + _DIR$("desktop")
  size& = {{Cl|LOF}}(ff%)
{{Cl|PRINT}} "DOWNLOADS=" + {{Cl|_DIR$}}("download")
  {{Cl|CLOSE}} #ff%
{{Cl|PRINT}} "DOCUMENTS=" + {{Cl|_DIR$}}("my documents")
  {{Cl|IF...THEN|IF}} size& = 0 {{Cl|THEN}} {{Cl|KILL}} TmpFile$: {{Cl|EXIT FUNCTION}}
{{Cl|PRINT}} "PICTURES=" + {{Cl|_DIR$}}("pictures")
  {{Cl|OPEN}} TmpFile$ {{Cl|FOR (file statement)|FOR}} {{Cl|INPUT (file mode)|INPUT}} {{Cl|AS}} #ff%
{{Cl|PRINT}} "MUSIC=" + {{Cl|_DIR$}}("music")
  {{Cl|DO...LOOP|DO}} {{Cl|WHILE}} {{Cl|NOT}} {{Cl|EOF}}(ff%) {{Cl|AND (boolean)|AND}} Index% < ListMAX%
{{Cl|PRINT}} "VIDEO=" + {{Cl|_DIR$}}("video")
    Index% = Index% + 1
{{Cl|PRINT}} "APPLICATION DATA=" + {{Cl|_DIR$}}("data")
    {{Cl|LINE INPUT (file statement)|LINE INPUT}} #ff%, DirList$(Index%)
{{Cl|PRINT}} "LOCAL APPLICATION DATA=" + {{Cl|_DIR$}}("local application data")
  {{Cl|LOOP}}
  DIRCount% = Index% '{{Cl|SHARED}} variable can return the file count
  {{Cl|CLOSE}} #ff%
  {{Cl|KILL}} TmpFile$
{{Cl|ELSE}} {{Cl|IF...THEN|IF}} Index% > 0 {{Cl|THEN}} Index% = Index% - 1 'no spec sends next file name
{{Cl|END IF}}
DIR$ = DirList$(Index%)
{{Cl|END FUNCTION}} '' ''
{{CodeEnd}}
{{CodeEnd}}
{{small|Code by Ted Weissgerber}}
{{OutputStart}}DESKTOP=C:\Documents and Settings\Administrator\Desktop\
:''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.
DOWNLOADS=C:\Documents and Settings\Administrator\Downloads\
 
DOCUMENTS=C:\Documents and Settings\Administrator\My Documents\
PICTURES=C:\Documents and Settings\Administrator\My Documents\My Pictures\
MUSIC=C:\Documents and Settings\Administrator\My Documents\My Music\
VIDEO=C:\Documents and Settings\Administrator\My Documents\My Videos\
APPLICATION DATA=C:\Documents and Settings\Administrator\Application Data\
LOCAL APPLICATION DATA=C:\Documents and Settings\Administrator\Local Settings\Application Data\
{{OutputEnd}}


''See also:''
* [[FILES]]
* [[KILL]]
* [[SHELL]]


{{PageSeeAlso}}
* [[_CWD$]]
* [[_STARTDIR$]]


{{PageNavigation}}
{{PageNavigation}}

Revision as of 09:26, 24 July 2021

The _DIR$ function returns common paths in Windows only such as My Documents, My Pictures, My Music, Desktop.


Syntax

d$ = _DIR$("folderspecification")


Parameters

  • folderspecification may be "desktop", "download", "documents", "music", "video", "pictures", "appdata", "program data", "local data".
  • Some variation is accepted for the folder specification:
MY DOCUMENTS, TEXT, DOCUMENT, DOCUMENTS, DOWNLOAD, DOWNLOADS
MY MUSIC, MUSIC, AUDIO, SOUND, SOUNDS
MY PICTURES, PICTURE, PICTURES, IMAGE, IMAGES, PHOTO, PHOTOS, DCIM, CAMERA, CAMERA ROLL
MY VIDEOS, VIDEO, VIDEOS, MOVIE, MOVIES,
DATA, APPDATA, APPLICATION DATA, PROGRAM DATA, LOCAL DATA, LOCALAPPDATA, LOCAL APPLICATION DATA, LOCAL PROGRAM DATA


Description

  • The path returned ends with a backslash (Windows).
  • A nonexistent folder specification usually defaults to the Desktop folder path.
  • In Linux and macOS the function always returns "./"


Examples

Example: Displaying default paths in Windows only.

PRINT "DESKTOP=" + _DIR$("desktop") PRINT "DOWNLOADS=" + _DIR$("download") PRINT "DOCUMENTS=" + _DIR$("my documents") PRINT "PICTURES=" + _DIR$("pictures") PRINT "MUSIC=" + _DIR$("music") PRINT "VIDEO=" + _DIR$("video") PRINT "APPLICATION DATA=" + _DIR$("data") PRINT "LOCAL APPLICATION DATA=" + _DIR$("local application data")

DESKTOP=C:\Documents and Settings\Administrator\Desktop\ DOWNLOADS=C:\Documents and Settings\Administrator\Downloads\ DOCUMENTS=C:\Documents and Settings\Administrator\My Documents\ PICTURES=C:\Documents and Settings\Administrator\My Documents\My Pictures\ MUSIC=C:\Documents and Settings\Administrator\My Documents\My Music\ VIDEO=C:\Documents and Settings\Administrator\My Documents\My Videos\ APPLICATION DATA=C:\Documents and Settings\Administrator\Application Data\ LOCAL APPLICATION DATA=C:\Documents and Settings\Administrator\Local Settings\Application Data\


See also



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