-
Notifications
You must be signed in to change notification settings - Fork 0
/
filesInDir.pl
96 lines (76 loc) · 1.71 KB
/
filesInDir.pl
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
#!/usr/bin/perl
sub filesInDir($);
sub files($)
{
my $dir;
my $dirName = shift || die "Missing directory parameter\n";
opendir(dir, $dirName);
@files = readdir(dir);
return @files;
}
sub getFileSeparator
{
#use File::Util;
#my($f) = File::Util->new();
# TODO: Replace with OS independent value
return "/";
}
sub isDir($)
{
my $fileName = shift || die "isDir called with no file name\n";
if (-d $fileName)
{
print("$fileName is a dir\n");
return 1;
}
return 0;
}
sub filesReccursive($)
{
my $currentFolder = shift;
my @all;
chdir($currentFolder) or die("Cannot access folder $current_folder");
#Get the all files and folders in the given directory.
my @both = glob("*");
my @folders;
foreach my $item (@both)
{
if(-d $item)
{
#Get all folders into another array - so that first the files will appear and then the folders.
push(@folders,$item);
}
else
{
#If it is a file just put it into the final array.
push(@all,$item);
}
}
foreach my $thisFolder (@folders)
{
#Continue calling this function for all the folders
my $full_path = "$currentFolder" . getFileSeparator() . "$thisFolder";
my @deep_items = filesReccursive($full_path); # :RECURSION:
foreach my $item (@deep_items) {
push(@all, "$thisFolder" . getFileSeparator() . "$item");
}
}
return @all;
}
#
# test
#
#print("=============== Shallow ===============\n");
#my $dirParameter = shift || die "Usege: filesInDir <dirName>\n";
#my @foundFiles = files($dirParameter);
#for my $file (@foundFiles)
#{
# print("Found file named: $file\n");
#}
#print("=============== Recursive ===============\n");
#my @all = filesReccursive("$dirParameter");
#foreach my $item (@all)
#{
# print "$item\n";
#}
1;