Login Register
Frontpage Code library Pastebin

ListFiles()

Author: Jare
Added: 9.4.2011 22:05
Edited: 15.4.2011 22:19
Category: Tiedostot

Description

Hakee tiedostot nykyisestä hakemistosta ja alihakemistoista. Tarvittaessa vain tietyn tyyppiset tiedostot.

Code

Select all
 1
 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
//TYPE

Type File
	Field path As String
	Field isFolder As Byte
	Field isNew As Byte
EndType


//EXAMPLE PROGRAM
SCREEN 1024,768

SetWindow "Loading..."
listFiles("cb")
SetWindow ""

Repeat
	y=0
	x=0
	For f.File = Each File
		Text x,y, f\path 
		y+15
		If y > 760 Then
			y = 0
			x + 300
		EndIf
	Next f
	DrawScreen
Forever


//FUNCTIONS

Function listFiles(extension$="")
	//extension$ = which Type of files To search For. example: "txt" (without Dot). Leave empty If you want To find all files.
	orig_dir$ = CurrentDir()
	StartSearch
	foundFiles = 0
	Repeat
		filename$ = FindFile()
		If IsDirectory(filename)=False And (Lower(Str(getFileExtension(filename))) = extension Or extension = "") Then
			f.File		= New(File)
			f\path		= CurrentDir()+filename
			f\isFolder	= False
			f\isNew		= True
			foundFiles+1
		ElseIf (IsDirectory(filename) And filename <> "." And filename <> "..") Then
			f.File		= New(File)
			f\path		= CurrentDir()+filename
			f\isFolder	= True
			f\isNew		= True
			foundFiles+1 'although this is a directory (Not a file), we will COUNT it As a file
		EndIf
	Until filename = ""
	EndSearch
	
	If foundFiles > 0 Then
		For f.File = Each File
			If (f\isNew) Then
				f\isNew = False
				If (f\isFolder) Then
					ChDir f\path
					listFiles(extension)
				EndIf
			EndIf
		Next f
	EndIf
	
	ChDir orig_dir
EndFunction

Function getFileExtension(filename$)
	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.