Skip to content

Instantly share code, notes, and snippets.

@codewizard13
Created October 13, 2015 19:51
Show Gist options
  • Save codewizard13/45f7b0d639a710560af0 to your computer and use it in GitHub Desktop.
Save codewizard13/45f7b0d639a710560af0 to your computer and use it in GitHub Desktop.
Change the icons of multiple subfolders in Windows OS at a time. [Not fully working yet]
# use warnings;
use Cwd;
use Data::Dumper;
print "Hello World!\n\n";
#########################################################
## FILE: ehCode_multiSubdirIconChanger_01.pl
## REPO: EWH-Perl-IconChangerMultiSubdir
## CREATED: 2015.10.05
## AUTHOR: Eric Hepperle
##
## VERSION: 1.0
##
## PURPOSE: To change the icons of multiple subfolders
## in Windows OS at a time.
##
## USAGE: <filename>.pl <path_to_change_icons>
##
#########################################################
$bar = '-'x45,"\n";
# my $nppPath = '/c/Users/Eric/AppData/Roaming/Notepad++/backup';
my $testPath = '/c/Users/Eric/AppData/Roaming/Notepad++/backup';
# my @dirContents = `ls $nppPath | grep 'new '`;
my @subdirs = `ls -d */`;
print "\n$bar\n";
# print_r ("$bar\@subdirs:\n", @subdirs);
# Works for all files and folders in dir
# print "current directory contains " . join(', ', <*>) . "\n";
# print "current directory contains:\n" . join("\n", @subdirs) . "\n";
print "current directory contains:\n" . Dumper(\@subdirs) . "\n";
print "\n$bar\n";
# chdir($nppPath);
# confirm chdir:
# print "\tCurrent working directory after change is: \n" . `pwd`;
print "\tRESULTS:\n\n";
# Debugging/ Testings ...
my $testPath = `pwd`;
chomp($testPath);
# $testPath .= '/';
# printFileContents('eh_testfile_2', $testPath);
printFileContents('eh_testfile_2', $testPath);
msg($bar . "\n");
# printFileContents('butters');
# msg($bar . "\n");
# printFileContents('eh_testfile_2.txt');
# msg($bar . "\n");
exit;
my $out = changeDirIcons(@subdirs);
print $out;
# write desktop.ini into subdir
my $outfileName = 'eh_masterjoined_01.txt';
open FH, '>', $outfileName or die "Can't open file $!";
print FH $out;
###################################
# SUBROUTINES:
###################################
sub msg {
my $message = shift;
print $message;
}
sub removeTrailingSlash {
my ($path) = @_;
# If there is a "/" at end lets remove it before building fullpath
my $substr = substr($path, -1);
msg("\$substr = $substr\n");
my $resp = ($substr == "/") ? "SLASH was found at end of [$path]" : "There's NO SLASH at end of [$path]!";
msg("\$resp = $resp\n");
}
sub printFileContents {
my ($filename, $path) = @_;
# Assume file is in current path if none given.
$path = defined $path ? $path : `pwd`;
# Remove newline from filename and path.
chomp($filename);
chomp($path);
# I was trying to remove trailing slash from path if there is one.
# The slash gets removed, but the ternary I'm using to
# verify it isn't working for some reason.
removeTrailingSlash($path);
# build fullpath.
$fullPath = $path . '/' . $filename;
msg("\tFilename = $filename\n");
msg("\tPath = $path\n");
msg("\tfullPath = $fullPath\n");
my $resp = -e $fullPath ? "$fullPath EXISTS" : "$fullPath DOES NOT EXIST!";
msg("\$resp = $resp\n");
exit;
# print "FILENAME TO OPEN: $filename\n";
open my $FH, '<', $fullPath or die "Can't open file {$fullPath}[$!]";
# print "Successfully opened:\t$filename\n";
my $fileContents = do { local $/; <$FH> };
my $outStr = nameBar($filename) . "\n\n" . $fileContents . "\n\n";
}
sub changeDirIcons {
my (@subdirs) = @_;
my $rootPath = `pwd`;
print "\$rootPath = $rootPath\n";
my $outStr = '';
foreach my $dirname (@subdirs) {
# Remove newline from $dirname.
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;
}
#########################################################
##
## ALGORITHM:
## 1) Program is called with no args.
## A) Foreach subdir in a dir ...
## 1. If no desktop.ini file in subdir then ..
## a. Write new desktop.ini file.
## ELSE
## a. Overwrite current desktop.ini.
## 2. Flag this dir as system file with
## attrib command.
##
## NOTES:
##
## 2015.10.13
## - Began writing program.
## - I was trying to remove trailing slash from path if there
## is one. The slash gets removed, but the ternary I'm
## using to verify it isn't working for some reason.
##
#########################################################
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment