in reply to How do I create folders by trimming the text

See File::Path for creating (multiple levels of) directories and File::Copy for copying files.

Also, a system call to cp -rp or xcopy might be faster if the structure already exists somewhere.

Replies are listed 'Best First'.
Re^2: How do I create folders by trimming the text
by kennethk (Abbot) on Nov 09, 2010 at 16:11 UTC
    I would add File::Spec (particularly splitpath) if you need to parse out information about paths, e.g. splitting the directory path from the file name.
      Tried:
      ($directory1,$directory2,$filename)=splitpath( $file ); print "directory1=\'".$directory1."\'directory2=\'".$directory2."\'filename=\'".$filename."\'\n";

      Got:

      directory1=''directory2='.\folder1\folder11\'filename='elementA.xml'

      I am looking to get: directory1='folder1'directory2='folder11'filename='elementA.xml'

      Appreciates your help.
        Did you bother to read the documentation? From File::Spec:
        splitpath

        Splits a path in to volume, directory, and filename portions. On systems with no concept of volume, returns '' for volume.

        ...

        splitdir

        The opposite of catdir().

        @dirs = File::Spec->splitdir( $directories );

        As well, please read How do I post a question effectively?. When you post code, input and output, wrap it in <code> tags so that formatting and whitespace is maintained.

        Consider the following script:

        #!/usr/bin/perl use strict; use warnings; use File::Spec::Functions qw(splitpath splitdir); my @files = qw( .\folder1\folder11\elementA.xml .\folder1\folder11\elementB.xml .\folder2\folder22\element1.xml .\folder2\folder22\folder222\element11.xml ); for my $file (@files) { my ($volume, $directory, $filename) = splitpath($file); print <<EOT; Input: $file Volume: $volume Directory: $directory Filename: $filename splitdir gives: EOT for my $dir ( splitdir $directory) { print "$dir\n"; } print "\n"; }

        outputs:

        Input: .\folder1\folder11\elementA.xml Volume: Directory: .\folder1\folder11\ Filename: elementA.xml splitdir gives: . folder1 folder11 Input: .\folder1\folder11\elementB.xml Volume: Directory: .\folder1\folder11\ Filename: elementB.xml splitdir gives: . folder1 folder11 Input: .\folder2\folder22\element1.xml Volume: Directory: .\folder2\folder22\ Filename: element1.xml splitdir gives: . folder2 folder22 Input: .\folder2\folder22\folder222\element11.xml Volume: Directory: .\folder2\folder22\folder222\ Filename: element11.xml splitdir gives: . folder2 folder22 folder222
      I just tried, excellent command for parsing out the path from the element. However I want to do the opposite. I would like to create the folder structure from the path and then copy the elements into it (that I can do with my existing code). I just need help in splitting the folders and creating them from the path. Thanks.
        I just need help in splitting the folders and creating them from the path
        Did you read Corion's linked documentation? File::Spec's splitpath and splitdir extract the folder names. File::Path's make_path creates folders from a path.