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

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.
  • Comment on Re^2: How do I create folders by trimming the text

Replies are listed 'Best First'.
Re^3: How do I create folders by trimming the text
by Anonymous Monk on Nov 10, 2010 at 14:43 UTC
    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
        Thank you Sir for the very useful insight. I am going to try it out and let you know.
Re^3: How do I create folders by trimming the text
by Anonymous Monk on Nov 09, 2010 at 16:29 UTC
    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.