ListFiles()
Author: Jare
        Added: 9. huhtikuuta 2011 kello 22.05
        Edited: 20. helmikuuta 2021 kello 20.09
        Category: Tiedostot
    
Description
Hakee tiedostot nykyisestä hakemistosta ja alihakemistoista. Tarvittaessa vain tietyn tyyppiset tiedostot.
Edit 2021-02-20 lähes kymmenen vuoden jälkeen:
 - Tehty funktioista yhteensopivia Force Variable Declarationin kanssa.
 - Lisätty parameterit directory, recursive ja reset. Directorylla voit helpommin määrittää, mistä kansiosta haetaan. Funktio oli aiemmin aina rekursiivinen eli sisällytti alikansioiden sisällön - nyt sen voi kytkeä pois. reset = resetoidaanko löydettyjen tiedostojen ja kansioiden lista aluksi. Aiemmin tätä ei tehty.
 - Muutettu tyyppikenttiä: path muuttui muotoon absolute_path ja kaksi uutta kenttää tuli lisäksi: name ja directory.
 - File-tyypin nimeksi muutettu FoundFile.
 - Erotettu löydetyt kansiot tiedostoista erilliseksi tyypiksi FoundFolder.
    Code
Select all1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99  | //TYPE
Type FoundFile
	Field name As String
	Field directory As String
	Field absolute_path As String
EndType
Type FoundFolder
	Field name As String
	Field directory As String
	Field absolute_path As String
	Field isNew As Byte
EndType
//EXAMPLE PROGRAM
SCREEN 1024,768
SetWindow "Loading..."
listFiles("", "cb")
SetWindow ""
Repeat
	y=0
	x=0
	For f.FoundFile = Each FoundFile
		Text x,y, f\absolute_path 
		y+15
		If y > 760 Then
			y = 0
			x + 300
		EndIf
	Next f
	DrawScreen
Forever
//FUNCTIONS
Function listFiles(directory$="", extension$="", recursive=1, reset=1)
	//extension$ = which Type of files To search For. example: "txt" (without Dot). Leave empty If you want To find all files.
	Dim orig_dir$, filename$ // Force Variable Declaration
	Dim f.FoundFile, fo.FoundFolder
	
	If reset Then
		// Forget old files
		For f = Each FoundFile
			Delete f
		Next f
	EndIf
	
	orig_dir$ = CurrentDir()
	If Not IsDirectory(directory) Then Return False
	ChDir directory
	StartSearch
	Repeat
		filename$ = FindFile()
		If IsDirectory(filename)=False And (Lower(Str(getFileExtension(filename))) = extension Or extension = "") Then
			f				= New(FoundFile)
			f\name			= filename
			f\directory		= CurrentDir()
			f\absolute_path = CurrentDir()+filename
		ElseIf (IsDirectory(filename) And filename <> "." And filename <> "..") Then
			fo				= New(FoundFolder)
			fo\name			= filename
			fo\directory	= CurrentDir()
			fo\absolute_path= CurrentDir()+filename
			fo\isNew		= True
		EndIf
	Until filename = ""
	EndSearch
	
	If recursive Then
		For fo = Each FoundFolder
			If (fo\isNew) Then
				fo\isNew = False
				listFiles(fo\absolute_path, extension, True, False)
			EndIf
		Next fo
	EndIf
	
	ChDir orig_dir
	Return True
EndFunction
Function getFileExtension(filename$)
	Dim i, char$, extension$ // Force Variable Declaration
	
	For i = Len(filename) To 1 Step -1
		char$ = Mid(filename, i,1)
		If (char = ".") Then
			Return extension$
		Else
			extension = char + extension
		EndIf
	Next i
	Return "" 'No extension
EndFunction
 | 
Comments
No comments. You can be first!
Leave a comment
            You must be logged in to comment.