Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
_OpenFile Dialog in same folder as exe
#11
I wouldn't recommend relying on `Command$(0)`, it's unreliable for this as it may have a relative path such as `./foobar/qb64pe.exe`. Stripping off the exe in that case will give you the path "./foobar", which will be treated relative to your current CWD and likely give you the wrong result since it's not absolute.

The best way to handle this would be saving the contents of `_CWD$` as soon as the program starts, since we always fill that with the location of the executable (discovered by OS-specific means other than `Command$(0)`).

If you're finding that `_CWD$` is unreliable for some reason at the start of the program then that sounds like a bug, but it really shouldn't be as it's fairly straight forward in what it does. I'd be curious to hear what behavior you're seeing where it's wrong.
Reply
#12
(12-05-2022, 03:12 AM)SMcNeill Wrote: 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

Yes makes sense to have slash ready for other file work.
b = b + ...
Reply
#13
(12-05-2022, 03:48 AM)DSMan195276 Wrote: I wouldn't recommend relying on `Command$(0)`, it's unreliable for this as it may have a relative path such as `./foobar/qb64pe.exe`. Stripping off the exe in that case will give you the path "./foobar", which will be treated relative to your current CWD and likely give you the wrong result since it's not absolute.

The best way to handle this would be saving the contents of `_CWD$` as soon as the program starts, since we always fill that with the location of the executable (discovered by OS-specific means other than `Command$(0)`).

If you're finding that `_CWD$` is unreliable for some reason at the start of the program then that sounds like a bug, but it really shouldn't be as it's fairly straight forward in what it does. I'd be curious to hear what behavior you're seeing where it's wrong.

OK it's possible I got confused which where I was. I have a nightmare jungle of GUI versions in a couple of places and loading files from IDE I sometimes mess up path... 

I will switch back to _Cwd$ if that is the one most likely to return the full or absolute path and try and pay more attention when using _OpenFileDialog$

Thanks for the an under-the-hood tip Smile

That slash thing still could be handy.
b = b + ...
Reply
#14
Just remember to use _CWD$ at the start of the program, and to save the result to a non-changing variable.  If you navigate to different folders, your _CWD$ isn't going to be the same one that you started in.

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

Dim Shared As String ExePath
ExePath = _CWD$

CHDIR "C:\"
Print ExePath, _CWD$
Reply
#15
Brings back memories. Back in the ol' days we had to use a SHELL DIR to get the current folder. I used CHDIR a lot, so I always did the SHELL call then set a variable DIM SHARED OrigDir to the original directory the exe was running in.

Pete
If eggs are brain food, Biden has his scrambled.

Reply
#16
(12-05-2022, 05:54 AM)SMcNeill Wrote: Just remember to use _CWD$ at the start of the program, and to save the result to a non-changing variable.  If you navigate to different folders, your _CWD$ isn't going to be the same one that you started in.

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

Dim Shared As String ExePath
ExePath = _CWD$

CHDIR "C:\"
Print ExePath, _CWD$

(12-05-2022, 10:47 AM)Pete Wrote: Brings back memories. Back in the ol' days we had to use a SHELL DIR to get the current folder. I used CHDIR a lot, so I always did the SHELL call then set a variable DIM SHARED OrigDir to the original directory the exe was running in.

Pete

Yes 2 thumbs up from one thread! I think I will call slash, OSslash$, and it probably is better as fixed string.

Actually my OP code would work better for code where I was pasting on forward slash or backslash?... (geez those are worse than left or right!)  I was building pathed filenames with Linux slash because Windows didn't care so OSslash$ will have to be new code.

Very good! 

Pete, it does remind me of the shell like program I made back in DOS days. I remember having something rewriting a batch file as you use the batch file. Pretty cool stuff to modify a program (this more a script I suppose) as you Run it.
b = b + ...
Reply
#17
Batch files were a great tool. I made a few, but I had some friends who created them frequently, who created them frequently. Is there an ECHO in here?

Pete
If eggs are brain food, Biden has his scrambled.

Reply




Users browsing this thread: 1 Guest(s)