Creating folder using perl

30,929

Solution 1

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;

Solution 2

You can use the FindBin module, which give us the $Bin variable. It locates the full path to the script bin directory to allow the use of paths relative to the bin directory.

use FindBin qw($Bin);

my $folder = "$Bin/$ARGV[0]";

mkdir($folder, 0700) unless(-d $folder );
chdir($folder) or die "can't chdir $folder\n";

Solution 3

I think it works exactly as it is written, except you have a typo, namely missing a closing parenthesis around chdir.

unless(chdir($ARGV[0])) {   #fixed typo
    mkdir($ARGV[0], 0700);
    chdir($ARGV[0]) or die "can't chdir $ARGV[0]\n";
}

The script runs like this:

  1. If the script cant chdir to $ARGV[0] then:
  2. Make the directory $ARGV[0], with the permission mask 0700.
  3. Change the working directory to $ARGV[0] or exit the script with the error text "cant chdir..".

The starting directory for the script will be the directory it is called from, whatever that directory may be. On *nix that'll be the $ENV{PWD} variable inside your script. It will make a new folder in any folder it has permission to do so.

I think this behavior is logical, and as it should be. If you want your example to work, do this:

.../Scripts> perl ./ScriptContainFolder/FolderCreator.pl ScriptContainFolder/New

You can also use an absolute path, such as

?> FolderCreator.pl /home/m/me/Scripts/ScriptContainFolder/New

ETA: Oh, and you should of course always, always put this in your scripts, no matter how small:

use strict;
use warnings;
Share:
30,929
DarRay
Author by

DarRay

Love to solve puzzles.. and music too.

Updated on September 30, 2021

Comments

  • DarRay
    DarRay almost 3 years

    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?