Posts: 61
Threads: 11
Joined: Apr 2008
Is there a QB command that allows you to see what directories / files are in a folder?
WHILE RPG$ <> "complete" : make up silly excuses :WEND
Posts: 82
Threads: 8
Joined: Oct 2006
Well, the PDS has the DIR$ command but just it's for files.
http://www.qbasicnews.com/abc/showpacket...et=DOS.ABC
MY QBASIC'S PAGEÂ Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â I ONLY USE WINDOWS 98SE
Posts: 33
Threads: 2
Joined: Aug 2007
07-04-2008, 08:45 PM
(This post was last modified: 07-04-2008, 09:52 PM by roy.)
There is SHELL but you would need to make a program.
Posts: 1,138
Threads: 211
Joined: Feb 2020
Someone made a DIR$ function for QB45 using Regex library, not sure where to get a copy but hopefully Dav might know.
Posts: 544
Threads: 27
Joined: Jan 2005
To only "see" the files, you can use the SHELL statement Roy mentioned. For instance, to see the directory of my QB files, which are in the same directory as my QB.exe files, I can use
SHELL "DIR/P"
The SHELL statement let's me use DOS commands, such as DIR, including its switches, such as /P, which let's one scroll the directory, one page at a time.
Notice that the syntax is
SHELL string
so the string part has to be either an actual string, such as "DIR/P", or a string variable, such as direct$ = "DIR/P"; in the case of a string variable, use
SHELL direct$
To see the directory of My Documents, use
SHELL "c:\MyDocu~1/p"
in accordance with the DOS method of seeing filenames with more than 8 characters.
Look in the Help, Syntax, SHELL statement for more information on this.
Ralph, using QuickBASIC 4.5 and Windows XP Home Edition and Service Pack 2, with HP LaserJet 4L printer.
Posts: 440
Threads: 65
Joined: Sep 2001
07-07-2008, 09:03 PM
(This post was last modified: 07-07-2008, 09:08 PM by Dav.)
QB doesn't have a built-in way, other than using FILES. You can see this QBKB doc on advanced ways for using FILES:
http://kb.qbasicnews.com/Q32724.TXT
Like Opresion mentioned, there are a few DIR$ like routines in the ABC archives -- look in the DOS section for them.
I'll post one I've found in there, made by Dave Cleary:
Quote:'DIR.BAS by Dave Cleary
'
'One of the most useful additions to BASIC 7 PDS is the DIR$ function.
'This function allows you to read a directory of filenames. It also
'allows you to check the existence of a file by doing the following:
'
'Â IF LEN(DIR$("COMMAND.COM")) THEN
'Â Â Â PRINT "File Found"
'Â ELSE
'Â Â Â PRINT "File not found"
'Â END IF
'
'Now QuickBASIC 4.X users can have this useful function for their
'programs.
'
'Calling DIR$ with a FileSpec$ returns the the name of the FIRST
'matching file name. Subsequent calls with a null FileSpec$ return the
'NEXT matching file name. If a null string is returned, then no more
'matching files were found. FileSpec$ can contain both a drive and a
'path plus DOS wildcards. Special care should be taken when using
'this on floppy drives because there is no check to see if the drive
'is ready.
DEFINT A-Z
DECLARE FUNCTION DIR$ (FileSpec$)
'$INCLUDE: 'QB.BI'
'-----Â Some constants that DIR$ uses
CONST DOS = &H21
CONST SetDTA = &H1A00, FindFirst = &H4E00, FindNext = &H4F00
'--------------------------------------------------------------------
'This shows how to call DIR$ to find all matching files
CLS
FileSpec$ = "C:\QB\*.*"
Found$ = DIR$(FileSpec$)
DO WHILE LEN(Found$)
  PRINT Found$
  Found$ = DIR$("")
LOOP
'--------------------------------------------------------------------
FUNCTION DIR$ (FileSpec$) STATIC
  DIM DTA AS STRING * 44, Regs AS RegTypeX
  Null$ = CHR$(0)
'-----Â Set up our own DTA so we don't destroy COMMAND$
  Regs.AX = SetDTA          'Set DTA function
  Regs.DX = VARPTR(DTA)        'DS X points to our DTA
  Regs.DS = -1            'Use current value for DS
  InterruptX DOS, Regs, Regs     'Do the interrupt
'-----Â Check to see if this is First or Next
  IF LEN(FileSpec$) THEN       'FileSpec$ isn't null, so
  'FindFirst
FileSpecZ$ = FileSpec$ + Null$Â Â 'Make FileSpec$ into an ASCIIZ
  'string
