DIR does list (some) files as directories

New to FreeBASIC? Post your questions here.
Post Reply
padawan
Posts: 10
Joined: Jan 10, 2019 13:41

DIR does list (some) files as directories

Post by padawan »

I´ve discovered a strange phenomenon. I played around with the following code example I found on the german Freebasic portal on this page:

https://www.freebasic-portal.de/befehls ... r-197.html

The only amend I did is the CHDIR in Line 3 because I wanted to try the code in various folders. The result the console ouputs is obviously errorous: The files changelog.txt, fbx.exe and readme.txt are categorized as folders.

The funny thing is: the differentiation between files ( archives) and folders works perfectly fine in all folders of my hard drive except for the path where the Freebasic compiler is in. I tried it on 2 workstations. Both show the same effect. I am using Win10 64bit. I don´t understand why this happens and I wonder how this can be fixed?

The code is:

Code: Select all

#INCLUDE "dir.bi"

CHDIR "C:\Users\padawan\FreeBASIC-1.05.0-win64"

DECLARE SUB listFiles (filespec AS STRING, attrib AS INTEGER)

SUB listFiles (filespec AS STRING, attrib AS INTEGER)
  ' alle Dateien mit den angegebenen Attributen auflisten
   DIM filename AS STRING

   filename = DIR(filespec, attrib)
   DO
      PRINT SPACE(4); filename
      filename = DIR("", attrib)
   LOOP WHILE LEN(filename)
END SUB

PRINT "subfolders"
listFiles "*", fbDirectory

PRINT "archive files:"
listFiles "*", fbArchive

PRINT "hidden and/or system files beginning with an 'a':"
listFiles "a*", fbHidden OR fbSystem
SLEEP
The console output is:

Code: Select all

subfolders
    .
    ..
    bin
    changelog.txt
    doc
    examples
    fbc.exe
    inc
    lib
    readme.txt
archive files:
    changelog.txt
    FB-manual-1.01.0.chm
    fbc.exe
    readme.txt
hidden and/or system files beginning with an 'a':
fxm
Moderator
Posts: 12107
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: DIR does list (some) files as directories

Post by fxm »

I do not get an error, but can you test this code?

Code: Select all

#INCLUDE "dir.bi"

CHDIR "C:\Users\padawan\FreeBASIC-1.05.0-win64"

DECLARE SUB listFiles (filespec AS STRING, attrib AS INTEGER)

SUB listFiles (filespec AS STRING, attrib AS INTEGER)
  ' alle Dateien mit den angegebenen Attributen auflisten
   DIM filename AS STRING

   filename = DIR(filespec, attrib)
   DO WHILE LEN(filename)
      PRINT SPACE(4); filename
      filename = DIR()
   LOOP
END SUB

PRINT "subfolders"
listFiles "*", fbDirectory

PRINT "archive files:"
listFiles "*", fbArchive

PRINT "hidden and/or system files beginning with an 'a':"
listFiles "a*", fbHidden OR fbSystem
SLEEP
padawan
Posts: 10
Joined: Jan 10, 2019 13:41

Re: DIR does list (some) files as directories

Post by padawan »

Thanks, fxm. I see you changed the loop condition. But I get the same wrong output with your code (the output I posted in my first post) .

I don´t get an error like a runtime error or so, it´s just that the results are wrong. Files are listed as folders. Files are marked with the attribute fbDirectory, but they sould be marked with fbArchive. But only in the Freebasic directory. In all other directories it works fine. When you cange the path in the CHDIR line to the path where you fbc.exe is, do you get correct output?
fxm
Moderator
Posts: 12107
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: DIR does list (some) files as directories

Post by fxm »

padawan wrote:When you change the path in the CHDIR line to the path where your fbc.exe is, do you get correct output?
Yes, correct result for me (Win 10 64bit):

Code: Select all

subfolders
    .
    ..
    bin
    doc
    examples
    IDE
    inc
    lib
