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

Just starting out? Need help? Post your questions and find answers here.
User avatar
skinkairewalker
Enthusiast
Enthusiast
Posts: 627
Joined: Fri Dec 04, 2015 9:26 pm

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

Post 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()

User avatar
idle
Always Here
Always Here
Posts: 5038
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

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

Post by idle »

can you use Getfilelist
viewtopic.php?p=394408
infratec
Always Here
Always Here
Posts: 6817
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

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

Post 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())
User avatar
STARGÅTE
Addict
Addict
Posts: 2067
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

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

Post 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.
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
BarryG
Addict
Addict
Posts: 3292
Joined: Thu Apr 18, 2019 8:17 am

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

Post by BarryG »

Code: Select all

Debug "PathPart: " + ReplaceString(ReplaceString(RemoveString(Directory$,Dir$),"\","/"),"//","/")+"/"+DirectoryEntryName(tmpDir)
What the heck is this?
User avatar
skinkairewalker
Enthusiast
Enthusiast
Posts: 627
Joined: Fri Dec 04, 2015 9:26 pm

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

Post 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 ...
AZJIO
Addict
Addict
Posts: 1312
Joined: Sun May 14, 2017 1:48 am

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

Post by AZJIO »

See more "File operations" section here and here
Post Reply