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

Hi All, I am very weak in Perl and need some help. I need to modify an existing script.

From the script, I get the output as follows:

.\folder1\folder11\elementA.xml
.\folder1\folder11\elementB.xml
.\folder2\folder22\element1.xml
.\folder2\folder22\folder222\element11.xml
.
.

I need to parse this path and create the folder structure on the destination and then copy the elements appropriately in those folders, i.e. I need a folder1 created and folder11 created under folder1 and elementA.xml and elementB.xml copied in folder11 and so forth...

How do I achieve this? Thanks in advance.
  • Comment on How do I create folders by trimming the text

Replies are listed 'Best First'.
Re: How do I create folders by trimming the text
by Corion (Patriarch) on Nov 09, 2010 at 16:07 UTC

    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.

      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.
        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.