File search ends immediately?

Just starting out? Need help? Post your questions and find answers here.
User avatar
Fangbeast
PureBasic Protozoa
PureBasic Protozoa
Posts: 4749
Joined: Fri Apr 25, 2003 3:08 pm
Location: Not Sydney!!! (Bad water, no goats)

File search ends immediately?

Post by Fangbeast »

Thought I had this one right as I was just basically copy some of my earlier code but with my eyesight, you never know.

I feed this routine a full filename and then use StringField to 'peel off' just the first bit I am looking to match and then returning the "Debug "Found close partial match:: " + FileName.s" debug to let me know it found the filename with the partial match.

But, this procedure ends straight away. I used Windows explorer with the same partial string and it can find it so it stands to reason that I don't know what I am doing again:):)

Okay, what did dingo brain forget this time?

Code: Select all

Procedure FindDocument(MissingDocument.s)
  
  SearchTerm.s      = Trim(StringField(MissingDocument.s, 1, "-"))
  
  Debug "Missing document name::    " + MissingDocument.s
  
  Debug "Search term to look for::  " + SearchTerm.s
  
  If MissingDocument.s  <> #Empty$
    
    SearchDirectory.s = PathRequester("Gimme a path bitch", "")
    
    If SearchDirectory.s  <> #Empty$
      
      Debug "Search directory requester:: " + SearchDirectory.s
      
      NewList FoundDirectories.s()
      
      If Right(SearchDirectory.s, 1) = "\"
        
        SearchDirectory.s = Left(SearchDirectory.s, Len(SearchDirectory.s) - 1)
        
      EndIf
      
      AddElement(FoundDirectories.s())
      
      FoundDirectories.s() = SearchDirectory.s
      
      IndexLevel.i = #False
      
      Repeat
        
        SelectElement(FoundDirectories.s(), IndexLevel.i)
        
        DirectoryHandle.i =  ExamineDirectory(#PB_Any, FoundDirectories.s(), "*.pdf")
        
        Debug "Directory handle was retrieved:: " + Str(DirectoryHandle.i)
        
        If DirectoryHandle.i <> #False
          
          Path.s = FoundDirectories.s() + "\"
          
          While NextDirectoryEntry(DirectoryHandle.i)
            
            Filename.s = DirectoryEntryName(DirectoryHandle.i)
            
            Select DirectoryEntryType(DirectoryHandle.i)
                
              Case #PB_DirectoryEntry_File
                
                If FindString(FileName.s, SearchTerm.s, 1, #PB_String_NoCase)
                  
                  Debug "Found close partial match::  " + FileName.s
                  
                EndIf
                
              Case #PB_DirectoryEntry_Directory
                
                Filename.s = DirectoryEntryName(DirectoryHandle.i)
                
                If Filename.s <> ".." And Filename.s <> "."
                  
                  AddElement(FoundDirectories())
                  
                  FoundDirectories() = Path.s + Filename.s
                  
                EndIf
                
            EndSelect
            
          Wend
          
        Else
          
          Debug "Could not get a directory handle, cannot examine anything"
          
        EndIf
        
        IndexLevel.i + 1
        
      Until IndexLevel.i > ListSize(FoundDirectories()) -1
      
    Else
      
      Debug "The search directory was empty"
      
    EndIf
    
    ClearList(FoundDirectories.s())
    
  Else
    
    Debug "Document string was empty"
    
  EndIf
  
EndProcedure

FindDocument("Tandy Electronics - 3 Sets Of Av Cables - Receipt.Pdf")

End
Last edited by Fangbeast on Fri May 07, 2021 9:18 am, edited 1 time in total.
Amateur Radio, D-STAR/VK3HAF
infratec
Always Here
Always Here
Posts: 6869
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: File search ends immediately?

Post by infratec »

You didn't use your search string :wink:

And your ClearList() was at the wrong place.

Code: Select all

Procedure FindDocument(MissingDocument.s)
  
  SearchTerm.s = Trim(StringField(MissingDocument.s, 1, "-"))
  
  Debug "Missing document name:: " + MissingDocument.s
  
  Debug "Search term to look for:: " + SearchTerm.s
  
  If MissingDocument.s <> #Empty$
    
    SearchDirectory.s = PathRequester("Gimme a path bitch", "")
    
    If SearchDirectory.s <> #Empty$
      
      Debug "Search directory requester:: " + SearchDirectory.s
      
      NewList FoundDirectories.s()
      
      If Right(SearchDirectory.s, 1) = "\"
        
        SearchDirectory.s = Left(SearchDirectory.s, Len(SearchDirectory.s) - 1)
        
      EndIf
      
      AddElement(FoundDirectories.s())
      
      FoundDirectories.s() = SearchDirectory.s
      
      IndexLevel.i = #False
      
      Repeat
        
        SelectElement(FoundDirectories.s(), IndexLevel.i)
        
        DirectoryHandle.i = ExamineDirectory(#PB_Any, FoundDirectories.s(), SearchTerm + "*")
        
        Debug "Directory handle was retrieved:: " + Str(DirectoryHandle.i)
        
        If DirectoryHandle.i <> #False
          
          Path.s = FoundDirectories.s() + "\"
          
          While NextDirectoryEntry(DirectoryHandle.i)
            
            Filename.s = DirectoryEntryName(DirectoryHandle.i)
            
            Select DirectoryEntryType(DirectoryHandle.i)
                
              Case #PB_DirectoryEntry_File
                
                If FindString(FileName.s, SearchTerm.s, 1, #PB_String_NoCase)
                  
                  Debug "Found close partial match:: " + FileName.s
                  
                EndIf
                
              Case #PB_DirectoryEntry_Directory
                
                Filename.s = DirectoryEntryName(DirectoryHandle.i)
                
                If Filename.s <> ".." And Filename.s <> "."
                  
                  AddElement(FoundDirectories())
                  
                  FoundDirectories() = Path.s + Filename.s
                  
                EndIf
                
            EndSelect
            
          Wend
          
        Else
          
          Debug "Could not get a directory handle, cannot examine anything"
          
        EndIf
        
        IndexLevel.i + 1
        
      Until IndexLevel.i > ListSize(FoundDirectories()) -1
      
      ClearList(FoundDirectories.s())
      
    Else
      
      Debug "The search directory was empty"
      
    EndIf
    
  Else
    
    Debug "Document string was empty"
    
  EndIf
  
EndProcedure

FindDocument("Tandy Electronics - 3 Sets Of Av Cables - Receipt.Pdf")

User avatar
Fangbeast
PureBasic Protozoa
PureBasic Protozoa
Posts: 4749
Joined: Fri Apr 25, 2003 3:08 pm
Location: Not Sydney!!! (Bad water, no goats)

Re: File search ends immediately?

Post by Fangbeast »

I think I have inhaled too much of Idle's strangely abused goat collection again.

That and the bad things happening to my eyes and memory. Oh well, as long as I don't go blind before I die, I'm okay:):)

Thanks Bernd. Do you think I could see this even after hours of trying?? No! (Not even on a 49 inch ultrawide:):):) (I blame Idle's goats:):)
Amateur Radio, D-STAR/VK3HAF
User avatar
Fangbeast
PureBasic Protozoa
PureBasic Protozoa
Posts: 4749
Joined: Fri Apr 25, 2003 3:08 pm
Location: Not Sydney!!! (Bad water, no goats)

Re: File search ends immediately?

Post by Fangbeast »

Actually, the search term is going to be used in the StringField area where it searches for the searchterm but :

DirectoryHandle.i = ExamineDirectory(#PB_Any, FoundDirectories.s(), "*.pdf") is supposed to be that way just to find pdf files where stringfield will do the partial matches.

Either way, neither yours nor mine works here. Both stop straight away without finding anything. BUGGER BUM POOP TIT BUM NICKERS
Amateur Radio, D-STAR/VK3HAF
User avatar
Fangbeast
PureBasic Protozoa
PureBasic Protozoa
Posts: 4749
Joined: Fri Apr 25, 2003 3:08 pm
Location: Not Sydney!!! (Bad water, no goats)

Re: File search ends immediately?

Post by Fangbeast »

Bernd, I just found if I replaced the *.pdf with *.*, the search works so I don't know what is going on here.

The manual states "A 'Pattern$' like "*.exe" will list only .exe files (and sub-directories ending with .exe if any)" so my *.pdf should have worked right? To find pdf files only? Only, it doesn't.
Amateur Radio, D-STAR/VK3HAF
User avatar
idle
Always Here
Always Here
Posts: 5093
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: File search ends immediately?

Post by idle »

what happens when you try this assuming you have random >PDF files in downloads, cause your brainy and read papers rather than watch goat porn. :P

Code: Select all

Directory$ = GetUserDirectory(#PB_Directory_Downloads) ; Lists all files and folder in the home directory
  If ExamineDirectory(0, Directory$, "*.pdf")  
    While NextDirectoryEntry(0)
      If DirectoryEntryType(0) = #PB_DirectoryEntry_File
        Type$ = "[File] "
        Size$ = " (Size: " + DirectoryEntrySize(0) + ")"
      Else
        Type$ = "[Directory] "
        Size$ = "" ; A directory doesn't have a size
      EndIf
      
      Debug Type$ + DirectoryEntryName(0) + Size$
    Wend
    FinishDirectory(0)
  EndIf
User avatar
Fangbeast
PureBasic Protozoa
PureBasic Protozoa
Posts: 4749
Joined: Fri Apr 25, 2003 3:08 pm
Location: Not Sydney!!! (Bad water, no goats)

Re: File search ends immediately?

Post by Fangbeast »

idle wrote: Fri May 07, 2021 11:16 pm what happens when you try this assuming you have random >PDF files in downloads, cause your brainy and read papers rather than watch goat porn. :P

Code: Select all

Directory$ = GetUserDirectory(#PB_Directory_Downloads) ; Lists all files and folder in the home directory
  If ExamineDirectory(0, Directory$, "*.pdf")  
    While NextDirectoryEntry(0)
      If DirectoryEntryType(0) = #PB_DirectoryEntry_File
        Type$ = "[File] "
        Size$ = " (Size: " + DirectoryEntrySize(0) + ")"
      Else
        Type$ = "[Directory] "
        Size$ = "" ; A directory doesn't have a size
      EndIf
      
      Debug Type$ + DirectoryEntryName(0) + Size$
    Wend
    FinishDirectory(0)
  EndIf
The problem with the pb code is that if there is *.pdf as a pattern and there is actually a pdf in the directory, it will list it. With the code I posted, if I use *.pdf, and there is NO PDF in the directory being examined, it stops dead a the first directory, doesn't go any further.

While NextDirectoryEntry(DirectoryHandle.i) seems to just quit at the very first directory if there is no matching file pattern.

On the other hand, if I use *.*, it will continue the whole code that I posted going through the very first directory selected to the last directory in the chain.

It's as if there is a bug in this directory code and the pattern works just once and quits. Sounds like a bug to my uneducated mind.
Amateur Radio, D-STAR/VK3HAF
collectordave
Addict
Addict
Posts: 1309
Joined: Fri Aug 28, 2015 6:10 pm
Location: Portugal

Re: File search ends immediately?

Post by collectordave »

Hi

Did this get resolved?

There is a file search routine on the forum where you can search for say *.pdf and it will find all the *.pdf files in all folders including sub folders.

I use it to search for files. I t produces a list of filenames for you so if you then need to search for a partial file name ending with .PDF you can iterate the list.
Any intelligent fool can make things bigger and more complex. It takes a touch of genius — and a lot of courage to move in the opposite direction.
collectordave
Addict
Addict
Posts: 1309
Joined: Fri Aug 28, 2015 6:10 pm
Location: Portugal

Re: File search ends immediately?

Post by collectordave »

Just in case here is the code for the search routine:-

Code: Select all

Procedure SearchDirectory(dir$, pattern$, List dList.s(), level.l = 0)
  Protected eName$
  NewList Dirs.s()
  
  Static FileCount.i
  
  
  If (level = 0)
    ClearList(dList())
  EndIf
 
  If Right(dir$, 1) <> #PS$
    dir$ + #PS$
    
  EndIf
 
  If ExamineDirectory(0, dir$, "")
    While NextDirectoryEntry(0)
      If DirectoryEntryType(0) = #PB_DirectoryEntry_Directory
        eName$ = DirectoryEntryName(0)
        If (eName$ <> ".") And (eName$ <> "..")
          AddElement(Dirs())
          Dirs() = eName$ + #PS$
        EndIf
      EndIf
    Wend
    FinishDirectory(0)
   
    If ExamineDirectory(0, dir$, pattern$)
      While NextDirectoryEntry(0)
        eName$ = DirectoryEntryName(0)
        If (eName$ <> ".") And (eName$ <> "..")
          AddElement(dList())
          FileCount = FileCount + 1
          dList() = dir$ + eName$
          If DirectoryEntryType(0) = #PB_DirectoryEntry_Directory
            dList() + "\"
          EndIf
        EndIf
      Wend
      FinishDirectory(0)
    EndIf
  EndIf
  
 ; SetGadgetText(LoadProgress::txtTotal,Str(FileCount) + " Songs Found")
 ; While WindowEvent():Wend
  
  If ListSize(Dirs())
    ForEach Dirs()
      SearchDirectory(dir$ + Dirs(), pattern$, dList(), level + 1)
    Next
  EndIf
 
  If (level = 0)
    ForEach dList()
      dList() = Mid(dList(), Len(dir$) + 1, Len(dList()))
    Next
    SortList(dList(), 2)
  EndIf
 
EndProcedure
You can it like this

Code: Select all

 
 SearchDirectory(MediaFolder, "*.ogg", SongFiles())
 
MediaFolder if the full path to the top folder to search.

SongFiles() is the list for the routine to fill.

Hope this helps?
Any intelligent fool can make things bigger and more complex. It takes a touch of genius — and a lot of courage to move in the opposite direction.
Post Reply