Skip to content

Instantly share code, notes, and snippets.

@jbarrett
Created December 18, 2018 11:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jbarrett/f6c516f5cbad56d91e6154f4fd870e4b to your computer and use it in GitHub Desktop.
Save jbarrett/f6c516f5cbad56d91e6154f4fd870e4b to your computer and use it in GitHub Desktop.
Very quick and dirty NTFS file renamer since ntfs-3g will happily write "invalid" filenames.
#!/usr/bin/env perl
use strict;
use warnings;
use File::Copy qw/ mv /;
use File::Find ();
my $dir = $ARGV[0] || '.';
use vars qw/*name *dir *prune/;
*name = *File::Find::name;
*dir = *File::Find::dir;
*prune = *File::Find::prune;
my $invalid_chars = qr/[*:?<>]/;
sub wanted;
File::Find::find({wanted => \&wanted}, $dir);
exit;
sub wanted {
my ($dev,$ino,$mode,$nlink,$uid,$gid);
(($dev,$ino,$mode,$nlink,$uid,$gid) = lstat($_)) &&
! -f _
&& return;
$name !~ $invalid_chars && return;
printf("%s\t->\t%s\n", $name, $name =~ s/$invalid_chars/-/gr);
mv $name, $name =~ s/$invalid_chars/-/gr;
}
@jbarrett
Copy link
Author

Bug : should do two File::Find::finds - first dirs, then files.

As this was a once-off I'm probably not going to update it. This is just something you should be aware of.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment