-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
mkdir_extension_and_categorize
executable file
·102 lines (97 loc) · 2.18 KB
/
mkdir_extension_and_categorize
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
#!/usr/bin/perl
# abstract: quickly sort content in dl dir into categories
# beware, no error handling
#
# $ magnusw1]> mkdir_extension_and_categorize {h,i}*
#
# `hilightcmd.pl' -> `code/hilightcmd.pl'
# `hilightwin.pl' -> `code/hilightwin.pl'
# `hotplug.json' -> `code/hotplug.json'
# `hotplug-preinit.json' -> `code/hotplug-preinit.json'
# `idoler.gif' -> `img/idoler.gif'
# `image01.jpeg' -> `img/image01.jpeg'
# `image02.jpeg' -> `img/image02.jpeg'
# `image03.jpeg' -> `img/image03.jpeg'
# `index.html' -> `code/index.html'
# `ipcalc.sh' -> `code/ipcalc.sh'
use strict;
use warnings FATAL => 'all';
use File::Copy;
use File::Path qw(make_path);
use File::LsColor qw(ls_color_internal);
*ls_color = *ls_color_internal;
my %categorize = (
'7z' => 'archive',
gz => 'archive',
tar => 'archive',
zip => 'archive',
lha => 'archive',
xz => 'archive',
m3u => 'audio',
pls => 'audio',
mp3 => 'audio',
flac => 'audio',
ogg => 'audio',
wav => 'audio',
bin => 'bin',
exe => 'bin',
pl => 'code',
pm => 'code',
sh => 'code',
vim => 'code',
js => 'code',
html => 'code',
css => 'code',
csv => 'code',
php => 'code',
json => 'code',
tt => 'code',
xs => 'code',
py => 'code',
c => 'code',
h => 'code',
adf => 'game',
nes => 'game',
spc => 'game',
gb => 'game',
gbc => 'game',
gba => 'game',
doc => 'doc',
docx => 'doc',
pdf => 'doc',
jpg => 'img',
jpeg => 'img',
gif => 'img',
png => 'img',
ico => 'img',
svg => 'img',
webp => 'img',
ttf => 'font',
otf => 'font',
otb => 'font',
txt => 'text',
conf => 'conf',
rc => 'conf',
);
_mkdir(@ARGV);
sub _mkdir {
for my $file(@_) {
if(-f $file) {
if($file =~ m|(.+)[.](.+)$|) {
my $new_dir = exists($categorize{lc($2)})
? $categorize{lc($2)} # archive
: $2; # swf
if(! -d $new_dir) {
make_path($new_dir)
}
if(File::Copy::move($file, $new_dir)) {
printf "`%s' -> `%s/%s'\n",
ls_color($file), ls_color($new_dir), ls_color($file);
}
else {
print STDERR "$file: $@\n";
}
}
}
}
}