DIR is driving me nuts.

New to FreeBASIC? Post your questions here.
PhiltheBear
Posts: 30
Joined: Jul 08, 2014 17:45

DIR is driving me nuts.

Post by PhiltheBear »

Can someone please help me with the DIR function?

Firstly, let me explain - I have about 50 years experience with BASIC (I used the very first BASIC developed by Kemeny and Kurtz) and I have whole suites of programs developed using FreeBasic using FBIde, all of which have worked perfectly. However, once again I come up against a problem which appears to be compounded by some fairly silly documentation. I refer to the DIR command.

To the best of my understanding (and remembering from years ago) if I use DIR once, defining the directory I wish to search by passing the appropriate directory name and an attribute I should get returned to me the first file name. Next time I call without any name or attribute and i should get the next file name (repeat as required until the filename comes up blank). However, the documentation on this is unclear, largely because somebody decided to put the example in a SUB structure. Why? I want to know about the simple use of DIR, not how clever you are with SUB. Be that as it may, I copied, precisely, the example given to try out. Unsurprisingly it doesn't work.

Here it is:
#include "dir.bi" 'provides constants to use for the attrib_mask parameter

Sub list_files (ByRef filespec As String, ByVal attrib As Integer)
Dim As String filename = Dir(filespec, attrib) ' Start a file search with the specified filespec/attrib *AND* get the first filename.
Do While Len(filename) > 0 ' If len(filename) is 0, exit the loop: no more filenames are left to be read.
Print filename
filename = Dir() ' Search for (and get) the next item matching the initially specified filespec/attrib.
Loop
End Sub

Print "directories:"
list_files "*", fbDirectory

Print
Print "archive files:"
list_files "*", fbArchive


I have "dir.bi" in the same directory as fbc.exe

The error I get is:
Only valid in -lang fb or deprecated or fblite, found '='
Dim As String filename = Dir(filespec, attrib)

So I tried passing the parameter of -lang fb to the compiler - and got precisely the same error. If there is something wrong with this code then can someone tell me what it is? Alternatively, if the code is correct why won't it compile?

The really annoying thing is that I can remember using such a function in Basic7 (i.e.before QuickBasic) with no problem at all.

Sorry for the attitude but I've spent hours trying to sort this out.
Tourist Trap
Posts: 2958
Joined: Jun 02, 2015 16:24

Re: DIR is driving me nuts.

Post by Tourist Trap »

Hello, for me this code (copied from your post) works:

Code: Select all

#include "dir.bi" 'provides constants to use for the attrib_mask parameter

Sub list_files (ByRef filespec As String, ByVal attrib As Integer)
	Dim As String filename = Dir(filespec, attrib) ' Start a file search with the specified filespec/attrib *AND* get the first filename.
	Do While Len(filename) > 0 ' If len(filename) is 0, exit the loop: no more filenames are left to be read.
		Print filename
		filename = Dir() ' Search for (and get) the next item matching the initially specified filespec/attrib.
	Loop
End Sub

Print "directories:"
list_files "*", fbDirectory

Print
Print "archive files:"
list_files "*", fbArchive
So it works well and shows my current directory content.
I don't know what is wrong in your case.
dbickin
Posts: 59
Joined: Aug 03, 2005 16:40

Re: DIR is driving me nuts.

Post by dbickin »

It compiled and ran fine for me using fbc 1.06 linux 64 bits.

I only got your error when forcing it to use -lang qb.

If you are using an IDE, could it be set up to set the lang to qb?

David
fxm
Moderator
Posts: 12107
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: DIR is driving me nuts.

Post by fxm »

I think dbickin made the correct diagnosis for your problem.
PhiltheBear wrote:However, once again I come up against a problem which appears to be compounded by some fairly silly documentation.
Otherwise, try to weigh your comments about this documentation page and consequently its author (SysOp), and even the documentation in general.
Last edited by fxm on Jun 16, 2020 7:43, edited 1 time in total.
paul doe
Moderator
Posts: 1733
Joined: Jul 25, 2017 17:22
Location: Argentina

Re: DIR is driving me nuts.

Post by paul doe »

Why is it that the posts by noobies that claim 'extensive experience' with BASIC always take the same form? Do I see a pattern here? Do they use a template of sorts?
  • Start by claiming lots of experience in BASIC (sine qua non requisite), then stating a trivial problem for which there is a working code snippet in the docs, which of course doesn't compile in their archaic setups since it was written for FreeBasic (say, 2004 onwards).
  • Long rant about why FreeBasic is sh*t, how the documentation/devs suck, and other 'blame your tools' kind of flaming.
  • Taking the wrong steps for solving it/no steps at all, without realizing that the root of the problem is that they can't properly tie their own shoelaces.
