Page 1 of 1

Error with algorithm to read folders, subfolders and files inside a directory, can someone help me to improve it?

Posted: Thu Mar 30, 2023 1:41 am
by skinkairewalker
Hello everyone, can someone help me with this algorithm to read all files, folders and subfolders inside a directory?

it works well, but in directories with many files and folders, it crashes at some point saying that:
[21:38:41] [ERROR] Line: 19
[21:38:41] [ERROR] The specified #Directory is not initialised.
[21:38:43] The Program was killed.

Can someone help me to improve it without this problem occurring?

screenshot : https://prnt.sc/ryyr6JviuIR1

code below :

Code: Select all


Procedure ReadDirectory()

  tmpDir = Random(9999,1)
  
  While IsDirectory(tmpDir) = #True
    tmpDir = Random(9999,1)
    FinishDirectory(tmpDir)
  Wend    
    
 
  Directory$ = GetHomeDirectory() 
  If ExamineDirectory(tmpDir, Directory$, "*.*")
    While NextDirectoryEntry(tmpDir)
      If DirectoryEntryType(tmpDir) = #PB_DirectoryEntry_File
        arquivosLidos = arquivosLidos + 1
        Debug arquivosLidos
        Type$ = "[File] "
       ; Size$ = " (Size: " + DirectoryEntrySize(tmpDir) + ")"
       ;  Debug Type$ + DirectoryEntryName(tmpDir) + " Pasta : "+RemoveString(Directory$,Dir$)
        Debug "PathPart: " + ReplaceString(ReplaceString(RemoveString(Directory$,Dir$),"\","/"),"//","/")+"/"+DirectoryEntryName(tmpDir)
      Else
        If (DirectoryEntryName(tmpDir) <> ".") And (DirectoryEntryName(tmpDir) <> "..")
          Type$ = "[Directory] "
         ; Size$ = "" ; A directory doesn't have a size
         ; Debug pasta$+"\"+DirectoryEntryName(tmpDir)
          ReadDirectory(pasta$+"\/"+DirectoryEntryName(tmpDir))
         ; Debug Type$ + DirectoryEntryName(tmpDir); + Size$
        EndIf 
      EndIf

      ;Debug Type$ + DirectoryEntryName(tmpDir) + Size$
    Wend
    FinishDirectory(tmpDir)
  EndIf

EndProcedure

ReadDirectory()


Re: Error with algorithm to read folders, subfolders and files inside a directory, can someone help me to improve it?

Posted: Thu Mar 30, 2023 2:39 am
by idle
can you use Getfilelist
viewtopic.php?p=394408

Re: Error with algorithm to read folders, subfolders and files inside a directory, can someone help me to improve it?

Posted: Thu Mar 30, 2023 6:59 am
by infratec

Code: Select all


Procedure ReadDirectory(Directory$)
  
  Directory$ = RTrim(Directory$, #PS$)
  
  tmpDir = ExamineDirectory(#PB_Any, Directory$, "*.*")
  If tmpDir
    While NextDirectoryEntry(tmpDir)
      If DirectoryEntryType(tmpDir) = #PB_DirectoryEntry_File
        arquivosLidos = arquivosLidos + 1
        Type$ = "[File] "
        ; Size$ = " (Size: " + DirectoryEntrySize(tmpDir) + ")"
        ;  Debug Type$ + DirectoryEntryName(tmpDir) + " Pasta : "+RemoveString(Directory$,Dir$)
        Debug RSet(Str(arquivosLidos), 5) + " PathPart: " + Directory$ + DirectoryEntryName(tmpDir)
      Else
        If (DirectoryEntryName(tmpDir) <> ".") And (DirectoryEntryName(tmpDir) <> "..")
          Type$ = "[Directory] "
          ; Size$ = "" ; A directory doesn't have a size
          Debug "Directory: " + Directory$ + #PS$ + DirectoryEntryName(tmpDir)
          ReadDirectory(Directory$ + #PS$ + DirectoryEntryName(tmpDir))
          ; Debug Type$ + DirectoryEntryName(tmpDir); + Size$
        EndIf 
      EndIf
      
      ;Debug Type$ + DirectoryEntryName(tmpDir) + Size$
    Wend
    FinishDirectory(tmpDir)
  EndIf
  
EndProcedure

ReadDirectory(GetHomeDirectory())

Re: Error with algorithm to read folders, subfolders and files inside a directory, can someone help me to improve it?

Posted: Thu Mar 30, 2023 7:35 am
by STARGĂ…TE
skinkairewalker wrote: Thu Mar 30, 2023 1:41 am it works well, but in directories with many files and folders, it crashes at some point saying that:
How did you compile your code snippets? You call ReadDirectory() with a parameter, but you didn't define it.
What is Dir$ ? What is pasta$ ? It is used but not defined. And further, what the hell is "\/" in between?

The issue in your code is, the random generation of tmpDir for the examination.
If a new random number in a sub directory is the same as a number before, IsDirectory(tmpDir) gives true and you generate a new number and then you use this new number and call FinishDirectory(tmpDir). But why? The new number is random, why you call FinishDirectory() on this new number?
Even if you use the old number, it would be wrong, because the old directory is still examined and needed.

See infratec' post for a solution.

Re: Error with algorithm to read folders, subfolders and files inside a directory, can someone help me to improve it?

Posted: Thu Mar 30, 2023 8:30 am
by BarryG

Code: Select all

Debug "PathPart: " + ReplaceString(ReplaceString(RemoveString(Directory$,Dir$),"\","/"),"//","/")+"/"+DirectoryEntryName(tmpDir)
What the heck is this?

Re: Error with algorithm to read folders, subfolders and files inside a directory, can someone help me to improve it?

Posted: Thu Mar 30, 2023 7:08 pm
by skinkairewalker
How amazing, all the codes are incredibly efficient! I am very grateful to all, thank you for helping !!
BarryG wrote: Thu Mar 30, 2023 8:30 am

Code: Select all

Debug "PathPart: " + ReplaceString(ReplaceString(RemoveString(Directory$,Dir$),"\","/"),"//","/")+"/"+DirectoryEntryName(tmpDir)
What the heck is this?
I made a workaround to get just " a part of the path and the file , starting from the root of the main program ...

Re: Error with algorithm to read folders, subfolders and files inside a directory, can someone help me to improve it?

Posted: Thu Mar 30, 2023 7:20 pm
by AZJIO
See more "File operations" section here and here