QB64 Phoenix Edition
_OpenFile Dialog in same folder as exe - Printable Version

+- QB64 Phoenix Edition (https://staging.qb64phoenix.com)
+-- Forum: QB64 Rising (https://staging.qb64phoenix.com/forumdisplay.php?fid=1)
+--- Forum: Code and Stuff (https://staging.qb64phoenix.com/forumdisplay.php?fid=3)
+---- Forum: Utilities (https://staging.qb64phoenix.com/forumdisplay.php?fid=8)
+---- Thread: _OpenFile Dialog in same folder as exe (/showthread.php?tid=1233)

Pages: 1 2


_OpenFile Dialog in same folder as exe - bplus - 12-04-2022

I want the DefaultPathAndFile$ where my exe is.

_StartDir$ sure didn't get it, that's my QB64 exe folder

_Cwd$ works if I have been in folder for awhile but sometimes seems to be wrong when just starting cold.

So I am trying this:
Code: (Select All)
Function ExePath$
    Dim As Long t1, t2
    t1 = _InStrRev(Command$(0), "\")
    t2 = _InStrRev(Command$(0), "/")
    If t1 > t2 Then ExePath$ = Left$(Command$(0), t1 - 1) Else ExePath$ = Left$(Command$(0), t2 - 1)
End Function

Look alright?  I add forward slash in case of Linux, Windows doesn't care. It seems to be working for all the tests I gave trying to get out of _CWD$


RE: _OpenFile Dialog in same folder as exe - Pete - 12-04-2022

That will work. I'm not happy _CWD$ isn't working 100%. Is it incorrect or just null. If it's a null return, you could always use a delay or...

Code: (Select All)
DO
    _LIMIT 10
    i = i + 1
    ExePath$ = _CWD$
LOOP UNTIL LEN(ExePath$) OR i > 100
IF i > 100 THEN PRINT "I BLAME STEVE!": END ELSE PRINT ExePath$

Pete


RE: _OpenFile Dialog in same folder as exe - SMcNeill - 12-04-2022

_CWD$ is the current working directory.  Say you start in D:\QB64, then CHDIR to D:\QB64\Files...  _CWD$ at this point would tell you D:\QB64\Files.

_STARTDIR$ tells you the directory that you started QB64 from.  Let's say you're in Drive "C:\", and you start QB64 in "D:\QB64\qb64pe.exe".  _STARTDIR$ will report "C:\" to you, as that's the directory you called QB64 from.

Neither of these commands are guaranteed to tell you where your compiled program itself is located.  COMMAND$(0), is the best place to look for that information, as you've noticed.  Wink


RE: _OpenFile Dialog in same folder as exe - mnrvovrfc - 12-04-2022

Only one 32-bit long integer variable is necessary:

Code: (Select All)
Function ExePath$
    Dim As Long t1
    t1 = _InStrRev(Command$(0), "\")
    if t1 = 0 then
        t1 = _InStrRev(Command$(0), "/")
    end if        
    ExePath$ = Left$(Command$(0), t1 - 1)
End Function



RE: _OpenFile Dialog in same folder as exe - Pete - 12-05-2022

From the good ol' days of "Must I optimize everything you do!" or Variable? We don't need no stinkin' variable!

Code: (Select All)
PRINT ExePath$

FUNCTION ExePath$
    IF _INSTRREV(COMMAND$(0), "\") THEN
        ExePath$ = LEFT$(COMMAND$(0), _INSTRREV(COMMAND$(0), "\") - 1)
    ELSE
        ExePath$ = LEFT$(COMMAND$(0), _INSTRREV(COMMAND$(0), "/") - 1)
    END IF
END FUNCTION


Just sayin'

Pete


RE: _OpenFile Dialog in same folder as exe - Pete - 12-05-2022

I know, I know, for fun I should have....

Code: (Select All)
PRINT ExePath$

FUNCTION ExePath$
    IF _INSTRREV(COMMAND$(0), "\") THEN
        ExePath$ = LEFT$(COMMAND$(0), ABS(_INSTRREV(COMMAND$(0), "\")) - ABS(1))
    ELSE
        ExePath$ = LEFT$(COMMAND$(0), ABS(_INSTRREV(COMMAND$(0), "/")) - ABS(1))
    END IF
END FUNCTION

Big Grin Big Grin Big Grin

Pete


RE: _OpenFile Dialog in same folder as exe - bplus - 12-05-2022

Absolutely, what starts with a w and has 3 letters but ends with t

I thought it was a joke about a wit missing an i, no...


RE: _OpenFile Dialog in same folder as exe - Pete - 12-05-2022

Or the one about the guy who failed the one question math test: What do you get when you cut a wit in half? His answer, a half-wit. Well, he was half right... two half-wits.

Pete Big Grin


RE: _OpenFile Dialog in same folder as exe - SMcNeill - 12-05-2022

Personally, I'd go this way:

Code: (Select All)
$If WIN Then
    Dim Shared As String * 1 Slash: Slash = "\"
$Else
        Dim Shared As String * 1 Slash: Slash = "/"
$End If

Print ExePath


Function ExePath$
    ExePath$ = Left$(Command$(0), _InStrRev(Command$(0), Slash) - 1)
End Function


Here we sort out our slash direction to begin with, and save it in a stored variable for use whenever it's needed in our program.  After all, if it's needed to find ExePath, then it's going to be needed when we try and make use of that path and open a file, or anything else with it as well.  Wink


RE: _OpenFile Dialog in same folder as exe - Pete - 12-05-2022

Not so fast there, Sparky. Sure, I recon Windows is the only OS that uses bsackslashes, but I'm just gettin' started on my Yosemite Vista OS with Smart ASCII Old-West technology. Then you'll be sorry when ya goes to run Steve's code, ya canrsarn galoots!

 - Sam