-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcopySelectedFiles.pl
71 lines (48 loc) · 1.57 KB
/
copySelectedFiles.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
#!/usr/bin/perl
# Example 11-3 Demonstrate a recursive subroutine to list a subtree of a filesystem
use strict;
use warnings;
use File::Copy::Recursive;
my $folder = '2005';
list_recursively('test');
exit;
################################################################################
# Subroutine
################################################################################
# list_recursively
#
# list the contents of a directory,
# recursively listing the contents of any subdirectories
sub list_recursively {
my($directory) = @_;
my @files = ( );
# Open the directory
unless(opendir(DIRECTORY, $directory)) {
print "Cannot open directory $directory!\n";
exit;
}
# Read the directory, ignoring special entries "." and ".."
#
@files = grep (!/^\.\.?$/, readdir(DIRECTORY));
closedir(DIRECTORY);
# If file, print its name
# If directory, recursively print its contents
# Notice that we need to prepend the directory name!
foreach my $file (@files) {
# If the directory entry is a regular file
if (-f "$directory/$file") {
next;
# If the directory entry is a subdirectory
}elsif( -d "$directory/$file") {
if ($file eq "selected") {
my $file1="$directory/$file";
my $file2="$folder";
File::Copy::Recursive::dircopy $file1, $file2 or die "Copy failed: $!";
}
else {
next;}
}
# Here is the recursive call to this subroutine
list_recursively("$directory/$file");
}
}