It is currently Sat May 25, 2013 7:48 am

All times are UTC + 1 hour




Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: FindFile & CountFile
PostPosted: Mon May 28, 2012 6:31 pm 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Wed Oct 22, 2003 2:51 am
Posts: 734
Location: Canada
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:
; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; 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 <<<<<
; <<<<<<<<<<<<<<<<<<<<<<<


Top
 Profile  
 
 Post subject: Re: FindFile & CountFile
PostPosted: Fri Jun 01, 2012 3:03 am 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Wed Oct 22, 2003 2:51 am
Posts: 734
Location: Canada
Hello everyone,

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

Best regards
Guimauve

Code:
; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; 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 <<<<<
; <<<<<<<<<<<<<<<<<<<<<<<


Top
 Profile  
 
 Post subject: Re: FindFile & CountFile
PostPosted: Fri Jun 01, 2012 12:46 pm 
Online
Addict
Addict
User avatar

Joined: Sun Nov 05, 2006 11:42 pm
Posts: 2512
Location: Lyon - France
Thanks :wink:

_________________
ImageThe happiness is a road...
Not a destination


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: Blankname, kvitaliy and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  

 


Powered by phpBB © 2008 phpBB Group
subSilver+ theme by Canver Software, sponsor Sanal Modifiye