Finds File Path under Root directory


Tweet blitzmax file-utilities rated
(Posted 3 weeks ago) hardcoal

Hi.. another small contribution...

This process finds a file under a path you give it..
it only brings one reply.. but it can easily can be modified to return all found paths..
I just dont need that atm so I didnt do it.

The Var in the function Called "SerialNumber" means its number in the search..
so if its 1, it will return the first path he finds.. if its 2 than the second.. and so on..
I just didnt have a better name for it ;)


Strict

'Example
 Print FindFilePath("test.bmx")

'----------------Function----------------;

    Global SerialCounter

    Function FindFilePath:String(Filename:String, RootURL:String = "", SerialNumber = 1)
        If Trim(Filename) = "" Then Return
        SerialCounter = 0
        Filename = Trim(Lower(Filename))
        If Trim(RootURL) = "" Then RootURL = CurrentDir() + "\"
        AddSlashToURLIfNeeded(RootURL)
        Return FindFilePath_Req_Int(RootURL, Filename, SerialNumber)
    End Function

            Function FindFilePath_Req_Int:String(RootURL:String = "", Filename:String, SerialNumber = 1)
                Local FileFound_Flg

                If FileExists(RootURL + Filename) Then FileFound_Flg = True ; SerialCounter = SerialCounter + 1

                If FileFound_Flg = True And SerialNumber = SerialCounter Then
                    Return RootURL + Filename
                Else
                    Local DirList:TList, Str:String, Answer:String
                    DirList = ReadDirectory(RootURL)
                    For Str = EachIn DirList
                        If IsFolder(RootURL + Str + "\") Then
                            Answer = FindFilePath_Req_Int(RootURL + Str + "\", Filename, SerialNumber)
                            If Answer <> "" Then Return Answer
                        End If
                    Next
                End If

            End Function

'----------Externals-----------'

        Function AddSlashToURLIfNeeded:String(URL:String)
            If IsSlash(Right(URL, 1)) = False Then URL = URL + "\"  'Adds \ if needed
            Return URL
        End Function

        Function FileExists(URLAndFilename:String = "")  'change name to fileexistsinfolder
            Local TempStream:TStream
            TempStream = OpenStream(URLAndFilename, False, False)
            If TempStream <> Null Then
                CloseStream(TempStream)
                Return True
            End If  
        End Function

        Function ReadDirectory:TList(URL:String, RemoveExtention = False)
            Local ALIST:TList = CreateList(), T:String, Files:String[]

            Files = LoadDir(URL)

            For T:String = EachIn Files

                If RemoveExtention = False Then
                    ListAddLast(ALIST, T)
                Else
                    ListAddLast(ALIST, StripAll(T))
                End If

            Next

            Return AList
        End Function

        Function IsFolder(URL:String)   'works with or without slash
            If ReadDir(URL) > 0 Then Return True
        End Function

        Function IsSlash(Char:String)
            Return char = "/" Or char = "\"
        End Function
(Posted 3 weeks ago) RonTek commented:

Nice! now you should add the tags code-archive, blitzmax and file-utilities so it will be included in the code archives.

(Posted 3 weeks ago) hardcoal commented:

Did i do it right? I mean the Tag Thing?

(Posted 3 weeks ago) RonTek commented:

Reply To Topic (minimum 10 characters)

Please log in to reply