QBasic Directory Listings

QBasic Directory Listings

Introduction

This page was written because of some postings I made to the 250Free web hosting forum, A better way to submit to Google? and Looking for something

Getting a directory listing from QBasic isn't an easy task, the reason being that there are a couple of problems with the tools available in QBasic v1.1

QBasic has an inbuilt command, FILES, which can give a directory listing but it outputs it to the screen which unless you you use a screen reading function means it can't be used by the program. Also, FILES outputs the old DOS 8.3 naming convention and so can't show long file names. Here's a simple program to demonstrate FILES...

'files.bas      Ray Thomas      6th April 2006

'A program to demonstrate the output of the FILES function

CLS
FILES "c:\*.*"
END

And here's the output of that program...

Output of files.bas

The SHELL command "Suspends execution of a Basic program to run a DOS command or batch file" - quote from the QBasic help file. Unfortunately, if you run the DIR command from within a QBasic shell the result isn't the same as if you run DIR from a command line prompt. Again, the problem is that the output is the old DOS 8.3 filename format.

'dirshell.bas      Ray Thomas      6th April 2006

'A program to demonstrate the output of the SHELL function and DIR command

CLS
SHELL "dir c:\*.*"
END

The output of which is...

There is a solution to getting and using a directory listing using long filenames in Qbasic and it involves writing a batch file and a temporary text file to hold the directory listing from a command line DIR command.

To create a file containing a directory listing from a command prompt you can use...

dir c:\ > c:\dirlist.txt

The contents of the dirlist.txt created is...

 Volume in drive C is System
 Volume Serial Number is FC07-97B0

 Directory of c:\

02/27/2006  10:25 PM    <DIR>          audiograbber
11/13/2005  06:40 PM                 0 AUTOEXEC.BAT
11/16/2005  12:17 AM        12,284,879 AVG7QT.DAT
11/13/2005  06:40 PM                 0 CONFIG.SYS
04/06/2006  09:01 AM                 0 dirlist.txt
02/14/2006  06:06 PM    <DIR>          Documents and Settings
11/16/2005  01:10 PM    <DIR>          My Installations
03/16/2006  10:12 PM    <DIR>          Program Files
04/02/2006  07:43 PM            53,011 wialog.txt
03/19/2006  02:25 PM    <DIR>          WINDOWS
               5 File(s)     12,337,890 bytes
               5 Dir(s)  14,752,882,688 bytes free

There are two ways of creating the text file directory listing. The line dir c:\ > c:\dirlist.txt can either be put in a batch file on its own and run within a QBasic program by using the SHELL command or the line can be put into a batch file along with the code to run the QBasic program itself.

Calling a batch file from within a QBasic program

The batch file, in this case I've called it dodir1.bat, simply contains the line dir c:\ > c:\dirlist.txt

The QBasic program used to run it is...

'getdir1.bas      Ray Thomas      6th April 2006

'A program to demonstrate running a batch file using SHELL

SHELL "k:\qbasic\dodir.bat"
END

The problem with this approach is that QBasic always comes up with the "Press any key to continue" prompt so I prefer the method where the batch file also runs the QBasic program.

Running a QBasic program from a Batch File

The batch file, dodir2.bat contains the lines...

dir c:\ > c:\dirlist.txt
k:\qbasic\qbasic /run getdir2.bas

and the QBasic program getdir2.bas is simply...

'getdir2.bas      Ray Thomas      6th April 2006

'A program to demonstrate running a QBasic program from a batch file

PRINT
PRINT "Hello, it works!"
PRINT
PRINT "Press a key to exit"
DO
LOOP UNTIL INKEY$ <> ""
SYSTEM

Output of the DIR command

The output produced by the DIR command, whether to the screen or redirected to a file, will vary with the version of DIR installed and the switches chosen. DIR is an "internal" command, that is it is actually embedded in command.com, and so cannot be installed independantly.

Using the command

dir "k:\website\free hosts\" > c:\dir.txt

The contents of the dir.txt created is...

 Volume in drive K is Working
 Volume Serial Number is 406F-5707

 Directory of k:\website\free hosts

