nisha has asked for the wisdom of the Perl Monks concerning the following question:

Perl Monks,

I have this problem and please help me solve it.

Suppose i have 2 variables

$dir1 = "c:\\reports\\temp1\\temp2"; $dir2 = "c:\\output";
my script has to do a pattern match to extract everything after c:\\reports in $dir1 and then i should be able to create a similar directory structure in
c:\\output i.e c:\\output\\temp1\\temp2.
How do i go about extracting it this way and how can i create directories recursively?? I should be able to do a pattern match for any given generic value.

Please please help me!

Considered by jbrugger: delete, seems to me it's a dup of Directory operations
Unconsidered by Arunbear: keep (and edit) votes prevented reaping; Keep/Edit/Delete: 11/1/18

Replies are listed 'Best First'.
Re: Extracting from variables
by davido (Cardinal) on Nov 08, 2005 at 07:59 UTC

    If you want to grab each portion of the path, delimited by '/', you can do this (more on '/' later):

    my $dir1 = 'c:/reports/temp1/temp2'; my $dir2 = 'c:/output'; my( @dirs ) = split /\//, $dir1; $dirs[0] = $dir2; foreach my $dir ( @dirs ) { $dir2 .= '/' . $dir; mkdir $dir2 or die $!; }

    Note: For the sake of portability and simplicity, Perl allows you to separate directories with a '/' character, which is a whole lot easier to deal with than '\' characters. And you can do it that way on both a Windows system and a *nix system, from within a Perl script.


    Dave

Re: Extracting from variables
by murugu (Curate) on Nov 08, 2005 at 08:02 UTC
Re: Extracting from variables
by blazar (Canon) on Nov 08, 2005 at 10:44 UTC
    First off, an useful hint that I can give you is that you can use (forward) slashes under Windows too. Said this, I would just substitute (s/// read about it in perldoc perlop - it's quite an ubiquitous operator in Perl) "reports" for the wanted output directory. Then rumors have it that File::Path will automatically create directories recursively for you.