5

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?

2
  • btw, i tried $path = substr(abs_path($0), 0, index(abs_path($0),'FolderCreator.pl') ); $pwd = pwd; chop($pwd); $path = substr($path, length($pwd)); to get the relative path and also tried mkpath($path.$ARGV[0]) but it gives error "mkdir /Darshana: Permission denied at ./Darshana/scripts/FolderCreator.pl line 25" Commented May 11, 2011 at 5:45
  • my script is generating some text files. so i want to organize those into folders. when i want to create those text files, i create folder then change the folder to it and create the text files. So FolderCreator.pl MyDir will create MyDir folder and create those text files in it. Commented May 11, 2011 at 6:02

3 Answers 3

8

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";
Sign up to request clarification or add additional context in comments.

Comments

4

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;

Comments

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;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.