FindFile & CountFile

Share your advanced PureBasic knowledge/code with the community.
User avatar
Guimauve
Enthusiast
Enthusiast
Posts: 742
Joined: Wed Oct 22, 2003 2:51 am
Location: Canada

FindFile & CountFile

Post by Guimauve »

Hello everyone,

A serious update from a code found in Code Archives
FindFile.pb

I need this function for my project, I'm not the original author so I return the code back to all of you.

Best regards
Guimauve

Code: Select all

; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; Project name : FindFile()
; File Name : FindFile.pb
; File version: 1.0.1
; Programming : OK
; Programmed by : Guimauve
; Date : 28-05-2012
; Last Update : 28-05-2012
; PureBasic code : 4.61
; Platform : Windows, Linux, MacOS X
; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; Notes :
;
; The "FindFile()" original code has been created by dige 
; and extended + updated for PB 4.00 by Andre
; Date: 03. February 2005
; 
; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

Procedure.s FindFile(Directory.s, FileName.s)
  
  CompilerIf #PB_Compiler_OS = #PB_OS_Windows 
    
    If Right(Directory, 1) <> "\"
      Directory + "\"
    EndIf
    
  CompilerElse
    
    If Right(Directory, 1) <> "/"
      Directory + "/"
    EndIf
    
  CompilerEndIf

  DirectoryHandle = ExamineDirectory(#PB_Any, Directory, "*.*") 
  
  If DirectoryHandle <> 0
    
    While NextDirectoryEntry(DirectoryHandle) 
      
      EntryName.s = DirectoryEntryName(DirectoryHandle)
      
      If EntryName <> "." And EntryName <> ".." 
        
        Select DirectoryEntryType(DirectoryHandle)
            
          Case #PB_DirectoryEntry_File
            
            If EntryName = FileName
              Output.s = Directory
              Break
            Else
              Output = ""
            EndIf
            
          Case #PB_DirectoryEntry_Directory 
            Output = FindFile(Directory + EntryName, FileName)
            If Output <> ""
              Break
            EndIf
     
        EndSelect
        
      EndIf
      
    Wend

    FinishDirectory(DirectoryHandle)
    
  EndIf
  
  ProcedureReturn Output
EndProcedure 

; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< 
; <<<<< !!! WARNING - YOU ARE NOW IN A TESTING ZONE - WARNING !!! <<<<< 
; <<<<< !!! WARNING - THIS CODE SHOULD BE COMMENTED - WARNING !!! <<<<< 
; <<<<< !!! WARNING - BEFORE THE FINAL COMPILATION. - WARNING !!! <<<<< 
; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< 

Procedure.b BuiltPathDirectory(Path.s)
  
  CompilerSelect #PB_Compiler_OS
      
    CompilerCase #PB_OS_Windows 
      PathSep.s = "\"
      
    CompilerCase #PB_OS_Linux
      PathSep.s = "/"
      
    CompilerCase #PB_OS_MacOS
      PathSep.s = "/"
      
  CompilerEndSelect
  
  DirectoryQty = CountString(Path, PathSep) + 1
  
  For Index = 1 To DirectoryQty
    
    Directory.s = Directory + StringField(Path, Index, PathSep) + PathSep
    
    If FileSize(Directory) = -1
      CreateDirectory(Directory)
    EndIf 
    
  Next
  
  If FileSize(Directory) = -2
    Success.b = #True
  Else
    Success = #False
  EndIf
  
  ProcedureReturn Success
EndProcedure

CompilerSelect #PB_Compiler_OS
    
  CompilerCase #PB_OS_Windows 
    #PathSep = "\"
    
  CompilerCase #PB_OS_Linux
    #PathSep = "/"
    
  CompilerCase #PB_OS_MacOS
    #PathSep = "/"
    
CompilerEndSelect

; Here we set a valid path in two parts, we want 
; to start the search far away from the file.

Path.s = GetHomeDirectory() + "Test Folder" + #PathSep
FullPath.s = Path + "SubFolder" + #PathSep + "SubSubFolder" + #PathSep

; Here we set the filename
FileName.s = "A simple text file.txt"

; Here we built an existing path

If BuiltPathDirectory(FullPath)
  
  ; we create a basic text file for testing
  
  If CreateFile(0, FullPath + FileName)
    
    WriteStringN(0, "This is a simple test !")
    WriteStringN(0, "This file and folders can be deleted !")
    CloseFile(0)
    
  EndIf
  
  ; Then we try to find where the file is to test 
  ; the FindFile() function.
  
  Debug "; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<"
  Debug "; Search Start form : " + Path
  Debug "; FindFile() should return an empty string"
  Debug ""
  Debug FindFile(Path, "A" + FileName) + "     <-------- Did you see something ? "
  Debug ""
  Debug "; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<"
  Debug "; Search Start form : " + Path
  Debug "; FindFile() should return : " + FullPath
  Debug ""
  Debug FindFile(Path, FileName)  
  Debug ""
  Debug "; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<"
  Debug "; Search Start form : " + GetHomeDirectory()
  Debug "; FindFile() should return : " + FullPath
  Debug ""
  Debug FindFile(GetHomeDirectory(), FileName)  
  
EndIf

; <<<<<<<<<<<<<<<<<<<<<<<
; <<<<< END OF FILE <<<<<
; <<<<<<<<<<<<<<<<<<<<<<<
User avatar
Guimauve
Enthusiast
Enthusiast
Posts: 742
Joined: Wed Oct 22, 2003 2:51 am
Location: Canada

Re: FindFile & CountFile

Post by Guimauve »

Hello everyone,

Another command update form code archive. If this command can bu useful for someone else.

Best regards
Guimauve

Code: Select all

; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; Project name : CountFile
; File Name : CountFile.pb
; File version: 1.0.0
; Programming : OK
; Programmed by : Guimauve
; Date : 31-05-2012
; Last Update : 31-05-2012
; PureBasic code : 4.61
; Platform : Windows, Linux, MacOS X
; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; Notes :
;
; The "CountFile()" original code has been created by benny 
; and updated for PB 4.00 by HeX0R
; Date: 09. June 2005 
; 
; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

Procedure.l CountFile(Directory.s)
  
  CompilerIf #PB_Compiler_OS = #PB_OS_Windows 
    
    If Right(Directory, 1) <> "\"
      Directory + "\"
    EndIf
    
  CompilerElse
    
    If Right(Directory, 1) <> "/"
      Directory + "/"
    EndIf
    
  CompilerEndIf
  
  DirectoryHandle = ExamineDirectory(#PB_Any, Directory, "*.*") 
  
  If DirectoryHandle <> 0
    
    While NextDirectoryEntry(DirectoryHandle) 
      
      EntryName.s = DirectoryEntryName(DirectoryHandle)
      
      If EntryName <> "." And EntryName <> ".." 
        
        Select DirectoryEntryType(DirectoryHandle)
            
          Case #PB_DirectoryEntry_File
            FileCount + 1
            
          Case #PB_DirectoryEntry_Directory 
            FileCount + CountFile(Directory + EntryName)

        EndSelect
        
      EndIf
      
    Wend
    
    FinishDirectory(DirectoryHandle)
    
  EndIf
  
  ProcedureReturn FileCount
EndProcedure 

; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< 
; <<<<< !!! WARNING - YOU ARE NOW IN A TESTING ZONE - WARNING !!! <<<<< 
; <<<<< !!! WARNING - THIS CODE SHOULD BE COMMENTED - WARNING !!! <<<<< 
; <<<<< !!! WARNING - BEFORE THE FINAL COMPILATION. - WARNING !!! <<<<< 
; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< 

Debug CountFile(GetHomeDirectory())

; <<<<<<<<<<<<<<<<<<<<<<<
; <<<<< END OF FILE <<<<<
; <<<<<<<<<<<<<<<<<<<<<<<
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5494
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: FindFile & CountFile

Post by Kwai chang caine »

Thanks :wink:
ImageThe happiness is a road...
Not a destination
Post Reply