AboutAlex 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++
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
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