-
Notifications
You must be signed in to change notification settings - Fork 12
/
file-tools.red
65 lines (62 loc) · 1.12 KB
/
file-tools.red
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
Red[
Title: "File tools"
Author: "Boleslav Březovský"
]
match: func [
"Match string to given wildcard pattern (supports ? and *)"
;TODO: escaping for * and ?
value [any-string!]
pattern [any-string!]
/local forward
][
forward: func [][
value: next value
pattern: next pattern
]
value: to string! value
pattern: to string! pattern
until [
switch/default pattern/1 [
#"?" [forward]
#"*" [
unless value: find value first pattern: next pattern [
return false
]
]
][
either equal? value/1 pattern/1 [forward][return false]
]
tail? pattern
]
unless empty? value [return false]
true
]
foreach-file: func [
"Evaluate body for each file in a path"
'file [word!]
path [file!]
body [block!]
/with "Wildcard based pattern file has to confort to"
pattern [any-string!]
/local files f
][
files: read path
foreach f files [
f: rejoin [path f]
either dir? f [
either with [
foreach-file/with :file f body pattern
][
foreach-file :file f body
]
][
if any [
not with
all [with match second split-path f pattern]
][
set :file f
do body
]
]
]
]