Most games and applications require data from the end user's harddrive; when developing such software in DBPRO, you may frequently need to lookup all of the files in a certain directory with a certain file extension, a feature which is not available in DBPRO's standard file querying commands.
This snippet can be used to query files by one or more extensions, and can limit the quantity of files permitted.
It is used by first clearing any previous file query using ClearFileQuery(). The file query is then executed by supplying the folder path, comma seperated file extensions, and file count limit.
The snippet creates a list of files using an array called FileQuery(), which contains file names and extensions.
A folder path is required, and the command will not search sub folders. The file extensions need not be prefixed with a dot delimiter, but should. The extension query is actually a lookup of file names ending with the comma seperated parameters, therefore it could be used to search for more than just the file extensions. You can default the file count limit by passing a negative count such as -1; the default file limit is 99999999.
The snippet also contains handy file expressions such as IsImageFile() which checks the file extension for an image format, and GetFolder$() which returns the folder of a filepath.
After a filequery is complete, you should use ClearFileQuery() unless you wish to append addition files to the query.
`======================
// File Queries by Chris Tate : chris.tate@binarymodular.com
`======================
Type FileType
FileName$
Path$
File$
Ext$
Ref
Name$
Endtype
//================================================================
Function SetupFiles()
Global Dim FileQuery(0) as FileType
Global FileQueryCount
Endfunction
//================================================================
Function GetFileName$(file$)
` Produce script compatible file key
If file$ <> ""
If InStr(file$,"\")
file$ = Mid$( file$, Last InStr( file$, "\") + 1, Fast Len(file$))
Endif
If InStr(file$,"/")
file$ = Mid$( file$, Last InStr( file$, "/") + 1, Fast Len(file$))
Endif
EndIf
Endfunction file$
//================================================================
Function FolderContainsFiles(sSourceFolder$,sCSV_Extensions$)
sOldDir$ = Get Dir$() : Set Dir sSourceFolder$
Find First
Split String sCSV_Extensions$, ","
t = Get File Type()
While t => 0
//----------------------------
If t = 0
f$ = Get File Name$()
If Fast Left$(f$,1) <> "."
//----------------------------
For i = 1 to Split Count()
If EndsWith( f$, Split Word$(i), 0)
Set Dir sOldDir$
ExitFunction 1
EndIf
Next i
//----------------------------
Endif
EndIf
//----------------------------
Find Next
t = Get File Type()
Endwhile
Set Dir sOldDir$
Endfunction 0
//================================================================
Function ClearFileQuery()
Empty Array FileQuery() : Array Insert At Bottom FileQuery()
Endfunction
//================================================================
Function GetFolder$(file$)
If file$ = ""
//Error("No folder returned due to null value file path")
ExitFunction ""
Endif
s$ = ""
` If in a full path, find the folder
If InStr(file$, "\")
s$ = Mid$( file$, 1, Last InStr(file$, "\") )
Else
//----------------------------
If InStr(file$, "/")
If Split Count() > 0
Split String file$, "/" : n = Split Count()-1
If n => 0
For i = 0 to n
s$ = s$ + Split Word$(i) +"\"
Next i
Else
s$ = file$
EndIf
EndIf
EndIf
EndIf
//----------------------------
s$ = SetEnd$(s$, "\", 0 )
Endfunction s$
//================================================================
Function SetEnd$(text$, suffix$, appendIfEmpty)
If text$ = ""
If appendIfEmpty
text$ = suffix$
Endif
Else
If Fast Right$(text$, Fast Len(suffix$)) <> suffix$
text$ = text$ + suffix$
Endif
Endif
Endfunction text$
//================================================================
Function QueryFiles(sSourceFolder$,sCSV_Extensions$,iLimit)
If sSourceFolder$ = ""
ExitFunction 0
Endif
If Path Exist(sSourceFolder$) = 0
//Error("Could not run query because the path is invalid: " + sSourceFolder$ )
ExitFunction 0
Endif
tm = Timer()
Local sOldDir$
sSourceFolder$ = RequireSuffix$(sSourceFolder$,"\","/")
sOldDir$ = Get Dir$() : Set Dir sSourceFolder$
If iLimit <= 0
iLimit = 99999999
Endif
Local iCount
Find First
Split String sCSV_Extensions$, ","
t = Get File Type()
While t => 0
//----------------------------
If iCount => iLimit
Set Dir sOldDir$
//Error("Found " + Str$(iCount) +" files. The maximum number of files requested is " + Str$(iLimit))
ExitFunction iCount
EndIf
If t = 0
f$ = Get File Name$()
If Fast Left$(f$,1) <> "."
//----------------------------
For i = 1 to Split Count()
If EndsWith( f$, Split Word$(i), 0)
Array Insert At Bottom FileQuery()
FileQuery().Path$ = sSourceFolder$
FileQuery().File$ = sSourceFolder$ + f$
FileQuery().FileName$ = f$
dp = InStr( f$, ".")
ep = Fast Len( f$ ) - dp
FileQuery().Ext$ = Mid$( f$, dp, ep + 1 )
FileQuery().Name$ = Mid$( f$, 1, dp - 1 )
Inc iCount
Endif
Next i
//----------------------------
Endif
EndIf
//----------------------------
Find Next
t = Get File Type()
Endwhile
Set Dir sOldDir$
tm = Timer() - tm
FileQueryCount = iCount
Endfunction iCount
//==================================================
Function FreeFile(f$)
If f$ <> ""
If File Exist(f$)
Delete File f$
ExitFunction 2
Endif
ExitFunction 1
EndIf
Endfunction 0
//=======================================================
Function IsImageFile( n$ )
n$ = Fast Lower$(n$)
If Fast Right$(n$,4) = ".png" Then ExitFunction 1
If Fast Right$(n$,4) = ".dds" Then ExitFunction 1
If Fast Right$(n$,4) = ".jpg" Then ExitFunction 1
If Fast Right$(n$,4) = ".bmp" Then ExitFunction 1
If Fast Right$(n$,4) = ".gif" Then ExitFunction 1
If Fast Right$(n$,4) = ".tga" Then ExitFunction 1
If Fast Right$(n$,4) = ".tif" Then ExitFunction 1
If Fast Right$(n$,5) = ".jpeg" Then ExitFunction 1
Endfunction 0
//================================================================
Function EndsWith(sText$, sEnd$, iCaseSensitive)
If sText$ <> ""
l = Fast Len(sEnd$)
If Fast Right$( sText$, l ) = sEnd$ Then ExitFunction 1
If iCaseSensitive = 0
If Fast Right$( Fast Lower$(sText$), l ) = Fast Lower$(sEnd$) Then ExitFunction 1
Endif
Endif
Endfunction 0
//================================================================
Function RequireSuffix$( sString$, sSuffix$, sOrOtherSuffix$ )
s$ = Fast Lower$(sString$)
If sSuffix$ <> ""
l = Fast Len( sSuffix$ )
If Fast Right$(s$, l) <> Fast Lower$(sSuffix$)
sString$ = sString$ + sSuffix$
Endif
Else
If sOrOtherSuffix$ <> ""
l = Fast Len( sOrOtherSuffix$ )
If Fast Right$(s$, l) <> Fast Lower$(sOrOtherSuffix$)
sString$ = sString$ + sOrOtherSuffix$
Endif
EndIf
EndIf
Endfunction sString$
</div>