0: #!/usr/bin/perl -w
1: #
2: # This peice of code recursivly searches directories
3: # untill it hits the end of its tree for files that
4: # have been modified in the last 10 days. In this case
5: # Im only searching for mp3 files....but that can easily
6: # be hacked out.
7: #
8: # It then symlinks the final directory of the tree from where
9: # the file that was modified lately into your $ndir. It then
10: # creates another text file that specifies exactly what files
11: # were updated lately in that directory that was symlinked. Hrm
12: # I think thats it...
13: #
14: # This was done so I could move my rips out of incoming/
15: # as soon as they were put in there, and still know what
16: # I had uploaded in the past few days. ( Previously I kept
17: # them in the incoming/ directory till I Made sure they were
18: # all ok rips.
19: #
20: # Mark Thomas <mark@cidera.com>
21:
22: use strict;
23:
24: # This is the directory you want to begin your recursive search
25: my $mdir = "/home/ackers";
26:
27: # Where the symlinks and files will be created.
28: my $ndir = "/home/ackers/new";
29:
30: # These are directories you want excluded from the search.
31: my @dont = ("$mdir/pub","$mdir/software","$mdir/stuff",
32: "$mdir/.ssh","$mdir/.BitchX","$mdir/.ncftp",$ndir);
33:
34: my($subdirz,@subdirs,$all,$wank,$orig,@moded_files,$m_files,$mdirs,$mfiles,%data,$newfd);
35:
36:
37: # We first unlink all the current files in $ndir
38: opendir (NEW, "$ndir") or die "$! $ndir dont exist!!!\n";
39: foreach $newfd (readdir(NEW))
40: {
41:
42: next if $newfd =~ /^\./; # We dont want "."'s and ".."'s
43: my $fullnewfd = "$ndir/$newfd";
44: unlink ($fullnewfd) or die "cant unlink $newfd!\n";
45:
46: }
47:
48:
49: readdirz($mdir);
50:
51: foreach $subdirz (@subdirs)
52: {
53:
54: readdirz($subdirz);
55:
56: }
57:
58: # we now have all our directories and sub's
59: # in an array. Now to stat each file in those
60: # dirs.
61: foreach $all (@subdirs)
62: {
63:
64: opendir (SUBDIR, $all);
65:
66: foreach $wank (readdir(SUBDIR)) {
67: $orig = $wank;
68: $wank = "$all/$wank";
69:
70: if (-f $wank)
71: {
72: next if $wank !~ /\.mp3/; # Next if the file isnt an mp3.
73: my ($dev,$ino,$mode,$nlink,$uid,$gid,
74: $rdev,$size,$atime,$mtime,$ctime,
75: $blksize,$blocks) = stat $wank;
76: my $curr = time;
77: my $diff = $curr - $mtime;
78:
79: if ($diff <= 864000)
80: {
81: push(@moded_files, $wank);
82: }
83: }
84: } closedir(SUBDIR);
85:
86: }
87:
88: # We now have a listing of modified or created
89: # files within the last 10 days.
90: foreach $m_files (@moded_files)
91: {
92:
93: # match everything up to last "/" then match
94: # everything after that and push our matches
95: # into a hash of lists.
96: $m_files =~ s/^(.*)\/(.*?\.*)$//g;
97: push @{$data{$1}},$2;
98:
99: }
100: foreach $mdirs ( keys %data )
101: {
102:
103: my $mdirs2 = $mdirs;
104: $mdirs2 =~ s/$mdir//g; #- Pure athstetics, get rid of the $mdir
105: $mdirs2 =~ s/\// \\ /g; #- turn /'s into \'s so we can symlink
106: # and keep good organazation.
107: my $sym = "$ndir/$mdirs2"; #- need to make the mdir2 a full path for
108: # the acctuall symlink.
109: symlink $mdirs, $sym; #- the symlink :)
110: open(LISTING, ">$sym.updated.txt");
111: foreach $mfiles (@{$data{$mdirs}})
112: {
113: print LISTING "\t$mfiles\n";
114: }
115: close(LISTING);
116: }
117: sub readdirz
118: {
119:
120: my($dir_to_read) = @_;
121: my($dirz,$excluded_dir);
122: opendir (DIR, "$dir_to_read") or print "DOH $! $dir_to_read\n";
123: foreach $dirz (readdir(DIR))
124: {
125: $dirz = "$dir_to_read/$dirz" ;
126: next if $dirz =~ /\./;
127: # This is the part that makes it exclude directories
128: # were not allowed to access defined in the @dont array
129: # at the top.
130: foreach $excluded_dir (@dont)
131: {
132: if ($dirz =~ /$excluded_dir/)
133: {
134: $dirz = "";
135: }
136: }
137: next if $dirz eq "";
138: if (-d $dirz)
139: {
140: push (@subdirs, $dirz);
141: }
142:
143: } closedir(DIR);
144:
145: }
In reply to Recursive New Files Generation Thing by cleen
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |