-
Notifications
You must be signed in to change notification settings - Fork 0
/
RecurseWork.pm
96 lines (84 loc) · 2.09 KB
/
RecurseWork.pm
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package RecurseWork;
our @EXPORT = ('RecurseWork', 'RecurseWork_DEBUG');
# Call RecurseWork, it will parse all folder and SubFoler and call
# a WorkOnFile for each file in all rep and subrep...
# Here is a proto for WorkOnFile
#
# sub WorkOnFile()
# {
# my $FileName = shift @_;
# my $UserData = shift @_;
#
# work to be done on file
#
# }
# Optionnal function
# sub WorkOnDirectory()
# {
# my $DirName = shift @_;
# my $UserData = shift @_;
#
# work to be done on file
#
# }
# sub EnterDirectory()
# {
# my $DirName = shift @_;
# my $UserData = shift @_;
#
# return 1;
# }
# Be sur not to use the 'RecurseWork_DIR' file handle because it is used by RecurseWork
my $RecurseWork_DEBUG = 0;
sub RecurseWork()
{
my $CurrentFolder = shift @_;
my $UserData = shift @_;
my $CurrentEntry;
my @ListeEntries;
$CurrentFolder =~ s/\/+$//;
print STDERR "$CurrentFolder\n" if $RecurseWork::DEBUG;
if ( defined &main::EnterDirectory )
{
# We can choose if we recurse into directory
if ( &main::EnterDirectory( $CurrentFolder . '/', $UserData ) == 0 )
{
return;
}
}
if ( defined &main::WorkOnDirectory )
{
# print STDERR "Work on '$CurrentEntry'\n" if $RecurseWork::DEBUG;
&main::WorkOnDirectory( $CurrentFolder, $UserData );
}
opendir( RecurseWork_DIR, $CurrentFolder ) or return;
while( $CurrentEntry = readdir(RecurseWork_DIR) )
{
# do not reprocess this folder and parent one...
next if ( $CurrentEntry =~ /^\.\.?$/ );
if ( $CurrentFolder =~ /\/$/ )
{
push @ListeEntries, "$CurrentFolder$CurrentEntry";
}
else
{
push @ListeEntries, "$CurrentFolder/$CurrentEntry";
}
}
closedir( RecurseWork_DIR );
foreach $CurrentEntry ( @ListeEntries )
{
if ( -d $CurrentEntry )
{
# Always recurse... check is done at the begining
&RecurseWork( $CurrentEntry . '/', $UserData );
}
else
{
# it is a file, call WorkOnFile
# print STDERR "Work on '$CurrentEntry'\n" if $RecurseWork::DEBUG;
&main::WorkOnFile( $CurrentEntry, $UserData );
}
}
}
1;