-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfilethem
More file actions
109 lines (92 loc) · 2.78 KB
/
Copy pathfilethem
File metadata and controls
109 lines (92 loc) · 2.78 KB
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
97
98
99
100
101
102
103
104
105
106
107
108
109
#!/usr/bin/perl
###############################################################################
# Quick script to file things away into there correct folders #
# September 2013 #
# #
# usage is sudo filethem to actually do the business of filethem -testing for #
# for just seeing what will happen #
###############################################################################
use Getopt::Long;
use strict;
use warnings;
use File::Copy qw(copy move);
# default stuff
my $testing = 0;
my $result = GetOptions ("testing" => \$testing);
print "We Are Just Testing\n\n" if ($testing);
# ignore the following files and folders - don't try to move them!
my @ignorefiles = qw(. .. iTunes mythtv iPlayer games images ISOs movies music software TV unsorted web filethem.pl);
# this is where we are looking for new videos - should really make it a parameter?
my $directory = "./";
readfiles($directory);
sub readfiles
{
my $directory = $_[0];
opendir my $dh, $directory or die $!;
while (my $file = readdir($dh)) {
# if it isn't in the list of ignored files/dirs then do something
if(!(grep $_ eq $file, @ignorefiles)) {
# if its a dir we need to do something different. Should ideally head into it and extract the file
if (-d ($file)) {
print "$file is a dir - recursing\n";
readfiles($file);
# still need to skip is as we shouldn't try to move a directory
next;
}
# split the filename into bits.
my @words = split/\./, $file;
my $wordCount = scalar @words;
my $folder = 'TV/';
my $episodeInfo = '';
my $season;
my $found = 0;
foreach (@words) {
# for episode info in the form S01E22
if (!($found) && ($_ =~ m/[S|s](\d\d)[E|e](\d\d)/)){
# print "looks like SxxDyy\n" if ($testing);
$episodeInfo = $_;
$season = $1 + 0;
$found = 1;
}
if (!($found) && ($_ =~ m/^(\d)(\d\d)$/)){
# print "looks like xyy\n" if ($testing);
# for episode info in the form 822
$episodeInfo = $_;
$season = $1 + 0;
$found = 1;
}
if ($episodeInfo eq '') {
$folder .= capitalise($_)."\.";
}
}
if (!($found)) {
#print "No Episode info found\n";
next;
}
chop($folder);
$folder.="/Season $season/";
my $destination = "$folder$file";
print "$directory/$file should be moved to $folder -> ";
if (-d $folder) {
print "Move in progress. Please wait. -> ";
if (!$testing) {
move $directory."/".$file, $destination or die $!;
}
print " Done.\n";
} else {
print "No such folder $folder\n";
}
}
}
closedir($dh);
}
# sub to do stuff...
sub capitalise
{
my $word = $_[0];
if ($word ne 'of') {
return ucfirst($word);
} else {
return $word;
}
}