Regs.AX = FindFirst       'Perform a FindFirst
Regs.CX = 0Â Â Â Â Â Â Â Â Â Â Â 'Only look for normal files
Regs.DX = SADD(FileSpecZ$)Â Â Â Â 'DS X points to ASCIIZ file
Regs.DS = -1Â Â Â Â Â Â Â Â Â Â Â 'Use current DS
  ELSE                'We have a null FileSpec$,
Regs.AX = FindNext        'so FindNext
  END IF
  InterruptX DOS, Regs, Regs     'Do the interrupt
'-----Â Return file name or null
  IF Regs.Flags AND 1 THEN      'No files found
DIR$ = ""Â Â Â Â Â Â Â Â Â Â Â Â 'Return null string
  ELSE
Null = INSTR(31, DTA, Null$)Â Â Â 'Get the filename found
DIR$ = MID$(DTA, 31, Null - 30)Â 'It's an ASCIIZ string starting
  END IF               'at offset 30 of the DTA
END FUNCTION
- Dav
Posts: 1,956
Threads: 65
Joined: Jun 2003
(07-04-2008, 01:12 PM)LPG link Wrote:Is there a QB command that allows you to see what directories / files are in a folder? Others have suggested using a SHELL with a DIR to see the directories/files. However, this will just allow you to scroll through the list onc from top to bottom. The following example will allow you to browse the list up and down.
folder$ = full path and name of subject forder
SHELL "cmd/c DIR "+folder$+">thelist
' The above puts the contents, directories/files, into a work file called thelist.
SHELL "cmd/c EDIT thelist"
'Using EDIT lets you be able to scroll through thelist file.
'When you're done looking, exit the EDIT.
'Note: the cmd/c in the SHELL is to insure compatibility.
Regards..... Moneo
Posts: 1,956
Threads: 65
Joined: Jun 2003
About EDIT, I forgot to mention that the program EDIT.COM has to be in the path. Normally, EDIT.COM is found in the \windows\system32 directory.
Regards..... Moneo
Posts: 544
Threads: 27
Joined: Jan 2005
Moneo:
I feel that you do not read all posts. If you will take the time to read my post, which I have qoted here below, you will see that I showed and explained the use of the /p switch for the dir command. And, to search for a particular file, say qb.exe, one can easily use
SHELL "dir qb.exe"Â or, evenÂ
SHELL "dir c:\qb\qb.exe"
And if the file does not exist, you will get a "File not found" message. Oh, yes, no need to make this into a program! I just use,
CLS:SHELL "dir qb.exe" on the immediate line and press Enter. Presto, I get my answer, and can easily go back to my normal coding area.
(07-05-2008, 01:59 AM)Ralph link Wrote:To only "see" the files, you can use the SHELL statement Roy mentioned. For instance, to see the directory of my QB files, which are in the same directory as my QB.exe files, I can use
SHELL "DIR/P"
The SHELL statement let's me use DOS commands, such as DIR, including its switches, such as /P, which let's one scroll the directory, one page at a time.
Notice that the syntax is
SHELL string
so the string part has to be either an actual string, such as "DIR/P", or a string variable, such as direct$ = "DIR/P"; in the case of a string variable, use
SHELL direct$
To see the directory of My Documents, use
SHELL "c:\MyDocu~1/p"
in accordance with the DOS method of seeing filenames with more than 8 characters.
Look in the Help, Syntax, SHELL statement for more information on this.
Ralph, using QuickBASIC 4.5 and Windows XP Home Edition and Service Pack 2, with HP LaserJet 4L printer.
Posts: 82
Threads: 8
Joined: Jul 2008
08-08-2008, 09:26 PM
(This post was last modified: 08-08-2008, 09:36 PM by Clippy.)
Here is a routine to count the number of files and place them into an array:
Code: 'Â Â Ethan Winer: PC Magazine's BASIC Techniques and Utilities Book - 353 -
'Â Â Â Â Â Â Â Function FileCount% returns number of files meeting Spec
'$INCLUDE: 'REGTYPE.BI'
DEFINT A-Z
DECLARE FUNCTION FileCount% (Spec$, Attribute%)
DECLARE SUB LoadNames (FileSpec$, Array$(), Attribute%)
 Â
  TYPE DTAData 'used by find first/next
    Reserved AS STRING * 21 'reserved for use by DOS
    Attr AS STRING * 1 'the file's attribute
    Time AS INTEGER 'the file's time
    Date AS INTEGER 'the file's date
    Size AS LONG 'the file's size
    Named AS STRING * 13 'the file's name
  END TYPE
  DIM SHARED DTA AS DTAData 'shared so LoadNames can
  DIM SHARED Regs AS RegType ' access them too
  DIM Spec AS STRING * 65
  REDIM Names$(1 TO 1) 'create a dynamic array
  'Attribute = 19 'matches directories only
  Attribute = 39 'matches all files
  INPUT "Enter a file specification: ", Spec$
  CALL LoadNames(Spec$, Names$(), Attribute)
  FOR X = LEN(Spec$) TO 1 STEP -1 'isolate the drive/path
  Temp = ASC(MID$(Spec$, X, 1))
  IF Temp = 58 OR Temp = 92 THEN '":" or "\"
  Path$ = LEFT$(Spec$, X) 'keep what precedes that
  EXIT FOR 'and we're all done
  END IF
  NEXT
  FOR X = 1 TO UBOUND(Names$) 'print the names
  PRINT Path$; Names$(X)
  NEXT
  PRINT
  PRINT FileCount(Spec$, Attribute); "matching file(s)"
  'PRINT UBOUND(Names$); "matching file(s)"
  END
  'Function counts number of files meeting SpecÂ
 'Attribute = 39 'matches all files 19 'matches directories only
  FUNCTION FileCount% (Spec$, Attribute)
  STATIC Temp AS STRING * 65, Count 'make this private
  Regs.dx = VARPTR(DTA): Regs.ds = -1 'the DTA is in DGROUP
  Regs.ax = &H1A00 'specify service 1Ah
  CALL INTERRUPT(&H21, Regs, Regs) 'DOS set DTA service
  Count = 0 'clear the counter
  Temp = Spec$ + CHR$(0) 'make an ASCIIZ string
  IF Attribute AND 16 THEN DirFlag = -1 ELSE DirFlag = 0'no
  Regs.dx = VARPTR(Temp) 'SADD(Spec$) 'the file spec address
  'Regs.ds = -1 'QuickBASIC '= SSEG(Spec$) 'for BASIC PDS
  Regs.cx = Attribute: Regs.ax = &H4E00 'find first matching name
  DO
   CALL INTERRUPT(&H21, Regs, Regs) 'see if there's a match
   IF Regs.flags AND 1 THEN EXIT DO 'flag quits count
   IF DirFlag THEN         'filter out Directories
     IF ASC(DTA.Attr) AND 16 THEN
      IF LEFT$(DTA.Named, 1) <> "." THEN Count = Count + 1
     END IF
   ELSE : Count = Count + 1'they want regular files
   END IF
   Regs.ax = &H4F00 'find next name
  LOOP
  FileCount% = Count 'assign the function
  END FUNCTION
        'displays filenames and puts them into an array