03/07/2006  12:44 PM    <DIR>          .
03/07/2006  12:44 PM    <DIR>          ..
03/07/1999  06:08 AM             3,135 back1.jpg
05/17/1999  09:39 AM             3,020 biog.gif
07/15/2004  10:51 PM            10,977 biog.htm
12/02/2000  03:17 AM             6,330 bravenet.gif
11/07/2005  02:30 AM    <DIR>          briscan
02/19/2006  07:28 PM    <DIR>          bristol
11/07/2005  02:30 AM    <DIR>          brisusa
05/28/2005  03:11 PM             2,753 bsign2e.jpg
06/22/2003  04:33 PM             5,089 capcam1a.jpg
.....
.....
06/08/1999  02:10 PM            16,813 wray1.jpg
10/18/2001  11:19 AM               706 wrlogo.gif
11/07/2005  02:30 AM    <DIR>          wstory
10/09/1999  06:01 PM             9,874 wwcolnk.gif
08/06/2000  08:51 AM             1,442 yahoowr.gif
             108 File(s)      2,890,869 bytes
              22 Dir(s)  10,557,845,504 bytes free

Using the command

dir /b "k:\website\free hosts\" > c:\dir.txt

The contents of the dir.txt created is...

back1.jpg
biog.gif
biog.htm
bravenet.gif
briscan
bristol
brisusa
bsign2e.jpg
capcam1a.jpg
.....
.....
wray1.jpg
wrlogo.gif
wstory
wwcolnk.gif
yahoowr.gif

Using the command

dir /b/s "k:\website\free hosts\" > c:\dir.txt

The contents of the dir.txt created is...

k:\website\free hosts\back1.jpg
k:\website\free hosts\biog.gif
k:\website\free hosts\biog.htm
k:\website\free hosts\bravenet.gif
k:\website\free hosts\briscan
k:\website\free hosts\bristol
k:\website\free hosts\brisusa
k:\website\free hosts\bsign2e.jpg
k:\website\free hosts\capcam1a.jpg
.....
.....
k:\website\free hosts\wstory\wrec2.gif
k:\website\free hosts\wstory\wrec2.htm
k:\website\free hosts\wstory\wuxb.gif
k:\website\free hosts\wstory\wuxb.htm
k:\website\free hosts\wstory\wveh.gif
k:\website\free hosts\wstory\wveh.htm

Using the command

dir /s "k:\website\free hosts\" > c:\dir.txt

The contents of the dir.txt created is...

 Volume in drive K is Working
 Volume Serial Number is 406F-5707

 Directory of k:\website\free hosts

03/07/2006  12:44 PM    <DIR>         .
03/07/2006  12:44 PM    <DIR>          ..
03/07/1999  06:08 AM             3,135 back1.jpg
05/17/1999  09:39 AM             3,020 biog.gif
07/15/2004  10:51 PM            10,977 biog.htm
12/02/2000  03:17 AM             6,330 bravenet.gif
11/07/2005  02:30 AM    <DIR>          briscan
02/19/2006  07:28 PM    <DIR>          bristol
11/07/2005  02:30 AM    <DIR>          brisusa
05/28/2005  03:11 PM             2,753 bsign2e.jpg
06/22/2003  04:33 PM             5,089 capcam1a.jpg
.....
.....
07/19/2002  03:39 PM            14,218 wrec1.htm
05/17/1999  09:48 AM             2,752 wrec2.gif
07/19/2002  03:40 PM            14,834 wrec2.htm
05/17/1999  09:43 AM             2,918 wuxb.gif
07/19/2002  03:40 PM            11,697 wuxb.htm
05/17/1999  09:43 AM             2,914 wveh.gif
06/25/2003  02:05 PM            15,963 wveh.htm
              54 File(s)        498,464 bytes

     Total Files Listed:
            3104 File(s)     72,209,927 bytes
              71 Dir(s)  10,557,845,504 bytes free

The ways of displaying this information are almost endless, there are 14 switches and a further 23 sub-switches that can used with the DIR command. It's up to the programmer to ensure that the QBasic program correctly reads the directory text file.

Notes