If you had all the experience you claim (and properly read the doc page), then you'll quickly realize two things:
  • BASIC (QuickBasic, QB64 and some/all of the rest) doesn't support initializers. So, this line:

    Code: Select all

    Dim As String filename = Dir(filespec, attrib)
    
    Is to be splitted in two:

    Code: Select all

    Dim As String filename
    filename = Dir(filespec, attrib)
    
  • Many dialects (up to QuickBasic 4.5) didn't support dir. So, in the #lang "qb" dialect, you need to use an alias: __dir. This is clearly stated on the silly documentation for the command in question.
So, here's the silly code snippet, showing off my ungodly skills with subs, refactored so that it compiles in the pleistoscene:

Code: Select all

#lang "qb"
#include "dir.bi" 'provides constants to use for the attrib_mask parameter

Sub list_files (ByRef filespec As String, ByVal attrib As Integer)
  Dim As String filename 
  filename = __Dir(filespec, attrib) ' Start a file search with the specified filespec/attrib *AND* get the first filename.
  Do While Len(filename) > 0 ' If len(filename) is 0, exit the loop: no more filenames are left to be read.
  ? filename
  filename = __Dir() ' Search for (and get) the next item matching the initially specified filespec/attrib.
  Loop
End Sub

? "directories:"
list_files "*", fbDirectory

?
? "archive files:"
list_files "*", fbArchive

sleep()
And now, expect the archetypal 'Oh my memory isn't what it used to be/it's too complicated/That's NOT BASIC anymore LOLZ!!!1' response.
badidea
Posts: 2591
Joined: May 24, 2007 22:10
Location: The Netherlands

Re: DIR is driving me nuts.

Post by badidea »

The example works fine here as well. Without 'sub' and for 'qb':

Code: Select all

#lang "qb"
#include "dir.bi"

Print "Archive files:"
filename$ = __Dir("*", fbArchive) 'first call
While Len(filename$) > 0
	Print "* " & filename$
	filename$ = __Dir()
Wend
Note that QBASIC used the FILES statement. 'Files' is not implemented in freebasic, see: LangQB, section 'Archaic commands'.
PhiltheBear
Posts: 30
Joined: Jul 08, 2014 17:45

Re: DIR is driving me nuts.

Post by PhiltheBear »

paul doe wrote: If you had all the experience you claim (and properly read the doc page), then you'll quickly realize two things:
  • BASIC (QuickBasic, QB64 and some/all of the rest) doesn't support initializers. So, this line:

    Code: Select all

    Dim As String filename = Dir(filespec, attrib)
    
    Is to be splitted in two:

    Code: Select all

    Dim As String filename
    filename = Dir(filespec, attrib)
    
    And now, expect the archetypal 'Oh my memory isn't what it used to be/it's too complicated/That's NOT BASIC anymore LOLZ!!!1' response.
Actually, I had tried that - and many other variations. But you prove my point that IF Dim As String filename = Dir(filespec, attrib) is wrong then it shouldn't be in the example showing how to use the function.

By the way (while you are talking about ignorance) the correct word is 'split' - not 'splitted.'

