I want to create folder using perl where, in the same folder, a perl script exists. I created FolderCreator.pl which requires an input parameter of folder name
.
unless(chdir($ARGV[0]){ # If the dir available change current , unless
mkdir($ARGV[0], 0700); # Create a directory
chdir($ARGV[0]) or die "can't chdir $ARGV[0]\n"; # Then change or stop
}
This worked fine only if we call the scipt, in the same folder where it resides. If it is called in another folder, if doesn't work.
Eg.
.../Scripts/ScriptContainFolder> perl FolderCreator.pl New
.../Scripts> perl ./ScriptContainFolder/FolderCreator.pl New
First one is working fine but second one doesn't. Is there way create these folders?
I've done the job and here is the code... Thank you all for the help...
#!usr/bin/perl
###########################################################################################
# Needed variables
use File::Path;
use Cwd 'abs_path';
my $folName = $ARGV[0];
#############################################################################################
# Flow Sequence
if(length($folName) > 0){
# changing current directory to the script resides dir
$path = abs_path($0);
$path = substr($path, 0, index($path,'FolderCreator.pl') );
$pwd = `pwd`;
chop($pwd);
$index = index($path,$pwd);
if( index($path,$pwd) == 0 ) {
$length = length($pwd);
$path = substr($path, $length+1);
$index = index($path,'/');
while( $index != -1){
$nxtfol = substr($path, 0, $index);
chdir($nxtfol) or die "Unable to change dir : $nxtfol";
$path = substr($path, $index+1);
$index = index($path,'/');
}
}
# dir changing done...
# creation of dir starts here
unless(chdir($folName)){ # If the dir available change current , unless
mkdir("$USER_ID", 0700); # Create a directory
chdir($folName) or $succode = 3; # Then change or stop
}
}
else {
print "Usage : <FOLDER_NAME>\n";
}
exit 0;