Once the text file containing the directory listing has been created then QBasic can access it using normal file access techniques.

Once the text file containing the directory listing has been processed by the QBasic program it can be deleted from inside QBasic by using the KILL command or it can be deleted from within the batch file using the DEL command.

Using the batch file technique all the normal DIR command switches can be used. You see what these are open a command prompt and type help dir or dir /?

The directory text file will have different formats depending on which switches were used with the DIR command. The QBasic program has to be tested with the switches you have used.

In the following programs you'll see that it's important when using directories in QBasic that you use the 8.3 naming convention within them.

Example 1 - Dirtxt

One of the ways you can submit your site to Google Sitemaps is to create a text file containing nothing but the names of the various pages of your site. Provided you keep an exact copy of your site on your own computer this is easy to create. This program was originally written for 250Free forum members.

The batch file dirtxt.bat contains the code to create a text file containing a directory listing, run a QBasic program to process that file then delete it

@echo off
echo.
echo *** Creating directory listing...
dir "k:\website\free hosts\*.htm*" /b/s > k:\website\dir.txt
echo *** Starting QBasic program...
k:\qbasic\qbasic /run dirtxt.bas
echo *** Deleting directory file...
del k:\website\dir.txt
echo *** Finished

The QBasic program dirtxt.bas processes the text file containing the directory listing and creates a new text file from it

'DIRTXT.BAS    Ray Thomas      20th March 2006

'Program to read the files listed in dir.txt and (gained by
'DIR "k:\website\free host\*.htm*" /s/b > d:\website\dir.txt) and to
'create a text file named sitemap.txt which lists the files found in that
'file. The file created is suitable for use with Google Sitemaps -
'http://google.com/webmasters/sitemaps. Also see my post at
'http://www.250free.com/help/forum/viewtopic.php?t=2958

'*** User changeable variables ***

DIM dirFile AS STRING   'The directory file - this should be the same as that
                        'specified in dirtxt.bat
DIM txtOut AS STRING    'The text output file - this includes the path
DIM ignore AS STRING    'The part of the directory path to replace with
                        'either nothing ie httpPath = "" or whatever is in
                        'httpPath
DIM httpPath AS STRING  'If the absolute path is to be shown in the HTML
                        'directory then use this
                        'for example http://brisray.com
                        'If a listing of just the directories and files is
                        'needed then leave this as ""
'The paths and file names must be written in the old DOS 8.3 filename format
'ie the directory "free hosts" has to be written as freeho~1

dirFile$ = "k:\website\dir.txt"
txtOut$ = "k:\website\freeho~1\sitemap.txt"
ignore$ = "k:\website\free hosts\"
httpPath$ = "http://brisray.com/"

'*** Program variables - do not change unless you know what you are doing ***

DIM inFile AS STRING    'Holds the lines from the directory file
DIM posn AS INTEGER     'Place holder for instr command

OPEN dirFile$ FOR INPUT ACCESS READ LOCK READ WRITE AS #1
OPEN txtOut$ FOR OUTPUT ACCESS WRITE LOCK READ WRITE AS #2

