Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Directories....
#1
I have about 5 years of qbasic experience, however the question I have is extremely noobish :roll: , so here goes it:
How do you get the directory that the exe is executing from? You know, so that you can avoid hard coding directories?
Thanks. Smile
[Image: freebasic.png]
Reply
#2
I wrote some code to do this a while back:

[syntax="QBasic"]
' A program to get the current directory and store it in a string.
' The main function is GetCurDir$
'

DECLARE FUNCTION GetCurDir$ ()
CLS
PRINT "Current directory: " + GetCurDir$

FUNCTION GetCurDir$
'
' GetCurDir$
' Coded by shiftLynx (Chris Davies) [12th Aug 2004]
' c.g.davies@gmail.com
'

' Create a string to store our assembly code in
DIM asmcode AS STRING * 27

' and a string to store the output from the asm code
DIM currentdir AS STRING * 512

' Get the segment and offset of the currentdir string
cdseg% = VARSEG(currentdir)
cdoff% = VARPTR(currentdir)

' Get the low and high bytes of cdseg%
cdsegb1% = cdseg% AND &HFF
cdsegb2% = (cdseg% \ &H100) AND &HFF

' Get the low and high bytes of cdoff%

cdoffb1% = cdoff% AND &HFF
cdoffb2% = (cdoff% \ &H100) AND &HFF

' First we need to get the current drive with this code...
'
'
' PUSHAD
' MOV AH, 0x19
' INT 0x21
' ADD AL, 0x41
' MOV BX, [cdseg]
' MOV ES, BX
' MOV DI, [cdoff]
' MOV [ESBig GrinI], AL
' POPAD
' RETF
'
asmcode = CHR$(&H66) + CHR$(&H60) + CHR$(&HB4) + CHR$(&H19) + CHR$(&HCD) + CHR$(&H21) + CHR$(&H4) + CHR$(&H41) + CHR$(&HBB) + CHR$(cdsegb1%) + CHR$(cdsegb2%) + CHR$(&H8E) + CHR$(&HC3) + CHR$(&HBF) + CHR$(cdoffb1%) + CHR$(cdoffb2%) + CHR$(&H26) + _
CHR$(&H88) + CHR$(&H5) + CHR$(&H66) + CHR$(&H61) + CHR$(&HCB)

' Execute the code
DEF SEG = VARSEG(asmcode)
CALL ABSOLUTE(VARPTR(asmcode))
DEF SEG

' Now we have the drive letter - keep track of it
retval$ = MID$(currentdir, 1, 1) + ":\"

' Next we need the current directory...
'
' PUSHAD
' MOV AH, 0x47
' XOR DL, DL
' INT 0x21
' MOV AX, [cdseg]
' MOV ES, AX
' MOV DI, [cdoff]
' nextbyte:
' MOVSB
' MOV AL, [ES:SI]
' TEST AL, AL
' JNZ nextbyte
' POPAD
' RETF
'
asmcode = CHR$(&H66) + CHR$(&H60) + CHR$(&HB4) + CHR$(&H47) + CHR$(&H30) + CHR$(&HD2) + CHR$(&HCD) + CHR$(&H21) + CHR$(&HB8) + CHR$(cdsegb1%) + CHR$(cdsegb2%) + CHR$(&H8E) + CHR$(&HC0) + CHR$(&HBF) + CHR$(cdoffb1%) + CHR$(cdoffb2%) + CHR$(&HA4) _
+ CHR$(&H26) + CHR$(&H8A) + CHR$(&H4) + CHR$(&H84) + CHR$(&HC0) + CHR$(&H75) + CHR$(&HF8) + CHR$(&H66) + CHR$(&H61) + CHR$(&HCB)

' Call the assembly code
DEF SEG = VARSEG(asmcode)
CALL ABSOLUTE(VARPTR(asmcode))
DEF SEG

' Find out how many characters currentdir has
numchars% = 0
FOR i% = 1 TO LEN(currentdir)
IF MID$(currentdir, i%, 1) = CHR$(0) THEN EXIT FOR
numchars% = numchars% + 1
NEXT i%

' Append the current directory to the drive letter
retval$ = retval$ + MID$(currentdir, 1, numchars%)

' Set the return value
GetCurDir$ = retval$

END FUNCTION

[/syntax]
img]http://www.cdsoft.co.uk/misc/shiftlynx.png[/img]
Reply
#3
:o you really need all that code? ok then. are there any other ways of doing it? Libraries perchance? I am using FB, if that changes anything. Also, whats the best FBasic IDE as far as not crashling? I have FBIDE (by vongrondric) and it crashes freqently.
Thanks
[Image: freebasic.png]
Reply
#4
have you tried BE (basic editor), it's a freeBASIC and PowerBASIC IDE.
url=http://www.sloganizer.net/en/][Image: style4,TheDarkJay.png][/url]
Reply
#5
Quote:..... How do you get the directory that the exe is executing from? You know, so that you can avoid hard coding directories? Thanks. Smile
The directory that you are referring could be the "default directory" or the directory where the exe resides.

The "default directory", is the directory from where you are executing the exe from. Example:
C:
CD \ MYSYS
D:\PROGS\UTIL.EXE
In this case, the default directory is C:\MYSYS. Input and output files opened by the UTIL.EXE program with a name only, no path, must be in this directory.

The directory of the exe is D:\PROGS. That's where it loads the exe from, but has no effect on the default directory unless the exe program changes the drive and directory.

An important consideration is the DRIVE. The complete path is composed of a drive:\directory.

If you know that the exe is always in the default directory, which is the normal case, then you can obtain the required path:\directory by executing the following instruction in your program:
SHELL "DIR >TEMP"
Then read the file TEMP comparing the leading characters for
" Directory of " (note the leading and trailing blanks)
afterwhich will appear the DRIVE:\DIRECTORY
*****
Reply
#6
Quote:If you know that the exe is always in the default directory, which is the normal case
thats what i'm trying to avoid....its not always where you want it to be Smile
[Image: freebasic.png]
Reply
#7
Quote:
Moneo Wrote:If you know that the exe is always in the default directory, which is the normal case
thats what i'm trying to avoid....its not always where you want it to be Smile
Then you have a dilemma. If you don't know where the subject exe is, then how the heck are you going to execute it?

The only thing I can think of is that you're running the exe from a menu program. In this case, the menu program has to have the path of the exe hardcoded, or it has to get the path from a file. If the path changes, you can change this file, and not have to modify the menu program.

If this is not the case, please explain.
*****
Reply
#8
Well, it cant be that hard. Here's what I'm talking about: I just downloaded Wetspot 2 and put it in a directory. I'm quite sure that its not the same directory it was programmed in. Yet, it can open all its files. How do i do this? Is that ASM code what it takes?
[Image: freebasic.png]
Reply
#9
if you are using qb71, just use the command DIR$ or CURDIR$ (just see the 'Help')
am the last survivor of the Quickbasics world (at least at qbasicnews).
Reply
#10
i dunno if this is how it works in qb (pretty sure it is) but....... all you do is for files you need from the same dir, just use the filename.

Lets say u have g.exe in c:\game

now in g.exe theres a file u need called 'r.txt', thats in c:\game. all you need to do is open "r.txt" for mode as blahblah


lets say you have a folder c:\game\pics

and inside theres a file called y.spr

to acess this from the exe, youd do open "pics\y.spr" for blhablhab


works in fb anywho
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)