Skip to content

Instantly share code, notes, and snippets.

@Songmu
Last active December 11, 2024 09:18
Show Gist options
  • Select an option

  • Save Songmu/1c9f86fc342eed8ba2911927a597a48b to your computer and use it in GitHub Desktop.

Select an option

Save Songmu/1c9f86fc342eed8ba2911927a597a48b to your computer and use it in GitHub Desktop.

Revisions

  1. Songmu revised this gist Jan 21, 2018. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion rename-nfd.pl
    Original file line number Diff line number Diff line change
    @@ -15,7 +15,7 @@
    my $fname = decode_utf8 basename $file;

    # readdir しないと 生のファイル名が取れない
    # (NFC/NFDどちらでもファイルの実態を解決できるため)
    # (NFC/NFDどちらでもファイルの実体を解決できるため)
    while (my $f = readdir $dh) {
    my $fbase = decode_utf8 $f;
    # NFC subroutine expects perl string
  2. Songmu created this gist Jan 21, 2018.
    37 changes: 37 additions & 0 deletions rename-nfd.pl
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,37 @@
    #!/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;
    }
    }