Qbasicnews.com
December 12, 2019, 02:19:12 AM *
Welcome, Guest. Please login or register.

Login with username, password and session length
News: Back to Qbasicnews.com | QB Online Help | FAQ | Chat | All Basic Code | QB Knowledge Base
 
   Home   Help Search Login Register  
Pages: [1]
  Print  
Author Topic: help  (Read 6217 times)
LPG
Member
*
Posts: 61

semper ubi sub ubi (always wear under wear)


« on: July 04, 2008, 05:12:43 AM »

Is there a QB command that allows you to see what directories / files are in a folder?
Logged

WHILE RPG$ <> "complete" : make up silly excuses :WEND
Opresion
Member
*
Posts: 82


« Reply #1 on: July 04, 2008, 07:47:05 AM »

Well, the PDS has the DIR$ command but just it's for files.
http://www.qbasicnews.com/abc/showpacket.php?packet=DOS.ABC
Logged

MY QBASIC'S PAGE                                                  I ONLY USE WINDOWS 98SE
roy
Member
*
Posts: 33


« Reply #2 on: July 04, 2008, 12:45:37 PM »

There is SHELL but you would need to make a program.



« Last Edit: July 04, 2008, 01:52:12 PM by roy » Logged
wildcard
*.*
Administrator
__/--\__
*****
Posts: 2369



« Reply #3 on: July 04, 2008, 02:44:19 PM »

Someone made a DIR$ function for QB45 using Regex library, not sure where to get a copy but hopefully Dav might know.
Logged
Ralph
Ancient Guru
****
Posts: 544


« Reply #4 on: July 04, 2008, 05:59:26 PM »

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

Ralph, using QuickBASIC 4.5 and Windows XP Home Edition and Service Pack 2, with HP LaserJet 4L printer.
Dav
Administrator
Ancient Guru
*****
Posts: 709



WWW
« Reply #5 on: July 07, 2008, 01:03:00 PM »

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:DX 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:DX 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
« Last Edit: July 07, 2008, 01:08:55 PM by Dav » Logged

Moneo
Na_th_an
*****
Posts: 1971


« Reply #6 on: July 08, 2008, 04:11:21 PM »

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
Logged
Moneo
Na_th_an
*****
Posts: 1971


« Reply #7 on: July 08, 2008, 09:09:14 PM »

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

Logged
Ralph
Ancient Guru
****
Posts: 544


« Reply #8 on: August 07, 2008, 12:21:11 PM »

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.

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

Ralph, using QuickBASIC 4.5 and Windows XP Home Edition and Service Pack 2, with HP LaserJet 4L printer.
Clippy
Member
*
Posts: 84



« Reply #9 on: August 08, 2008, 01:26:19 PM »

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
« Last Edit: August 08, 2008, 01:36:47 PM by Clippy » Logged

Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.21 | SMF © 2015, Simple Machines Valid XHTML 1.0! Valid CSS!