Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re^4: How do I create folders by trimming the text

by kennethk (Abbot)
on Nov 10, 2010 at 18:04 UTC ( [id://870636]=note: print w/replies, xml ) Need Help??


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

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

Replies are listed 'Best First'.
Re^5: How do I create folders by trimming the text
by Anonymous Monk on Nov 10, 2010 at 18:56 UTC
    Thank you Sir for the very useful insight. I am going to try it out and let you know.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://870636]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (6)
As of 2024-04-19 10:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found