Have @dir in a compiled program point to current directory?
-
- Posts: 9
- Joined: Mon 08 Aug 2022, 14:26
Have @dir in a compiled program point to current directory?
[Windows 11, BBC BASIC for SDL]
In a simple program, I used OSCLI "*DIR *.jpg > filelist.txt" to get a list of jpg files currently available in the directory in which the program resides.
That works as expected, but compiling the program fails, as the exe refers to the temporary directory that was used in compiling (which then generates a bad command because that temporary directory does not exist anymore).
What would be the correct way to make the exe use the directory in which it actually resides (not known at time of compiling) as @dir?
In a simple program, I used OSCLI "*DIR *.jpg > filelist.txt" to get a list of jpg files currently available in the directory in which the program resides.
That works as expected, but compiling the program fails, as the exe refers to the temporary directory that was used in compiling (which then generates a bad command because that temporary directory does not exist anymore).
What would be the correct way to make the exe use the directory in which it actually resides (not known at time of compiling) as @dir?
Re: Have @dir in a compiled program point to current directory?
This should work:jeroen_soutberg wrote: ↑Wed 10 Jan 2024, 08:56 In a simple program, I used OSCLI "*DIR *.jpg > filelist.txt" to get a list of jpg files currently available in the directory in which the program resides.
What would be the correct way to make the exe use the directory in which it actually resides (not known at time of compiling) as @dir?
Code: Select all
OSCLI "*DIR """ + @dir$ + "*.jpg"" > filelist.txt"
"This function scans a specified directory and returns the files (and sub-directories, if appropriate) it contains in a string array".
-
- Posts: 9
- Joined: Mon 08 Aug 2022, 14:26
Re: Have @dir in a compiled program point to current directory?
Many thanks for pointing me in the right direction, especially to the FN. Will work on that this afternoon.
-
- Posts: 9
- Joined: Mon 08 Aug 2022, 14:26
Re: Have @dir in a compiled program point to current directory?
I have now tried both options in very simple test programs,using a test folder D:\OSCLItest containing some jpg files and the test programs, and both programs exhibit the same behaviour: running them using the Run command they do exactly as expected, but when I try to compile them, that fails:
The FN_dirscan-program is
When trying to compile this, it fails with the message
Couldn't copy 'D:\OSCLItest\*.jpg'
In the compiler window, I unchecked all optimization options. The utility lists the following Embedded files:
@lib$+"dlglib"
@lib$+"sortlib"
@lib$+"stringlib"
@lib$+"filedlg"
@dir$ *.jpg
The OSCLI-program is
Again I unchecked all optimization options in the compiler window. As expected, the utility now lists as Embedded file only
@dir *.jpg
and this also fails with the message
Couldn't copy 'D:\OSCLItest\*.jpg'
I would be grateful for an explanation of what happens here, and a possible solution.
The FN_dirscan-program is
Code: Select all
INSTALL @lib$+"dlglib"
INSTALL @lib$+"sortlib"
INSTALL @lib$+"stringlib"
INSTALL @lib$+"filedlg"
DIM imageList$(300)
numberofImages% = FN_dirscan(imageList$(),"dir *.*",".jpg","","")
PRINT numberofImages%
END
Couldn't copy 'D:\OSCLItest\*.jpg'
In the compiler window, I unchecked all optimization options. The utility lists the following Embedded files:
@lib$+"dlglib"
@lib$+"sortlib"
@lib$+"stringlib"
@lib$+"filedlg"
@dir$ *.jpg
The OSCLI-program is
Code: Select all
OSCLI "*DIR """ + @dir$ + "*.jpg"" > filelist2.txt"
PRINT"After OSCLI"
END
@dir *.jpg
and this also fails with the message
Couldn't copy 'D:\OSCLItest\*.jpg'
I would be grateful for an explanation of what happens here, and a possible solution.
Re: Have @dir in a compiled program point to current directory?
There seem to be a couple of issues. Firstly, you can't (currently, at least) specify a wildcard in the list of embedded files, so this isn't valid:jeroen_soutberg wrote: ↑Thu 11 Jan 2024, 12:35 I would be grateful for an explanation of what happens here, and a possible solution.
Code: Select all
@dir$ *.jpg
Secondly, your program still suffers from the same issue as your previous attempt, in that it assumes that the current directory is the same as the directory containing the executable, and although that will usually be the case when a program is run from the IDE it generally won't be when 'compiled'.
So when referencing files in the same directory as the executable you need to do so explicitly:
Code: Select all
numberofImages% = FN_dirscan(imageList$(),"dir """ + @dir$ + "*.*""",".jpg","","")
Code: Select all
numberofImages% = FN_dirscan(imageList$(),"dir """ + @dir$ + "*.jpg""","","","")
Code: Select all
REM!Embed @lib$+"sortlib", @lib$+"stringlib", @lib$+"filedlg"
REM!Embed @dir$+"colosseum.jpg", @dir$+"virtual.jpg", @dir$+"freed.jpg"
INSTALL @lib$+"sortlib"
INSTALL @lib$+"stringlib"
INSTALL @lib$+"filedlg"
DIM imageList$(300)
numberofImages% = FN_dirscan(imageList$(),"dir """ + @dir$ + "*.jpg""","","","")
PRINT numberofImages%
END
Re: Have @dir in a compiled program point to current directory?
As this would clearly be a valuable feature, I can confirm that (barring unexpected side-effects) I will add this capability to the next release of BBC BASIC for SDL 2.0. Thank you for alerting me to this possibility, which I have considered previously but hasn't risen to the 'top of the pile'.Hated Moron wrote: ↑Thu 11 Jan 2024, 14:40 The option to include a wildcard [in a REM!Embed directive] is something that I could consider for a future release.
-
- Posts: 9
- Joined: Mon 08 Aug 2022, 14:26
Re: Have @dir in a compiled program point to current directory?
Thank you very much for the explanation. Indeed, if I embed only one jpg file explicitly the compiler finishes without errors, and the exe, when moved to the directory where I keep some 200 jpg files for an anniversary celebration, generates exactly the list of those 200-something I need.
For a more general list, it would indeed be great to have the possibility you describe above (saw that only after posting the first version of the present post).
For a more general list, it would indeed be great to have the possibility you describe above (saw that only after posting the first version of the present post).
-
- Posts: 9
- Joined: Mon 08 Aug 2022, 14:26
Re: Have @dir in a compiled program point to current directory?
In hindsight, I think what I would very much like to have is the possibility to use a variable similar to @dir$ that behaves as @dir$ in the compiled program (i.e. contains the directory in which the exe lives when it is executed, not the directory in which it was compiled).
Re: Have @dir in a compiled program point to current directory?
Sorry, I don't understand; what do you mean by "the directory in which it was compiled"?jeroen_soutberg wrote: ↑Wed 17 Jan 2024, 09:49 In hindsight, I think what I would very much like to have is the possibility to use a variable similar to @dir$ that behaves as @dir$ in the compiled program (i.e. contains the directory in which the exe lives when it is executed, not the directory in which it was compiled).
The purpose of @dir$ is to provide a 'starting point' from which your program can locate the resource files it needs, so for example image files might be in @dir$ + "resources/images/" and sound files in @dir$ + "resources/sounds/" etc.
Here's a snippet from a large program (David Williams' 'Forces of Darkness') showing how he loads sub-modules:
Code: Select all
INSTALL @dir$+"Resources/Lib/PROC/PROCInitNewGame.bbc"
INSTALL @dir$+"Resources/Lib/PROC/PathHandlers.bbc"
INSTALL @dir$+"Resources/Lib/PROC/PROCDrawNasty.bbc"
INSTALL @dir$+"Resources/Lib/PROC/PROCDrawNastyShadow.bbc"
INSTALL @dir$+"Resources/Lib/PROC/PROCHandleNasty.bbc"
What do you want to do that you cannot do now?
-
- Posts: 9
- Joined: Mon 08 Aug 2022, 14:26
Re: Have @dir in a compiled program point to current directory?
I would like to be able to use @dir$ in a compiled program in the same way as I can use it in the original program, e.g., having PRINT @dir$ printing the directory in which the program/the exe is run.