Page 1 of 1

quick ExamineDirectory question

Posted: Fri Oct 11, 2024 8:11 pm
by noxidderf
Hi everyone, I am a new user here. I am defiantly not a programmer but i can usually figure out how to do what i want, in time, with a few examples. The forums are awesome.

I have a quick question about "ExamineDirectory". I see from some examples how to look at a dir so i am good there.
my question is do I need to create another loop to look for a second extension or just get everything and get what I want from the list ? or is there a better way?

Re: quick ExamineDirectory question

Posted: Fri Oct 11, 2024 9:06 pm
by jacdelad
For multiple extensions I would seek for *.* and filter within the loop, rather than doing two examinations. It is not possible to specify more than one pattern at once.

Re: quick ExamineDirectory question

Posted: Fri Oct 11, 2024 11:20 pm
by kenmo
Try my ScanFolder() includefile, it can take multiple extensions at once, return relative or absolute paths, put the results in a string list, etc.

https://github.com/kenmo-pb/includes/bl ... Folder.pbi

Code: Select all

ScanFolder(Folder.s, Flags.i, Extensions.s)
example

Code: Select all

  ; Text files, excluding hidden, absolute paths
  If ScanFolder(GetHomeDirectory(), #ScanFolder_Absolute | #ScanFolder_NoHidden | #ScanFolder_Recursive, "*.txt")
    Debug "Text files in Home directory:"
    Debug "-----------------------------"
    While NextScanEntry()
      Debug ScanEntryPath()
    Wend
    FinishScan()
  EndIf

Re: quick ExamineDirectory question

Posted: Fri Oct 11, 2024 11:29 pm
by idle
There's also GetFileList
viewtopic.php?t=51878

Code: Select all

EnableExplicit 

Structure FileDate 
  Created.i
  Modified.i
  Accessed.i
EndStructure   

Structure File 
  Name.s
  Attributes.i
  Date.FileDate
  Size.q
EndStructure   

Procedure GetFileList(StartDirectory.s,List Lfiles.file(),Pattern.s="*.*",Recursive=1)
  Protected PatternCount,Depth,a,CurrentDirectoryID,Directory.s,TempDirectory.s
  Protected FileAttributes.i,FileSize.i,FileDate.FileDate,FileName.s,FullFileName.s 
  
  Static NewList Lpattern.s()
  Static PatternSet,FileCount 
  
  If Not PatternSet
    Pattern = RemoveString(Pattern,"*.")
    PatternCount = CountString(Pattern,"|") + 1
    ClearList(lpattern())
    For a = 1 To PatternCount 
      AddElement(Lpattern())
      Lpattern() = UCase(StringField(Pattern,a,"|"))
    Next
    PatternSet=1
  ElseIf depth = 0 
    PatternSet = 0
  EndIf 
  
  CurrentDirectoryID = ExamineDirectory(#PB_Any, StartDirectory, "*.*") 
  If CurrentDirectoryID 
    While NextDirectoryEntry(CurrentDirectoryID)
      If DirectoryEntryType(CurrentDirectoryID) = #PB_DirectoryEntry_File
        Directory = StartDirectory
        FileName = DirectoryEntryName(CurrentDirectoryID)
        FileDate\Created = DirectoryEntryDate(CurrentDirectoryID,#PB_Date_Created)
        FileDate\Modified = DirectoryEntryDate(CurrentDirectoryID,#PB_Date_Modified)
        FileDate\Accessed = DirectoryEntryDate(CurrentDirectoryID,#PB_Date_Accessed)
        FileAttributes = DirectoryEntryAttributes(CurrentDirectoryID)
        FileSize = DirectoryEntrySize(CurrentDirectoryID) 
        
        ForEach Lpattern()
          If lpattern() = "*" Or GetExtensionPart(UCase(FileName)) = lpattern()
            FullFileName.s = StartDirectory + FileName 
            AddElement(LFiles()) 
            Lfiles()\Name = FullFileName
            Lfiles()\Date = FileDate 
            Lfiles()\Size = FileSize 
            Lfiles()\Attributes = FileAttributes 
            FileCount+1
          EndIf
        Next  
        
      Else
        TempDirectory = DirectoryEntryName(CurrentDirectoryID)
        If TempDirectory <> "." And TempDirectory <> ".."
          If Recursive = 1
            Depth + 1
            GetFileList(StartDirectory + TempDirectory + #PS$,LFiles(),Pattern,Recursive) 
          EndIf
        EndIf
      EndIf
    Wend
    FinishDirectory(CurrentDirectoryID)
  EndIf
  
  ProcedureReturn FileCount
  
EndProcedure

Procedure SortFileListByDate(List InputFiles.file(),List OutPutFiles.file(),Order=#PB_Sort_Ascending,DateOption=#PB_Date_Modified,StartDate=0,EndDate=$7FFFFFFF)
  
  Select DateOption 
    Case #PB_Date_Modified 
      SortStructuredList(InputFiles(),Order,(OffsetOf(File\Date)+OffsetOf(FileDate\Modified)),#PB_Integer) 
    Case #PB_Date_Accessed 
      SortStructuredList(InputFiles(),Order,(OffsetOf(File\Date)+OffsetOf(FileDate\Accessed)),#PB_Integer)
    Case #PB_Date_Created
      SortStructuredList(InputFiles(),Order,(OffsetOf(File\Date)+OffsetOf(FileDate\Created)),#PB_Integer)
  EndSelect 
  
  If StartDate 
    ForEach InputFiles() 
      Select DateOption 
        Case #PB_Date_Modified 
          If (InputFiles()\Date\Modified >= StartDate And InputFiles()\Date\Modified <= EndDate) 
            AddElement(OutPutFiles()) 
            CopyStructure(@InputFiles(),@OutPutFiles(),File)
          EndIf
        Case #PB_Date_Accessed 
          If (InputFiles()\Date\Modified >= StartDate And InputFiles()\Date\Accessed <= EndDate) 
            AddElement(OutPutFiles()) 
            CopyStructure(@InputFiles(),@OutPutFiles(),File)
          EndIf
        Case #PB_Date_Created 
          If (InputFiles()\Date\Modified >= StartDate And InputFiles()\Date\Created <= EndDate) 
            AddElement(OutPutFiles()) 
            CopyStructure(@InputFiles(),@OutPutFiles(),File)
          EndIf
      EndSelect  
    Next  
  EndIf
  
  ProcedureReturn ListSize(OutPutFiles()) 
  
EndProcedure   

Procedure SortFileListBySize(List InputFiles.file(),List OutPutFiles.file(),Order=#PB_Sort_Ascending,MinimumSize=0,MaximumSize.q=$7FFFFFFFFFFFFFFF) 
  
  SortStructuredList(InputFiles(),Order,OffsetOf(File\Size),#PB_Integer) 
  If MinimumSize 
    ForEach InputFiles() 
      If (InputFiles()\Size >= MinimumSize And InputFiles()\Size <= MaximumSize)
        AddElement(OutPutFiles()) 
        CopyStructure(@InputFiles(),@OutPutFiles(),File)
      EndIf
    Next    
  EndIf 
  
  ProcedureReturn ListSize(OutPutFiles())  
  
EndProcedure  

Global Path.s = #PB_Compiler_Home  
Global NewList Files.File()  
If GetFileList(Path,Files(),"*.*")  ;get all pb files in the directory recursively   
  ForEach Files() 
      Debug Files()\Name 
  Next 
EndIf   

Re: quick ExamineDirectory question

Posted: Sat Oct 12, 2024 1:10 am
by noxidderf
oh wow, thanks.

thanks for the get file list suggestion.

I went ahead and use the *.* mostly to figure out how things work. I am grateful for the help.
I was having a real beginner. mostly because I am.
but i just started writing things down and got moving.
I am sure I will run into a real speed bump soon enough.

speed bump found.
i thought I would look at GetFileList before i hit post....therr was an error.

I could not find a reference to the getfilelist in the help or PDF's

I installed the ubuntu 6.12 LTS and ran into no problems. very interesting indeed. I will return after soothing my wife for a bit.

regardless, thanks for the info above.

Fred From Florida, who is really tired of being in the path of windy and wet storms.

Re: quick ExamineDirectory question

Posted: Sat Oct 12, 2024 3:38 am
by idle
yes, it would produce an error without the function code. I re posted it in the link above.
seems that strong hurricanes are a multiple yearly event now for Florida.

Re: quick ExamineDirectory question

Posted: Sat Oct 12, 2024 4:42 am
by noxidderf
i was thinking i really must have missed something. i just saw the bright blue link staring at me. I just this min. double checked and saw it. i hate over looking things. thank you.

hopefully the weather will give us a break for a few years.

Re: quick ExamineDirectory question

Posted: Sat Oct 12, 2024 6:48 am
by idle
No worries here, I'm dyslexic and can easily not see things.
I hope the weather will give you a break but I don't think the extremes are going to get better any time soon. :|

Re: quick ExamineDirectory question

Posted: Sat Oct 12, 2024 2:09 pm
by AZJIO
; cmd.exe + Dir -> viewtopic.php?t=82308
; FileSearch + RegExp -> viewtopic.php?p=597194#p597194
; Simple example viewtopic.php?p=617197#p617197
; Recursive viewtopic.php?p=595785#p595785

add it to your code, it might come in handy someday.

Code: Select all

; cmd.exe + Dir -> https://www.purebasic.fr/english/viewtopic.php?t=82308
; FileSearch + RegExp -> https://www.purebasic.fr/english/viewtopic.php?p=597194#p597194
; Simple example https://www.purebasic.fr/english/viewtopic.php?p=617197#p617197
; Recursive https://www.purebasic.fr/english/viewtopic.php?p=595785#p595785
I also want to make a method with a Callback function. That is, a situation when you don’t get the entire list at once, but get one file and do something with it, and only then get the next file. In this case, you can stop the search, for example, if you have found the file you need and do not need to search for the next ones.
For example, I want to do a preview when renaming files. I find only 20 files that are susceptible to change when renaming according to a regular expression or replacing some text in the name. If the preview is satisfactory and the user clicks "OK", then the function renames these files and continues to work. The problem is that if you get the entire list of files at once, it can take a lot of time, for example 5-10 seconds, and time will also be spent on outputting the data.

Re: quick ExamineDirectory question

Posted: Sat Oct 12, 2024 3:28 pm
by useful
Walk_Dir RecursivelyYes or RecursivelyNo
viewtopic.php?p=504824#p504824