archive files:
    changelog.txt
    fbc.exe
    fbide.exe
    FbIdeFix.dll
    FBIDETEMP.bas
    FBIDETEMP.exe
    fblogo.bmp
    fblogo.ico
    gmon.out
    libmydll.dll.a
    libwindow9.a
    libz.a
    mingwm10.dll
    mydll.bas
    mydll.bi
    mydll.dll
    readme.txt
    Window9.bi
    WinGUI.bi
hidden and/or system files beginning with an 'a':
padawan
Posts: 10
Joined: Jan 10, 2019 13:41

Re: DIR does list (some) files as directories

Post by padawan »

Boah... that´s weird! What could be the reason that I get wrong results on my 2 Workstations?
counting_pine
Site Admin
Posts: 6323
Joined: Jul 05, 2005 17:32
Location: Manchester, Lancs

Re: DIR does list (some) files as directories

Post by counting_pine »

Possibly this is due to the not-entirely-intuitive logic of Dir.
I think the expected behaviour is that Dir will include files which have all of the attributes given.
In reality, Dir's behaviour is to exclude files which have any attributes that are not given.

This means that Dir(..., fbDirectory) will include anything that doesn't have a ReadOnly, Hidden, System, or Archive attribute.
This includes directories that don't have the above attributes.
It will also include files that don't have the above attributes.

