-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgroupFiles
More file actions
executable file
·355 lines (276 loc) · 9.08 KB
/
groupFiles
File metadata and controls
executable file
·355 lines (276 loc) · 9.08 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
#!/usr/bin/env perl -w
#
# groupFiles: Organize a folder into subfolders by similarity of names.
# <2011-05-0: written by Steven J. DeRose.
#
use strict;
use Getopt::Long;
use File::Copy;
our %metadata = (
'title' => "groupFiles",
'description' => "Organize a folder into subfolders by similarity of names.",
'rightsHolder' => "Steven J. DeRose",
'creator' => "http://viaf.org/viaf/50334488",
'type' => "http://purl.org/dc/dcmitype/Software",
'language' => "Perl 5.18",
'created' => "<2011-05-06",
'modified' => "2022-07-22",
'publisher' => "http://github.com/sderose",
'license' => "https://creativecommons.org/licenses/by-sa/3.0/"
);
our $VERSION_DATE = $metadata{'modified'};
=pod
=head1 Usage
groupFiles [options] file
Normalize file names and move files into sub-folders based on
common parts of their names, ignoring any final digits.
This script does three main operations to help organize a big directory:
=over
=item Normalizes extensions such as jpg/jpeg/JPG/JPEG (I<--cleanExtensions>)
=item Renames files to lose odd characters (I<--cleanNames>)
=item Group files that have similar names.
=back
By default, grouping uses the I<--serials>
option, which removes final digits (ignoring any exstension), and uses
each distinct remaining string as the group/subdirectory name (I<--serials>).
For example,
groupFiles *.jpg
would move 'Yosemite01.jpg', 'Yosemite12.jpg', 'Yosemite1024.jpg', and
so on, into a subdirectory named 'Yosemite', but 'foo1.jpg', 'foo039.png', etc.
into a subdirectory named 'foo'.
To group files by using a regular expression to extract the "common" part, use
I<--regex>. The regex should use () to capture all the parts to be I<kept>.
For example,
groupFiles --regex '(\\w+)'
will extract the first group of 1 or more "word" characters (alphanumerics plus "_"),
and use distinct value of that as the groups.
=head1 Options
(prefix 'no' to negate where applicable)
=over
=item * B<--cleanExtensions>
Standardize various extesions, such as html->htm, jpeg->jpg, etc. (default).
=item * B<--cleanNames>
Rename files to turn runs of unusual characters to "_".
This would be better done separately, via C<renameFiles>.
=item * B<--copyTo> I<path>
Instead of moving files to subdirectories, copy them.
=item * B<--ignoreCase> or B<-i>
Disregard upper/lower case distinctions.
=item * B<--numerics> I<path>
Move files with digit-only names (they may also have extensions), into
the directory at I<path>. There is no special handling for duplicates.
=item * B<--onlyExtension> I<ext>
Only do files with the given extension, even if others are listed (most
likely via "*" or similar) on the command line.
=item * B<--quiet> OR B<-q>
Suppress most messages.
=item * B<--regex> I<expr>
For each file, extract the first match to the regular expression I<expr>, and
use that as the group name.
=item * B<--serials>
Remove final digits (and any extension), and use the remainder as the group name.
(default)
For example, 'Yosemite01.jpg', 'Yosemite12.jpg', 'Yosemite1024.jpg' would
all be moved into a subdirectory named 'Yosemite'.
=item * B<--singletons>
If set (the default), I<--serials> will still create a subdirectory and move
the file, even if the resulting subdirectory will only end up with a single
file in it. For example, if there's a "McKinley003.txt" but not other
files named "McKinley" plus a number.
=item * B<--verbose>
Add more messages (repeatable).
=item * B<--version>
Show version info and exit.
=back
=head1 Known Bugs and Limitations
This is only really meant for handling files in a single source directory
at a time. It's not smart about files with duplicate names and/or contents.
=head1 Related commands
C<find> can do similar work a bit more manually:
find . -regex '.*Yosemite[0-9]*\.jpg' -exec 'mv {} Yosemite/' \\;
C<renameFiles> does regex renaming (the *nix C<rename> command is similar).
=head1 History
<2011-05-06: written by Steven J. DeRose.
2011-05-06 sjd: clean up. Getopt. Support organizing by hyphenations.
2018-09-25: Generalize. Better messaging. Add --copyTo, --onlyExtension,
--singletons.
2022-07-22: Layout. Update for push-reference being dropped from Perl.
2025-05-17: Add --regex.
=head1 To do
Port to Python and use PowerWalk?
Add --dry-run option.
=head1 Ownership
Copyright 2011 by Steven J. DeRose. This work is licensed under a
Creative Commons Attribution-Share Alike 3.0 Unported License.
For further information on this license, see
L<https://creativecommons.org/licenses/by-sa/3.0>.
For the most recent version, see L<http://www.derose.net/steve/utilities> or
L<https://github.com/sderose>.
=cut
###############################################################################
# Options
#
my $cleanExtensions = 0;
my $cleanNames = 0;
my $copyTo = '';
my $dryRun = 0;
my $ignoreCase = 0;
my $numerics = '';
my $onlyExtension = '';
my $quiet = 0;
my $recursive = 0;
my $regex = "";
my $serials = 1;
my $singletons = 1;
my $verbose = 0;
Getopt::Long::Configure ("ignore_case");
my $result = GetOptions(
"cleanNames!" => \$cleanNames,
"copyTo!" => \$copyTo,
"cleanExtensions!" => \$cleanExtensions,
"dryRun|test!" => \$dryRun,
"h|help" => sub { system "perldoc $0"; exit; },
"i|ignoreCase!" => \$ignoreCase,
"numerics=s" => \$numerics,
"onlyExtension=s" => \$onlyExtension,
"q|quiet!" => \$quiet,
"recursive!" => \$recursive,
"regex=s" => \$regex,
"singletons!" => \$singletons,
"serials!" => \$serials,
"v|verbose+" => \$verbose,
"version" => sub {
die "Version of $VERSION_DATE, by Steven J. DeRose.\n";
}
);
($result) || die
"Bad options.\n";
($dryRun) && die
"--dryRun is not yet supported.\n";
($recursive) && die
"--recursive is not yet supported.\n";
(!$numerics || -d $numerics) || die
"--numerics directory '$numerics' not found.\n";
###############################################################################
#
sub vMsg {
my ($level, $msg) = @_;
($verbose >= $level) && warn($msg . "\n");
}
sub doRename {
my ($fr, $to) = @_;
my $suffix = sjdUtils::availableFileNum($to);
if ($suffix) {
$to = $to . $suffix; # FIX to insert before extension
}
rename($fr, $to);
return $to;
}
my %extensionMap = (
"jpeg" => "jpg",
"html" => "htm",
"text" => "txt",
"tiff" => "tif",
);
my $extFail = 0;
my $extSucc = 0;
my $charSucc = 0;
my $nNumerics = 0;
my $nSerials = 0;
# Determine the list of files to work on.
#
my $fileArgs = '';
foreach my $f (@ARGV) {
$f =~ s/'/\\'/g;
$fileArgs .= "'$f'";
}
my @files = @ARGV;
vMsg(1, "Number of files: " . scalar(@files));
if ($cleanExtensions) {
foreach my $x (@files) {
$x =~ m/\.([^.\+])$/;
my $ext = ($1) ? $1:"";
my $extKey = lc($ext);
if (defined $extensionMap{$extKey}) {
(my $newName = $x) =~ s/\.$ext$/.$extensionMap{$ext}/;
if (-e $newName) {
vMsg(0, "Could not rename '$x' to '$newName' -- already exists.\n");
$extFail++;
}
else {
my $cmd = "mv '$x' '$newName'";
system "$cmd" || vMsg(0, "$cmd failed\n");
$extSucc++;
}
}
} # $x
}
if ($cleanNames) {
foreach my $x (@files) {
my $newName = $x;
$newName =~ s/[^-_.a-zA-Z0-9]+/_/g;
if ($x ne $newName) {
($verbose) && vMsg(0, "normalizing '$x' to '$newName'\n");
rename($x,$newName);
$charSucc++;
}
} # $c
}
# Find unique group names
my %groups = ();
if ($serials) {
foreach my $f (@files) {
(my $fpre = $f) =~ s/\d+\.\w+$//;
if ($fpre == '' || $fpre == $f) { next; }
if (!defined $groups{$fpre}) {
$groups{$fpre} = [];
}
else {
my $aref = $groups{$fpre};
push @$aref, $f;
}
}
}
elsif ($regex) {
foreach my $f (@files) {
(my $fpre = $f) =~ m/$regex/;
if ($fpre == '' || $fpre == $f) { next; }
if (!defined $groups{$fpre}) {
$groups{$fpre} = [];
}
else {
my $aref = $groups{$fpre};
push @$aref, $f;
}
}
}
foreach my $pre (keys(%groups)) {
if (!$singletons && scalar(@$pre)) { next; }
my $nfiles = scalar(@$pre);
vMsg(1, "Moving $nfiles files for '$pre*' to '_$pre/'");
if (-e "_$pre" == 0) {
mkdir("_$pre");
}
foreach my $f (@$pre) {
if ($copyTo) {
File::copy($f, "$copyTo/$pre/$f");
}
else {
rename($f, "$pre/$f");
}
$nSerials++;
} # $f
}
if ($numerics) {
vMsg(0, "Moving all-numeric files to '$numerics' dir.");
for my $f (@files) {
($f =~ m/^\d+(\.\w+)?$/) || next;
if ($f =~ m/^\d+\.jpg$/) {
rename($f, "$numerics/$f");
$nNumerics++;
}
}
}
if (!$quiet) {
vMsg(0, "Done.");
}