And I have no settings for different dialects - including QB when compiling. (But before you blow your mouth off again I've tried all such possibilities).
Last edited by PhiltheBear on Jun 15, 2020 22:18, edited 1 time in total.
jj2007
Posts: 2326
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Re: DIR is driving me nuts.

Post by jj2007 »

dbickin wrote:If you are using an IDE, could it be set up to set the lang to qb?
That could be the cause indeed. The code posted by Phil works like a charm, but I am not using an IDE that takes awkward decisions for me.
paul doe wrote:Why is it that the posts by noobies that claim 'extensive experience' with BASIC always take the same form?
Why is it that some experienced FB coders always react so deeply offended when a noob (or what they think is a noob) is stuck with a problem and cries for help?
Why is it that this forum is so crammed full with sh*tty posts and entire threads discussing what are the exact settings, commandline args, little secrets ("__dir with two understroke works" - check Help\FB-manual-1.05.0.chm), VS C or Gcc or Clang compiler options, environment variables etc. that make a hello world proggie compile properly?
badidea
Posts: 2591
Joined: May 24, 2007 22:10
Location: The Netherlands

Re: DIR is driving me nuts.

Post by badidea »

PhiltheBear wrote:But you prove my point that IF Dim As String filename = Dir(filespec, attrib) is wrong then it shouldn't be in the example showing how to use the function.
There is nothing wrong with that for the default 'fb' dialect. The problem seems that you are somehow using the 'qb' dialect of freebasic. If that is not the intention, then just compile and run from the command line, removing possible IDE interference.
paul doe
Moderator
Posts: 1733
Joined: Jul 25, 2017 17:22
Location: Argentina

Re: DIR is driving me nuts.

Post by paul doe »

@PhiltheBear
@jj2007
Image
fxm
Moderator
Posts: 12107
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: DIR is driving me nuts.

Post by fxm »

With FBIde, If you go into View->Settings and click on the FreeBASIC tab, then you can look at the the Compiler command field.
If it contains the -lang qb (or -forcelang qb) term, the qb (QuickBasic) dialect overwrites the default dialect fb (FreeBasic) when compiling.

If you find the -lang qb (and not the -forcelang qb) term in your Compiler command field, one solution without modifying your FBIde configuration is to insert the following line at the head of your source file:
#lang fb
when you want to compile this particular code in FreeBasic dialect.
PhiltheBear
Posts: 30
Joined: Jul 08, 2014 17:45

Re: DIR is driving me nuts.

Post by PhiltheBear »

[quote="badidea"]The example works fine here as well. Without 'sub' and for 'qb':

Code: Select all

#lang "qb"
#include "dir.bi"

Print "Archive files:"
filename$ = __Dir("*", fbArchive) 'first call
While Len(filename$) > 0
	Print "* " & filename$
	filename$ = __Dir()
Wend
Thank you. That did indeed work but I have checked the IDE setting for lang qb and it was blank.

I fear the documentation on this is confusing/misleading. Surely the whole point of documentation is to be clear. Something like your example here is simple and does not need the SUB construct. One could go further and simplify the While/Wend construction. The whole point of documenting what item is supposed to do is to make it 100% clear - not to appeal to those people who already are immersed in the language. That means concentrating on the item - and nothing else.

By the way, the reason I posted this in the Beginners section wasn't because I'm a beginner but because I couldn't see any other part of the forum I thought would be relevant. I also expected that the replies would be helpful rather than (in one particular case) a display of arrogant superiority. Yes, I do have 50 years Basic experience. Yes, I did use the very first Basic from Dartmouth. Yes, I have used many different versions/flavours of Basic over the time I've used it. And, yes, there are at least 2 industries and a number of banks throughout the world which use programs I've written as standard. So, I'm not new - but when the documented example doesn't work for me I will firstly examine a large variety of ways to make it work and if I exhaust those then I'll ask for help. As I clearly stated in the first post I spent hours on this before posting here. I appreciate that FreeBasic is an expansion of the original and it's my normal choice when creating programs now but I'm afraid that it is losing the whole point of what Basic was about - to be a very simple language that anyone could pick up and use. The implementation of DIR is over complicated, even without the difficulties I had. It could have been so much simpler and still produced the same results.

Thank you to all who took the trouble to respond (although it appears that some replies don't show until I use the reply function of the forum - so I'm sorry if I missed your contributions).
jj2007
Posts: 2326
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Re: DIR is driving me nuts.

Post by jj2007 »

I have just installed FBIde from https://fbide.freebasic.net/download

The first attempt failed, it got stuck at "your download will start in 5 seconds", but then I succeeded. It's over 9 years old btw - does anybody maintain it, at least officially?

So I extracted all files to my FreeBasic folder, as ...\FreeBasic\FBIde0.4.6r4 and ran fbide.exe

It greets me with an error message (compiler path corrupt or not found), and then invites me to find the compiler somewhere in the User\Documents folder (looking upstairs in FreeBasic\* would have required too much programming effort, I suppose)

So I set the correct path to ...\FreeBasic\fbc.exe and copied Phil's example
Compilation failed with an error message "file must be saved" (why? isn't the IDE able to save the current content to a temp file? do I have to save every little edit to see the result...?)

So I saved the file, and compiled it again - successfully. Which could mean, Phil, that during the process of setting up the IDE you put a setting #lang "qb" somewhere... mysteries of FreeBasic.

@Paul Doe: apologies accepted, thanks. Lovely picture btw - the frog has the right attitude for bug chasing under Windows ;-)
Last edited by jj2007 on Jun 17, 2020 12:07, edited 1 time in total.
fxm
Moderator
Posts: 12107
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: DIR is driving me nuts.

Post by fxm »

jj2007 wrote:Compilation failed with an error message "file must be saved" (why? isn't the IDE able to save the current content to a temp file?
The 'Quick run' command does this.
jj2007
Posts: 2326
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Re: DIR is driving me nuts.

Post by jj2007 »

fxm wrote:
jj2007 wrote:Compilation failed with an error message "file must be saved" (why? isn't the IDE able to save the current content to a temp file?
The 'Quick run' command does this.
Yep, thanks!
Post Reply