If I recall correctly, it is often the case that files will have the Archive attribute set, while folders will not.
Files with the Archive attribute will therefore be excluded from Dir(..., fbDirectory). (And I think there is some logic in Dir that ensures Archive is set when Directory isn't, to capitalise on this.)

But there if a file has no attributes at all, not even Archive, then there is no way for Dir to exclude it using its attributes.

This means that in many cases, you must use the third parameter of Dir to return the attributes of each file/folder, and examine them to decide whether you want to process it or skip it.

The second example at KeyPgDir shows how to use the third parameter to identify which attributes a file/folder has. This kind of logic can be used by the coder to decide which ones to skip.
srvaldez
Posts: 3379
Joined: Sep 25, 2005 21:54

Re: DIR does list (some) files as directories

Post by srvaldez »

perhaps this code by Richard may be of interest to you viewtopic.php?p=191571#p191571
I incorporated it in your example

Code: Select all

#INCLUDE "dir.bi"

CHDIR "C:\Users\padawan\FreeBASIC-1.05.0-win64"

DECLARE SUB listFiles (filespec AS STRING, attrib AS INTEGER)

SUB listFiles (filespec AS STRING, attrib AS INTEGER)
  ' alle Dateien mit den angegebenen Attributen auflisten
   DIM filename AS STRING

   filename = DIR(filespec, attrib)
	Do While Len( filename )
		Select Case filename
		Case "."    ' list of file names to ignore
		Case ".."
		Case Else
			print filename
		End Select
		filename = Dir( )
	Loop
END SUB

PRINT "subfolders"
listFiles "*", fbDirectory
print "============================================================"
PRINT "archive files:"
listFiles "*", fbArchive
print "============================================================"
PRINT "hidden and/or system files beginning with an 'a':"
listFiles "a*", fbHidden OR fbSystem
SLEEP
Last edited by srvaldez on Jan 17, 2019 14:57, edited 1 time in total.
padawan
Posts: 10
Joined: Jan 10, 2019 13:41

Re: DIR does list (some) files as directories

Post by padawan »

I understand... could it be a way to 100% determine a directory to check if CHDIR returns 1 or 0? I mean like

Code: Select all

IF CHDIR(isItAFileOrAFolder) = 0 THEN
   PRINT "It´s a file"
ELSE
   PRINT "It´s a folder"
END IF
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: DIR does list (some) files as directories

Post by MrSwiss »

A more recent example, can be found in "Beginners": DIR() question
(Assume it to be FBC 64 bit compatible too, older stuff may have problems.)
Forum-Search is your friend ...
padawan
Posts: 10
Joined: Jan 10, 2019 13:41

Re: DIR does list (some) files as directories

Post by padawan »

MrSwiss wrote: DIR() question
(Assume it to be FBC 64 bit compatible too, older stuff may have problems.)
Forum-Search is your friend ...
Same command but different problem.
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: DIR does list (some) files as directories

Post by MrSwiss »

padawan wrote:Same command but different problem.
Well, I consider that remark, as rather short-sighted.
(It could very well solve your next problem ...)
padawan
Posts: 10
Joined: Jan 10, 2019 13:41

Re: DIR does list (some) files as directories

Post by padawan »

MrSwiss wrote:
padawan wrote:Same command but different problem.
(It could very well solve your next problem ...)
No question the thread you posted a link to is interesting when dealing with the DIR() command. What I meant was, I did use the board search and I found that thread before, but it didn´t solve my problem. So I already made friends with the board search.
Tourist Trap
Posts: 2958
Joined: Jun 02, 2015 16:24

Re: DIR does list (some) files as directories

Post by Tourist Trap »

padawan wrote:I understand... could it be a way to 100% determine a directory to check if CHDIR returns 1 or 0? I mean like

Code: Select all

IF CHDIR(isItAFileOrAFolder) = 0 THEN
   PRINT "It´s a file"
ELSE
   PRINT "It´s a folder"
END IF
I tried this below and it seems to work. Maybe you can try and tell us if it does the job?

Code: Select all

var initialdir  => curDir()

'test if CHDIR will go

var fileOrFolderNameToTest    => "xxx"  'could be some item taken in a loop
var result  => CHDIR(curDir() & "/" & fileOrFolderNameToTest)

if not result then 
    ? "it's a folder"
else
    ? "it's a file"
end if

'if we changed dir, then we return to initial dir
if not result then CHDIR(initialdir)

getKey()
'(eof)
Of course here I use CHDIR itself. If you want to avoid changing dirs to do the test, we'll have to look forward for something else.
counting_pine
Site Admin
Posts: 6323
Joined: Jul 05, 2005 17:32
Location: Manchester, Lancs

Re: DIR does list (some) files as directories

Post by counting_pine »

In my previous post, I explained how to use the attributes returned by Dir (using the optional third parameter) to choose whether to process or ignore the file.
Here's a function that takes that logic and wraps it in a function:

Code: Select all

#include "dir.bi"
function dir_folders(item_spec as const string = "", attrib_mask as const integer = &h37, byref out_attrib as integer = 0) as string
        dim filename as string
        if item_spec <> "" then
                filename = dir(item_spec, attrib_mask, out_attrib)
        else
                filename = dir(out_attrib)
        end if
        do until (out_attrib and fbDirectory)
                if filename = "" then return ""
                filename = dir(out_attrib)
        loop
        return filename
end function

print using "Folders matching '&':"; command(1)

dim as string d = dir_folders(command(1))

do until d = ""
        print d
        d = dir_folders()
loop
grindstone
Posts: 862
Joined: May 05, 2015 5:35
Location: Germany

Re: DIR does list (some) files as directories

Post by grindstone »

I would recommend to call DIR with all attributes set and check the desired attributes afterwards:

Code: Select all

#Include "dir.bi"

ChDir "C:\Users\padawan\FreeBASIC-1.05.0-win64"

Declare Sub listFiles (filespec As String, attrib As Integer)

Sub listFiles (filespec As String, attrib As Integer)
  ' alle Dateien mit den angegebenen Attributen auflisten
   Dim filename As String
   Dim att As Integer

   filename = Dir(filespec, -1, @att)
   Do
      If (attrib And att) Then
      	Print Space(4); filename
      EndIf
      filename = Dir("", attrib, @att)
   Loop While Len(filename)
End Sub

Print "subfolders"
listFiles "*", fbDirectory

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

Print "hidden and/or system files beginning with an 'a':"
listFiles "a*", fbHidden Or fbSystem
Sleep
Post Reply