DO
	LINE INPUT #1, inFile$

    '*** Replaces ignore$ with httpPath$

    IF INSTR(inFile$, ignore$) > 0 THEN
		inFile$ = MID$(inFile$, LEN(ignore$) + 1)
        inFile$ = httpPath$ + inFile$
    END IF

	'*** Checks for and replaces \ with /

    posn = 1
    DO UNTIL INSTR(posn, inFile$, "\") < 1
		posn = INSTR(1, inFile$, "\")
        MID$(inFile$, posn, 1) = "/"
        posn = posn + 1
    LOOP
        PRINT #2, inFile$
LOOP UNTIL EOF(1)

CLOSE
SYSTEM

As you can see, from the lines dir "k:\website\free hosts\*.htm*" /b/s > k:\website\dir.txt in the batch file and the line txtOut$ = "k:\website\freeho~1\sitemap.txt" in the QBasic program that when using QBasic it's important that you use the 8.3 DOS naming convention.

Example 2 - DirHTML

Following on from the previous example, mystickcal on the 250Free forums wanted a program to produce a HTML file that contained a list of links to all their HTML files. Again, this program will work if you keep a copy of your site on your own computer.

The batch file dirHTML.bat contains the code to create a text file containing a directory listing, run a QBasic program to process that file then delete it

@echo off
echo.
echo *** Creating directory listing...
dir "k:\website\free hosts\*.htm*" /b/s > k:\website\dir.txt
echo *** Starting QBasic program...
k:\qbasic\qbasic /run dirhtml.bas
echo *** Deleting directory file...
del k:\website\dir.txt
echo *** Finished

The QBasic program dirHTML.bas processes the text file containing the directory listing and creates a new HTML file from it

'DIRHTML.BAS    Ray Thomas      16th February 2006

'Program to read the files listed in dir.txt and (gained by
'DIR "k:\website\free host\*.htm*" /s/b > d:\website\dir.txt) and to
'create a HTML file named files.htm which lists the files found in that file.

'*** User changeable variables ***

DIM dirFile AS STRING   'The directory file - this should be the same as that
                        'specified in htmldir.bat
DIM htmlOut AS STRING   'The HTML output file - this includes the path
DIM ignore AS STRING    'The part of the directory path to replace with
                        'either nothing ie httpPath = "" or whatever is in
                        'httpPath
DIM httpPath AS STRING  'If the absolute path is to be shown in the HTML
                        'directory then use this
                        'for example http://brisray.250free.com
                        'If a listing of just the directories and files is
                        'needed then leave this as ""

dirFile$ = "k:\website\dir.txt"
htmlOut$ = "k:\website\dir.htm"
ignore$ = "k:\website\free hosts\"
httpPath$ = "http://brisray.250free.com/"

'*** Program variables - do not change unless you know what you are doing ***

DIM inFile AS STRING    'Holds the lines from the directory file
DIM posn AS INTEGER     'Place holder for instr command

OPEN dirFile$ FOR INPUT ACCESS READ LOCK READ WRITE AS #1
OPEN htmlOut$ FOR OUTPUT ACCESS WRITE LOCK READ WRITE AS #2

'*** This part writes the HTML header

PRINT #2, "<!DOCTYPE HTML PUBLIC " + CHR$(34) + "-//W3C//DTD HTML 4.0 Transitional//EN" + CHR$(34); ""
PRINT #2, CHR$(34) + "http://www.w3.org/TR/REC-html40/loose.dtd" + CHR$(34)>
PRINT #2, "<HTML>"
PRINT #2, "<HEAD>"
PRINT #2, "<meta http-equiv=" + CHR$(34) + "content-Type" + CHR$(34) + "- content=" + CHR$(34) + "text/html charset=windows-1252" + CHR$(34) + ">"
PRINT #2, "<TITLE>HTML Directory</TITLE>"
PRINT #2, "</HEAD>"
PRINT #2, ""
PRINT #2, "<BODY>"
PRINT #2, ""
PRINT #2, "<p>"

'*** This part processes the directory listing and writes the HTML links

DO
        LINE INPUT #1, inFile$

        '*** Replaces ignore$ with httpPath$

        IF INSTR(inFile$, ignore$) > 0 THEN
                inFile$ = MID$(inFile$, LEN(ignore$) + 1)
                inFile$ = httpPath$ + inFile$
        END IF

        '*** Checks for and replaces \ with /

        posn = 1
        DO UNTIL INSTR(posn, inFile$, "\") < 1
                posn = INSTR(1, inFile$, "\")
                MID$(inFile$, posn, 1) = "/"
                posn = posn + 1
        LOOP
        PRINT #2, "<a href=" + CHR$(34) + inFile$ + CHR$(34) + ">" + inFile$ + "</a><br>"
LOOP UNTIL EOF(1)

'*** This part writes the HTML footer

PRINT #2, "</p>"
PRINT #2, ""
PRINT #2, "</BODY>"
PRINT #2, "</HTML>"

CLOSE
SYSTEM

The programs on this page, like all the programs written for this site, can be downloaded from the Downloads page.

This page created 5th April 2006, last modified 7th April 2006


GoStats stats counter