SUB LoadNames (Spec$, Array$(), Attribute) STATIC
STATIC Temp AS STRING * 65
Temp = Spec$ + CHR$(0) 'make an ASCIIZ string
NumFiles = FileCount%(Spec$, Attribute) 'count names
IF NumFiles = 0 THEN EXIT SUB 'exit if none
REDIM Array$(1 TO NumFiles) 'dimension the array
IF Attribute AND 16 THEN DirFlag = -1 ELSE DirFlag = 0 'no eliminate!
 Â
'--The following code isn't strictly necessary because we know that FileCount already set the DTA address.
'Regs.DX = VARPTR(DTA): Regs.DS = -1 'the DTA in DGROUP
'Regs.AX = &H1A00 'specify service 1Ah
'CALL INTERRUPT(&H21, Regs, Regs) 'DOS set DTA service
Regs.dx = VARPTR(Temp)'SADD(Spec$) 'the file spec address
    'Regs.ds = -1 'for QuickBASIC 'Regs.DS = SSEG(Spec$) 'for BASIC PDS
Regs.cx = Attribute: Regs.ax = &H4E00: Count = 0
DO
  CALL INTERRUPT(&H21, Regs, Regs) 'see if there's a match
  IF (Regs.flags AND 1) THEN EXIT DO 'no more
  Valid = 0
  IF DirFlag THEN 'directories?
   IF ASC(DTA.Attr) AND 16 THEN
     IF LEFT$(DTA.Named, 1) <> "." THEN Valid = -1 'this name is valid
   END IF
  ELSE : Valid = -1  'they want regular files
  END IF
  IF Valid THEN 'process the file if it passed all the tests
    Count = Count + 1: Zero = INSTR(DTA.Named, CHR$(0))
    Array$(Count) = LEFT$(DTA.Named, Zero - 1)
  END IF
  Regs.ax = &H4F00 'find next matching name
LOOP
END SUBÂ Â Â Â Â
I changed the code to eliminate DEF Fn calls. In QB45 you must load the Library.
Ted
|