Last active
June 18, 2022 19:51
-
-
Save protospork/3885187 to your computer and use it in GitHub Desktop.
script for crc checking/tagging files, windows or linux
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
#!C:\perl\perl\bin | |
# sudo apt-get install libmodern-perl-perl libstring-crc32-perl | |
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 { #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"; | |
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; #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 | |
say $1 if $debugmode; | |
my $oldhash = $1; | |
#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; | |
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 { #and if it was untagged, tag it with one | |
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' | |
#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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment