0: =head1 DESCRIPTION
1:
2: Hello Monks,
3: I figure the only way I'm going to get better at perl is
4: by writing code and getting comments on it, so here is a
5: module to scratch a itch I had.
6:
7: I've got lots of Mp3s. More that one CDs worth anyway.
8: So I needed something to separate them into CD sized directories.
9: So here is my latest offering.
10:
11:
12: package Mp3::Backup;
13: use strict;
14:
15: BEGIN {
16: use Exporter ();
17: use vars qw ($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
18: $VERSION = 0.01;
19: @ISA = qw (Exporter);
20: #Give a hoot don't pollute, do not export more than needed by default
21: @EXPORT = qw ();
22: @EXPORT_OK = qw ();
23: %EXPORT_TAGS = ();
24: }
25:
26: use File::Find;
27: use File::Copy;
28: use Data::Dumper;
29:
30:
31: sub new {
32: my $class = shift;
33:
34: my $ref = {
35: MP3DIR => shift, # The directory to backup
36: BACKUPDIR => shift, # Where to backup the mp3s
37: MAXSIZE => (shift) * 1024 * 1024, # Size in kilobyes each disk should be
38: DEBUG => shift || 0, # Set debug status;
39: FILELIST => {}, # Hash of all mp3s
40: };
41:
42: my $self = bless ($ref, ref ($class) || $class);
43:
44: return ($self);
45: }
46:
47: # The sub builds the hash of mp3s to be backed up
48: sub buildFileList {
49: my $class = shift;
50:
51: find(
52: sub {
53:
54: return unless ($File::Find::name =~ /\.mp3$/); # Only mp3s
55: $class->{FILELIST}{$_}{PATH} = $File::Find::name;
56: $class->{FILELIST}{$_}{SIZE} = -s $File::Find::name;
57: },
58: $class->{MP3DIR}
59:
60: );
61:
62: $class->_dump if ($class->{DEBUG} == 1); # Just to check what you are getting
63:
64: }
65:
66: # Private method
67: sub _dump {
68: my $class = shift;
69: print Dumper $class->{FILELIST}; # Print to STDOUT the contents of the hash
70: }
71:
72: # Build backup
73: sub fullBackup {
74: my $class = shift;
75: my $size = 0;
76: my $name = 'Disc';
77: my $count = 1;
78: my $flag = 1;
79: my $dirname = '';
80:
81: my $list = $class->{FILELIST};
82: my $maxsize = $class->{MAXSIZE};
83:
84:
85: die("This is no directory named $class->{BACKUPDIR}\n") unless -d $class->{BACKUPDIR}; # Die if no directory
86:
87: foreach ( sort keys %$list) {
88:
89: if ($flag) { # There has got to be a better way to do this that seting a flag
90: $dirname = "$class->{BACKUPDIR}/$name$count"; # Create new directory name
91: mkdir $dirname; # Create the directory
92: print 'Creating new dir: '.$dirname."\n" if $class->{DEBUG}; # Be more verbose
93: $flag = 0;
94: open LIST, ">$dirname/index.txt" || die ("Cannot open file for writing: $!"); # Create index of Mp3s
95: }
96:
97: copy ($class->{FILELIST}{$_}{PATH},$dirname); # Copy file to new directoy
98: $size += $class->{FILELIST}{$_}{SIZE}; # Add total kilobyes in directory
99: print LIST $_."\n";
100:
101: if ($size >= $maxsize) { # Set up for new directory
102: $flag = 1;
103: $count++;
104: $size = 0;
105: close LIST;
106: }
107:
108: }
109: }
110:
111: 1; #this line is important and will help the module return a true value
112: __END__
113:
In reply to Organize you MP3 backup by rlb3
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |