Created
June 7, 2013 04:50
-
-
Save akirad/5727126 to your computer and use it in GitHub Desktop.
A perl script which can replace tab to 4 spaces in source files under an argument(dir).
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
use strict; | |
use warnings; | |
use File::Basename 'fileparse'; | |
my $dir = $ARGV[0]; | |
if(! -d $dir){ | |
die "Invalid argument."; | |
} | |
$dir =~ s|\\|/|g; # Just in case. | |
my @fileList; | |
getFileList($dir, \@fileList); | |
foreach my $file (@fileList){ | |
if(-f $file){ | |
my($basename, $dirname) = fileparse($file); | |
my $outFile = "$dirname/$basename.replaced"; | |
open(IN, $file); | |
open(OUT, ">$outFile"); | |
foreach my $line (<IN>){ | |
$line =~ s/\t/ /g; | |
print(OUT $line); | |
} | |
close(IN); | |
close(OUT); | |
rename($outFile, $file); | |
} | |
} | |
sub getFileList{ | |
my $dir = shift; | |
my $ref_fileList = shift; # for output. | |
opendir(DIR, $dir); | |
my @fileList = readdir(DIR); | |
closedir(DIR); | |
foreach my $file (sort @fileList){ | |
if($file =~ /^\.{1,2}$/){ | |
next; | |
} | |
if( -d "$dir/$file"){ | |
getFileList("$dir/$file", $ref_fileList); | |
} | |
else{ | |
push(@$ref_fileList, "$dir/$file"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment