Skip to content

Instantly share code, notes, and snippets.

@wcasanova
Forked from protospork/CRC.pl
Created June 18, 2022 19:51
Show Gist options
  • Save wcasanova/a5168dc6ac584bf4d7d80c99a03c1999 to your computer and use it in GitHub Desktop.
Save wcasanova/a5168dc6ac584bf4d7d80c99a03c1999 to your computer and use it in GitHub Desktop.

Revisions

  1. @protospork protospork revised this gist Jan 18, 2021. 1 changed file with 4 additions and 1 deletion.
    5 changes: 4 additions & 1 deletion CRC.pl
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,7 @@
    #!C:\perl\perl\bin

    # sudo apt-get install libmodern-perl-perl libstring-crc32-perl

    use Modern::Perl;
    use String::CRC32;
    my $sil = 0;
    @@ -32,7 +35,7 @@
    say $hash if $debugmode;
    close $file;

    $hash =~ s/^(.+)$/uc sprintf "%08x", $1/eg;
    $hash =~ s/^(.+)$/uc sprintf "%08x", $1/eg; #why did I do this in a regex?
    say $hash if $debugmode;

    if ($this =~ /\[([0-9A-F]{8})\]/){ #if the filename has a conventional "[A67F3C23]"-style crc tag, check it
  2. @protospork protospork revised this gist Oct 13, 2012. 1 changed file with 7 additions and 7 deletions.
    14 changes: 7 additions & 7 deletions CRC.pl
    Original file line number Diff line number Diff line change
    @@ -11,14 +11,14 @@
    $sil = 1;
    }
    @files = @ARGV;
    } else {
    } else { #if you invoke without any arguments, it'll do the whole directory
    @files = glob "*.*";
    }
    $sil++ if scalar @files > 25;

    for my $this (@files){
    unless (-e $this && -w $this){
    say $this." doesn't exist or isn't writable";# if $debugmode;
    say $this." doesn't exist or isn't writable";
    next;
    }
    my ($name,$ext) = $this =~ /^(.+?)\.(\w{2,4})$/;
    @@ -35,16 +35,15 @@
    $hash =~ s/^(.+)$/uc sprintf "%08x", $1/eg;
    say $hash if $debugmode;

    if ($this =~ /\[([0-9A-F]{8})\]/){
    if ($this =~ /\[([0-9A-F]{8})\]/){ #if the filename has a conventional "[A67F3C23]"-style crc tag, check it
    say $1 if $debugmode;
    my $oldhash = $1;
    # say $this.' OK ('.$hash.')' if uc $oldhash eq $hash;
    #all this sprintf stuff right-aligns the actual CRC and OK/NOT OK message
    printf "%-70s OK\n", (sprintf "%s %".(70-(length $this))."s", ($this, "($hash)")) if uc $oldhash eq $hash;
    # say $this.' NOT OK ('.$hash.')' if uc $oldhash ne $hash;
    printf "%-70s NOT OK\n", (sprintf "%s %".(70-(length $this))."s", ($this, "($hash)")) if uc $oldhash ne $hash;
    say 'next after check' if $debugmode;
    next;
    } else {
    } else { #and if it was untagged, tag it with one
    say $this.' => '.$hash;

    $name .= ' ['.$hash.'].'.$ext;
    @@ -61,6 +60,7 @@
    say 'took '.(time - $start).' seconds ('.(scalar @files).' files)';
    $^O eq 'MSWin32'
    ? system 'pause'
    : system 'read -p "Press any key to continue."';
    #I say 'any', but it might have to be enter? If so, listening to stdin makes more sense
    : system 'read -p "Press any key to continue."';
    exit;
    }
  3. @protospork protospork created this gist Oct 13, 2012.
    66 changes: 66 additions & 0 deletions CRC.pl
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,66 @@
    #!C:\perl\perl\bin
    use Modern::Perl;
    use String::CRC32;
    my $sil = 0;
    my $debugmode = 0;
    my $start = time;
    my @files;
    if (@ARGV){
    if ($ARGV[0] =~ /^-q/){
    shift @ARGV;
    $sil = 1;
    }
    @files = @ARGV;
    } else {
    @files = glob "*.*";
    }
    $sil++ if scalar @files > 25;

    for my $this (@files){
    unless (-e $this && -w $this){
    say $this." doesn't exist or isn't writable";# if $debugmode;
    next;
    }
    my ($name,$ext) = $this =~ /^(.+?)\.(\w{2,4})$/;
    say $name if $debugmode;

    open my $file, $this || die $!;
    binmode($file);

    my $hash = crc32(*$file);

    say $hash if $debugmode;
    close $file;

    $hash =~ s/^(.+)$/uc sprintf "%08x", $1/eg;
    say $hash if $debugmode;

    if ($this =~ /\[([0-9A-F]{8})\]/){
    say $1 if $debugmode;
    my $oldhash = $1;
    # say $this.' OK ('.$hash.')' if uc $oldhash eq $hash;
    printf "%-70s OK\n", (sprintf "%s %".(70-(length $this))."s", ($this, "($hash)")) if uc $oldhash eq $hash;
    # say $this.' NOT OK ('.$hash.')' if uc $oldhash ne $hash;
    printf "%-70s NOT OK\n", (sprintf "%s %".(70-(length $this))."s", ($this, "($hash)")) if uc $oldhash ne $hash;
    say 'next after check' if $debugmode;
    next;
    } else {
    say $this.' => '.$hash;

    $name .= ' ['.$hash.'].'.$ext;
    $name =~ s/\] +\[/][/g;
    rename ($this, $name) || die $!;
    say $name if $debugmode;
    say 'next after rename' if $debugmode;
    next;
    }
    say 'next' if $debugmode;
    next;
    }
    if (! $sil){
    say 'took '.(time - $start).' seconds ('.(scalar @files).' files)';
    $^O eq 'MSWin32'
    ? system 'pause'
    : system 'read -p "Press any key to continue."';
    exit;
    }