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