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

Hello PerlMonks, I have a problem where i have 2 variable holding the directory location say
$dir1 = "c:\temp"; $dir2 = "c:\docs";
i have a script which walks recursivley through c:\temp This directory has many sub-directories within sub-directories and files and extracts a file. I have to extract the file and place it in c:\docs and at the same time maintain the same directory structure. That is if the file is present in c:\temp\in-temp\file1.txt then this file file1.txt has to be extracted to c:\docs\in-temp\file1.txt. I do not know how to go about extracting only in-temp and creating it in c:\docs\ and then putting the file. I would be extremely grateful if you could please help me to do this.

Replies are listed 'Best First'.
Re: Directory operations
by xdg (Monsignor) on Nov 08, 2005 at 15:07 UTC

    Assuming that you've found the file and have the directory name separate from the basename (File::Basename), I'd use a regex to replace "c:\temp" with "c:\docs" and then use mkpath from File::Path to create the target destination. Then copy the file over from one to the other. (See read_file and write_file from File::Slurp or perhaps just File::Copy.)

    You may also want to consider using Path::Class for your path/filename handling and recursive searching, at it wraps useful functionality from several of the other modules I mentioned.

    -xdg

    Code written by xdg and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.

Re: Directory operations
by jbrugger (Parson) on Nov 08, 2005 at 07:17 UTC
    Since i don't have a clue how your script looks and what your exact problem is (eg. don't you know how to create a directory, don't you know how to keep track of your files/folders you want to copy / create), i assume the first and suggest you start to do some reading, and look at How (Not) To Ask A Question to help you a bit further.
    To create a folder use mkdir.
    mkdir("/path/to/new/dir");
    "We all agree on the necessity of compromise. We just can't agree on when it's necessary to compromise." - Larry Wall.
A reply falls below the community's threshold of quality. You may see it by logging in.