Skip to content

Instantly share code, notes, and snippets.

@akirad
Last active November 9, 2017 05:20
Show Gist options
  • Save akirad/454ec46f644e84008f1575173ef5ceda to your computer and use it in GitHub Desktop.
Save akirad/454ec46f644e84008f1575173ef5ceda to your computer and use it in GitHub Desktop.
A perl script that finds BOM from the files under the directory specified.
use strict;
use warnings;
my $dir = $ARGV[0];
if((!$dir) or (! -d $dir)) {
    die "Need to specify a directory.";
}
my $isFound = 0;
checkBomFrom($dir);
if ($isFound == 0) {
    print "No files which include BOM.";
}
sub checkBomFrom {
    my $dir = shift;
    opendir(my $in, $dir) or die "Failed to open $dir: $!";
my @fileList = readdir($in);
closedir($in);
foreach my $file (@fileList){
if($file =~ /^\.{1,2}$/){
            next;
}
if( -d "$dir\\$file"){
            checkBomFrom("$dir\\$file");
}
        else{
            printFileNameIfIncludeBom("$dir\\$file");
        }
    }
}
sub printFileNameIfIncludeBom {
    my $file = shift;
if(-f $file) {
        open(my $in, $file) or die "Failed to open $file: $!";
        my $firstLine = <$in>;
        close($in);
        return if (!$firstLine);
        if($firstLine =~ /^\xef\xbb\xbf/) {
            print $file;
            if ($isFound == 0) {
                $isFound = 1;
            }
        }
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment