AllExperts > Qbasic, Quickbasic 
Search      
Qbasic, Quickbasic
Volunteer
Answers to thousands of questions
 Home · More Qbasic, Quickbasic Questions · Answer Library  · Encyclopedia ·
More Qbasic, Quickbasic Answers
Question Library

Ask a question about Qbasic, Quickbasic
Volunteer
Experts of the Month
Expert Login

Awards

About Us
Tell friends
Link to Us
Disclaimer

 
 
 
 
About Alex Barry
Expertise
I have been a qbasic programmer since 2000, creating games, minor libraries and various small programs. I have experience using interrupts, graphics, file input/output, the mouse cursor, and using libraries. I have also learned FreeBASIC, c/c++, python, php and html.
I do not claim to be an absolute authority in any language, but I don't mind looking things up and learning with you.

Experience
Hobby programming since 2000

I no longer belong to any community programming groups, but do have knowledge of *basic dialects and C/C++

 
   

You are here:  Experts > Computing/Technology > Basic > Qbasic, Quickbasic > Number of files in a folder.

Qbasic, Quickbasic - Number of files in a folder.


Expert: Alex Barry - 7/3/2009

Question
I haven't programmed in Quick Basic in years, but dusted it off again for a quick fix I need for a problem. This requires me to be able to count the number of files in a given folder, but I have no idea how to do this. Any help would be appreciated!

Answer
Hi, Gerjan,

The simplest way to do this is to use DOS inside QBasic.  Here is how I would do it:

DECLARE FUNCTION GetFileCount%( path AS STRING, directories AS INTEGER, output AS INTEGER )

' Example of just get the number of files
DIM numFiles AS INTEGER

numFiles = GetFileCount%( "x:\your\directory", 0, 0 )

' Example of number of files and directories
DIM numFiles AS INTEGER
DIM numDirectories AS INTEGER

numFiles = GetFileCount%( "x:\your\directory", numDirectories, 0 )

' Example that also prints out what files and directories are found:
DIM dummy AS INTEGER

dummy = GetFileCount%( "x:\your\directory", 0, 1 )

FUNCTION GetFileCount%( path AS STRING, directories AS INTEGER, output AS INTEGER )
 DIM fhnd AS INTEGER
 DIM nfiles AS INTEGER
 DIM ndirs AS INTEGER
 CHDIR( path )
 SHELL( "dir /b /a:-D > ../__theFileList" )
 SHELL( "dir /b /a:-D > ../__theDirectoryList" )
 fhnd = FREEFILE
 IF output <> 0 THEN PRINT "Statistics for " + path

 ' Get the file list
 OPEN "../__theFileList" FOR INPUT AS #fhnd
 nfiles = 0
 DO UNTIL EOF( fhnd )
   LINE INPUT #fhnd, fname$
   IF output <> 0 then
     PRINT "", fname$
   END IF
   nfiles = nfiles + 1
 LOOP
 FUNCTION = nfiles
 CLOSE #fhnd

 ' Get the directory list (you can remove this if you don't want it)
 OPEN "../__theDirectoryList" FOR INPUT AS #fhnd
 nfiles = 0
 DO UNTIL EOF( fhnd )
   LINE INPUT #fhnd, dname$
   IF output <> 0 then
     PRINT "", dname$
   END IF
   ndirs = ndirs + 1
 LOOP
 directories = ndirs
 CLOSE #fhnd

 IF output <> 0 THEN
   PRINT nfiles; " file(s) and "; ndirs; " directory(s)"
 END IF

 KILL "../__theFileList"
 KILL "../__theDirectoryList"
END FUNCTION


I hope that helps,
-Alex

Add to this Answer   Ask a Question


 
User Agreement | Privacy Policy | Kids' Privacy Policy | Help
Copyright  © 2008 About, Inc. AllExperts, AllExperts.com, and About.com are registered trademarks of About, Inc. All rights reserved.