Last active
December 11, 2024 09:18
-
-
Save Songmu/1c9f86fc342eed8ba2911927a597a48b to your computer and use it in GitHub Desktop.
nfdなファイル名をnfcに変換するスクリプト
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
use 5.014; | |
use warnings; | |
use Encode qw/decode_utf8 encode_utf8/; | |
use Unicode::Normalize qw/NFC/; | |
use File::Basename qw/basename dirname/; | |
use File::Copy qw/move/; | |
use File::Spec; | |
for my $file (@ARGV) { | |
die "file not found: $file\n" unless -e $file; | |
my $dir = dirname $file; | |
opendir my $dh, $dir or die $!; | |
my $fname = decode_utf8 basename $file; | |
# readdir しないと 生のファイル名が取れない | |
# (NFC/NFDどちらでもファイルの実体を解決できるため) | |
while (my $f = readdir $dh) { | |
my $fbase = decode_utf8 $f; | |
# NFC subroutine expects perl string | |
if (NFC($fname) eq NFC($fbase)) { | |
try_move( File::Spec->catfile($dir, $f) ); | |
} | |
} | |
} | |
sub try_move { | |
my $file = shift; | |
$file = decode_utf8 $file; | |
my $nfc = NFC($file); | |
if ($nfc ne $file) { | |
my $raw_nfc = encode_utf8 $nfc; | |
move($file, $nfc) or die "Move $raw_nfc failed: $!\n"; | |
printf "moved: %s", $raw_nfc; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment