Dec 132017
Find directory that EXE is running from. Quick Basic source code.
File EXEDIR.ZIP from The Programmer’s Corner in
Category BASIC Language
Find directory that EXE is running from. Quick Basic source code.
File Name File Size Zip Size Zip Type
PROGPATH.TXT 3907 1540 deflated
TPCREAD.ME 199 165 deflated

Download File EXEDIR.ZIP Here

Contents of the PROGPATH.TXT file

' Keith Funk (CIS 72240,2020) Feb. 17,1991
' Vancouver, BC, Canada
' Demonstrates the use of the FUNCTION GetProgPath to find the drive and
' directory where the EXE file for this program is located.
' NOTE: The program must be compiled into an EXE file to work. Also,
' the program will NOT work with DOS versions earlier than 3.x.


' ===>>> uncomment one of the next two lines. ALSO, see line 26 of the
' GetProgPath$ FUNCTION.

'' $INCLUDE: 'QBX.BI' Basic 7 PDS
'' $INCLUDE: 'QB.BI' QuickBasic

PRINT "The EXE for this program is in "; GetProgPath$

'some demo,eh.


' FUNCTION GetProgPath - Finds and returns the drive and directory path
' where the EXE file of the calling program is located. Very useful if the
' program wants to open a file in its own directory (like a config file).
' The path is in the form: C:\George\Burns\. Note that the path always ends
' with a backslash.
' To use this routine, the calling program must contain the following line
' at the BEGINNING of the program. ' $INCLUDE: 'QB.BI' to include the
' QuickBasic file QB.BI that contains the stuff needed to use the routine
' CALL INTERRUPT. The /L option must be used when starting QB to load the
' QB Quick Library.
' The routine DOES NOT WORK when the program is run from within the Quick-
' Basic environment. The program MUST be compiled to an .EXE file. See below
' how to run the program within the QB development environment.
' Keith Funk (CIS 72240,2020) Feb. 17, 1991
' Vancouver, BC, Canada

'when testing the program within the QB environment, hardwire the program
'path like this. Comment out these lines BEFORE compiling to an EXE file.

GetProgPath$ = "E:\bang\"

DIM InRegs AS RegType, OutRegs AS RegType 'CPU registers.

'use DOS to find out where the Program Segment Prefix (PSP) is. = &H6200
CALL Interrupt(&H21, InRegs, OutRegs) 'DOS call.
DEF SEG = OutRegs.bx 'establish segment for PEEKing.

'get the segment address of the DOS environment from the PSP.

LSB% = PEEK(&H2C): MSB% = PEEK(&H2D) 'least & most significant bytes.
EnvSeg& = 256 * MSB% + LSB% 'make into an address.
DEF SEG = EnvSeg& 'point to environment segment.

'read the Environment until two Chr$(0) in a row are found. This marks the
'end of the environment. Skip the next 2 bytes and then read and save the
'ASCIIZ string which contains the complete drive, path and name of the
'program. (Only available with DOS 3.0 and later).

Offset% = 0 'index into memory locations.

Byte1% = PEEK(Offset%)
Offset% = Offset% + 1
IF Byte1% = 0 THEN Byte2% = PEEK(Offset%)
LOOP UNTIL Byte1% = 0 AND Byte2% = 0

Offset% = Offset% + 3 'skip the last Chr$(0) and next 2 bytes.

Path$ = Path$ + CHR$(PEEK(Offset%))
Offset% = Offset% + 1
LOOP UNTIL PEEK(Offset%) = 0

DEF SEG 'point back to BASIC segment.

'last job. Strip the program name and just return the drive and path.
'NOTE: that the path ends with a backslash - C:\GEORGE\BURNS\

Ndx% = LEN(Path$)
Ndx% = Ndx% - 1
LOOP UNTIL MID$(Path$, Ndx%, 1) = "\"

GetProgPath$ = LEFT$(Path$, Ndx%)


 December 13, 2017  Add comments

 Leave a Reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>