Last active
November 9, 2017 05:20
-
-
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.
This file contains 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
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