if (defined($ARGV[0]) && $ARGV[0] ne "") {
$f=""; #initialize $f
$dir = $ARGV[0];
chomp($dir);
#directory paths should use '\' instead of '/'
if ($dir =~ /^\w:\//) {
$_ = $dir;
s/\//\\/ig;
$dir = $_;
}
#seperate filename from directory path if given
if ($dir =~ /.*\.htm$|\.html$/i) {
$f = substr($dir, (rindex($dir,"\\"))+1,);
$dir = substr($dir, 0, (rindex($dir,"\\"))+1);
chdir("$dir");
}
}
#if no directory given, default to script directory
else {
$dir = substr($0, 0, (rindex($0,"\\"))+1);
}
#Directory path must end with '\'
if ($dir !~ /\\$/i) { $dir = $dir . "\\"; }
The above code is what i am currently using to initialize the directory and filename for my script. The program will always be used with a html file, so there is no worries about other file formats being passed through the code.
While the code works, it seems to me to be a little 'clunky' in design. I don't know if its possible, but any suggestions on improving the code to make it cleaner and or faster?
Thanks for any help in advance.