Page 1 of 1

Strange directory output, isn't the same?

Posted: Sun Aug 20, 2017 5:13 pm
by oakvalley
Hi there,

I have 4 files in a directory on Windows 7 64bit English:

franç.txt
franš.txt
franþ.txt
ranã.txt

In a normal directory via windows standard explorer or Total Commander they are listed exactly in that order assumingly sorted alphabetically, while purebasic's Examinedirectory manages to shift the 2 middle ones, so they appear like this:

franç.txt
franþ.txt
franš.txt

ranã.txt

I'm trying with both PB5.60 (unicode) and PB5.31 both same result.
All chars are within 0-255 ASCII Range.
ç = $E7 = 231
š = $9A = 154
þ = $FE = 254

ã = $E3 = 227

How can this happen? Doesn't Purebasic read the directory as each item comes along? In that case both listings should be the same?

Why are 154 and 254 flipped in PureBasic's output?

I used the standard ExamineDirectory() example from PureBasic help. It should be noted that issuing a "DIR" command in a standard windows console outputs the same as Purebasic does, where the 2 middle items are flipped.

Re: Strange directory output, isn't the same?

Posted: Sun Aug 20, 2017 5:33 pm
by DK_PETER
Use a list or array and do a sort...

Code: Select all

Global NewList files.s()

Declare.i ExamineDir(dirname.s, Pattern.s = "*") 
Declare.s slash(direct.s)

Procedure.s slash(direct.s)
  If Right(direct,1) <> "\"
    ProcedureReturn direct + "\"
  Else
    ProcedureReturn direct
  EndIf
EndProcedure

Procedure.i ExamineDir(dirname.s, Pattern.s = "*") 
    Protected dir.i, Filename.s
    dir = ExamineDirectory(#PB_Any, dirname, Pattern) 
    If dir > 0
      While NextDirectoryEntry(dir) 
        Filename = DirectoryEntryName(dir) 
        If DirectoryEntryType(dir) = #PB_DirectoryEntry_Directory And Filename <> "." And Filename <> ".." 
          ExamineDir(slash(dirname) + Filename)
        ElseIf DirectoryEntryType(dir) = #PB_DirectoryEntry_File And Filename <> "." And Filename <> ".."
          AddElement(files())
          files() = slash(dirname) + Filename
        EndIf 
      Wend 
      FinishDirectory(dir) 
    EndIf 
    ;Add to the list and sort after search...
    SortList(files(), #PB_Sort_Ascending)
    ForEach files()
      Debug files()
    Next 
    ProcedureReturn #True
  EndProcedure 
  
  ExamineDir("E:\texttest", "*.txt")
  

Re: Strange directory output, isn't the same?

Posted: Sun Aug 20, 2017 5:38 pm
by Little John
DK_PETER wrote:Use a list or array and do a sort...
... and please do not ask comprehension questions in the "Bug reports" section of the forum!

Re: Strange directory output, isn't the same?

Posted: Sun Aug 20, 2017 5:38 pm
by GPI
PureBasics Directory-Commands return a unsorted list, in the order of the directory-entries in the file system.

Re: Strange directory output, isn't the same?

Posted: Sun Aug 20, 2017 5:44 pm
by oakvalley
Yes, into an array it works as intended, sorting works. I tried that too.

However, for my routine I simply wanted to use the OS own sorting of filenames to get stuff sorted "for free" just by reading the directory 1-by-1.
With your approach one have to add more code + spend time on sorting.

Its basically just the wierdness of Windows that sometimes makes me wonder and freak out, where is the logic for that behaviour, when thinking items
in a directory, you would expect them to be sorted alphabetically in Windows Explorer, Command Console and PB as the same.

Guess its not a bug in PB - but something to be aware of.

Re: Strange directory output, isn't the same?

Posted: Sun Aug 20, 2017 5:57 pm
by skywalk
There are many nitpick bugs in Windows 7,10. :idea:
Ex. Select several files in File Explorer, and do a Shift+RM Click -> Copy as path.
Then paste into a text editor. You'll see the order is different than your original selection.