-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgallery_filename_munge
More file actions
61 lines (51 loc) · 1.35 KB
/
gallery_filename_munge
File metadata and controls
61 lines (51 loc) · 1.35 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
#!/usr/bin/perl -w
# Spidereyeballs is a great program, but it does not well handle files with identical names in different source directories - it treats them as the same file. This script munges the filenames to be unique by incorporating elements of their directory.
$dir = qw{/home/pgunn/mediafiles};
use File::Copy;
my $doit=1;
main();
#########################
sub main
{
my $targdir = handle_args($dir, @ARGV);
chdir($targdir) || die "Cannot enter directory-to-munge [$targdir]:$!\n";
foreach my $subdir (all_subdirs())
{
# print "D: $subdir/\n";
foreach my $file (all_files($subdir))
{
next if($file =~ /__/); # In case it gets run multiple times
my $targfile = $subdir . '__' . "$file";
print "$subdir/$file -> $subdir/$targfile\n";
if($doit)
{
move("$subdir/$file", "$subdir/$targfile");
}
}
}
}
sub handle_args
{
my ($defdir, @args) = @_;
my $ret = $defdir;
if(@args == 1 && [ -d $args[0] ])
{$ret = $args[0];}
elsif(@args != 0)
{die "Usage: $0 [DIR]\n";}
return $ret;
}
sub all_subdirs
{
opendir(HERE, '.') || die "Could not read current directory:$!\n";
my @ret = grep { $_ !~ /^\./ } grep {-d $_} readdir(HERE);
closedir(HERE);
return @ret;
}
sub all_files
{
my ($dir) = @_;
opendir(DIR, $dir) || die "Could not read directory [$dir]:$!\n";
my @ret = grep { $_ !~ /^\./ } grep { [ -f $_ ] } readdir(DIR);
closedir(DIR);
return @ret;
}