Created
October 8, 2015 17:01
-
-
Save codewizard13/37bdff15f12042b531c1 to your computer and use it in GitHub Desktop.
[Perl] Gets all files in a windows directory (with GitBash) and joins them together into one. Useful for join all anonymous Notepad++ files into one.
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 warnings; | |
use Cwd; | |
print "Hello World!\n\n"; | |
######################################################### | |
## CREATED: 2015.10.05 | |
## AUTHOR: Eric Hepperle | |
## | |
## Get all files in dir and concat them together | |
## with a horizontal rule including the filename, | |
## followed by the file contents and 3 blank lines. | |
## | |
## NOTE: This version concats the files but needs work | |
## on how to display filename and bars. | |
## Currently working on how to center the | |
## filename in the bar, but Perl modulus (%) | |
## operator behavior needs examination. | |
## | |
## 10/06/15 - Works. Adds namebar and contents to | |
## screen. Next is to get it to write | |
## out to a file. | |
## | |
## 10/07/15 - Works and writes to joinfile! :). | |
## | |
######################################################### | |
my $nppPath = '/c/Users/Eric/AppData/Roaming/Notepad++/backup'; | |
my @dirContents = `ls $nppPath | grep 'new '`; | |
# chdir($nppPath); | |
# confirm chdir: | |
# print "\tCurrent working directory after change is: \n" . `pwd`; | |
print "\$dirContents:\n\n"; | |
# $bar = '-'x45,"\n"; | |
my $out = joinFileContents(@dirContents); | |
print $out; | |
# write joined file contents to joinfile | |
my $outfileName = 'eh_masterjoined_01.txt'; | |
open FH, '>', $outfileName or die "Can't open file $!"; | |
print FH $out; | |
################################### | |
# SUBROUTINES: | |
################################### | |
sub joinFileContents { | |
my (@dirContents) = @_; | |
my $rootPath = `pwd`; | |
print "\$rootPath = $rootPath\n"; | |
my $outStr = ''; | |
foreach my $filename (@dirContents) { | |
# Remove newline from $filename. | |
chomp($filename); | |
$fullPath = $nppPath . '/' . $filename; | |
# print "FILENAME TO OPEN: $filename\n"; | |
open my $FH, '<', $fullPath or die "Can't open file {$fullPath}[$!]"; | |
# print "Successfully opened:\t$filename\n"; | |
# verify contents: | |
my $fileContents = do { local $/; <$FH> }; | |
# print "\n\n$bar\n\n"; | |
# print "File Contents:\n\n$fileContents\n"; | |
# print bar and contents | |
$outStr .= nameBar($filename) . "\n\n" . $fileContents . "\n\n"; | |
} | |
=begin comment | |
print nameBar("Eric's Name Bar", 69); | |
print nameBar("Apples", 48); | |
print nameBar("Incredible Hulk"); | |
=cut | |
return $outStr; | |
} | |
sub nameBar { | |
my ($name, $barlen) = @_; | |
# default bar length. | |
my $barLen = $barlen ? $barlen : 60; | |
my $nameLen = length($name); | |
my $topBar = '#'x$barLen; | |
my $bottomBar = '#'x$barLen; | |
=begin comment | |
print "\$name = $name\n\n"; | |
print "\$topBar = $topBar\n"; | |
print "\$barLen = $barLen\n"; | |
print "\$nameLen = $nameLen\n"; | |
print "\$bottomBar = $bottomBar\n"; | |
=cut | |
my $nameStartPoint = int($barLen / 2) - int($nameLen / 2); | |
my $barCharsFront = $nameStartPoint - 1; | |
my $barCharsRear = $barLen - ($barCharsFront + $nameLen); | |
my $midBar = '##' . ' 'x ($barCharsFront - 2) . $name . ' 'x ($barCharsRear - 2) . '##'; | |
=begin comment | |
print "NAME WILL START AT CHARACTER [$nameStartPoint] of the bar\n\n"; | |
print "\$barCharsFront = $barCharsFront\n\n"; | |
print "\$midBar =\n$midBar\n" . length($midBar) . "\n\n"; | |
=cut | |
# my $fullBar = "\t$topBar\n\t$midBar\n\t$bottomBar\n\n"; | |
my $fullBar = "\t$topBar\n\t$midBar\n\t$bottomBar\n\n"; | |
return $fullBar; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment