'===========================================================================
' Subject: PATHNAME OF CURRENT PROGRAM        Date: Unknown Date           
'  Author: Christy Gemmell                    Code: VBDOS                  
'  Origin: PATHNAME,CURRENT,PROGRAM         Packet: VB.ABC
'===========================================================================
'  > Does anyone know how to find the directory a program was run
'  > from, from inside that program? I hate the idea of hard coding
'  > the directory names into the program as a poor solution...

'The function below will do it. I've tested it with DOS and Windows95
'and it works fine.  Be aware, though, that it will only work properly
'in a stand-alone program. If you run it in the IDE it returns the
'path of VBDOS.EXE.

'--- cut here ----------------------------------------------------------------
' PATHNAME.BAS  demonstrates function to extract the pathname of the
'               current program.
'
'   Author:     Christy Gemmell
'
'  $INCLUDE: 'vbdos.bi'
'
    DECLARE FUNCTION PathName$ (ProgName$)

    A$ = PathName$(B$)
    PRINT A$, B$
END

'   Returns the directory path from where the current program was
'   launched. Also extracts the program filename.
'
FUNCTION PathName$ (ProgName$)
    DIM Regs AS RegType                         ' To hold register values
    Regs.ax = &H6200                            ' DOS Service 98
    INTERRUPT &H21, Regs, Regs                  '  - find PSP segment
    DEF SEG = Regs.bx                           ' Segment of current program
    EnvSeg& = PEEK(&H2C) + PEEK(&H2D) * 256&    ' Get environment pointer
    DEF SEG = EnvSeg&                           ' Environment segment
    I% = 0                                      ' Shuffle
    DO                                          '   through
       DO                                       '     environment
          ThisByte% = PEEK(I%)                  '       strings
          I% = I% + 1                           '         looking
       LOOP WHILE ThisByte%                     '           for two
       ThisByte% = PEEK(I%)                     '             successive
       I% = I% + 1                              '               null
    LOOP WHILE ThisByte%                        '                 bytes
    I% = I% + 2                                 ' Skip over some junk
    ProgName$ = ""                              ' To hold the program name
    DO                                          ' Read
       ThisByte% = PEEK(I%)                     '   each
       IF ThisByte% THEN                        '     character
          ProgName$ = ProgName$ + CHR$(ThisByte%)  '    of program
       END IF                                   '         name until
       I% = I% + 1                              '           we find
    LOOP WHILE ThisByte%                        '             null byte
    DEF SEG                                     ' Restore default segment
    L% = LEN(ProgName$)                         ' Did we find anything?
    IF L% THEN                                  ' If so
       DO                                       '   scan
          C$ = MID$(ProgName$, L%, 1)           '     backwards
          IF C$ = "\" THEN EXIT DO              '       looking
          L% = L% - 1                           '         for the
       LOOP WHILE L%                            '           path
    END IF                                      '             delimiter
    IF L% THEN                                  ' Seperate
       PathName$ = LEFT$(ProgName$, L%)         '   directory
       ProgName$ = MID$(ProgName$, L% + 1)      '     path
    ELSE                                        '       from
       PathName$ = ""                           '         program
    END IF                                      '           name
END FUNCTION

