CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
dircopy maxdepth

 



AlexPerl
Novice

Apr 9, 2014, 12:04 PM

Post #1 of 10 (4533 views)
dircopy maxdepth Can't Post

Thanks in advance. I'm trying to copy some files and directories down a certain depth. Here is an example of my directory structure:

main_dir
---dir1
------txt1a
------txt1b
------txt_junk1
------dir1a
---dir2
------txt2a
------txt2b
------txt_junk2
------dir2a
---dir3
------txt3a
---file1
---file2

This is what I want when done copying:

main_dir
---dir1
------txt1a
------txt1b
---dir2
------txt2a
------txt2b
---dir3
------txt3a
---file1
---file2

Here is my complete code so far with an error: Name "File::Copy::Recursive::Maxdepth" used only once: possible typo at /home4/lang/c line 25.


Code
#!/bin/env perl5 

use strict;
use warnings;
use File::Copy::Recursive qw(dircopy);

my $path;

chomp ($path = `pwd`);

opendir my($dh), $path or die "Couldn't open dir '$path': $!\n";

my @directories = readdir $dh;

foreach my $directory (@directories)
{
if(-d $directory && $directory ne "." && $directory ne "..")
{
opendir my($dh_temp), $directory or die "Couldn't open dir '$directory': $!\n";
my @sub_directories = readdir $dh_temp;
print $directory."\n";
if ($#sub_directories > 1)
{
print "not empty .. $path\n";
$File::Copy::Recursive::Maxdepth = 1;
dircopy($directory, $path) or die "File cannot be copied: $!";
}
else
{
#print "empty\n";
}
closedir($dh_temp);
}
}

closedir($dh);



FishMonger
Veteran / Moderator

Apr 9, 2014, 12:33 PM

Post #2 of 10 (4529 views)
Re: [AlexPerl] dircopy maxdepth [In reply to] Can't Post

You have a typo.


Code
$File::Copy::Recursive::Maxdepth = 1;


Should be:

Code
$File::Copy::Recursive::MaxDepth = 1;


You code has other issues as well. Post back if you can't figure them out.


AlexPerl
Novice

Apr 9, 2014, 2:39 PM

Post #3 of 10 (4524 views)
Re: [FishMonger] dircopy maxdepth [In reply to] Can't Post

It didn't copy what I wanted and the directory structure is not what I want. Also the sub directories are empty.


Laurent_R
Veteran / Moderator

Apr 9, 2014, 2:59 PM

Post #4 of 10 (4520 views)
Re: [AlexPerl] dircopy maxdepth [In reply to] Can't Post


In Reply To
It didn't copy what I wanted and the directory structure is not what I want. Also the sub directories are empty.


Do you really think that this message is helping us to help you? We don't know for sure what you wanted not what you did, we have no idea of what you got, you don't tell us in which way the result is different from what you expect.

Please show what you did (your code and how you ran it), what the initial situation was (the dir structure at start) and what the final solution was.


AlexPerl
Novice

Apr 9, 2014, 4:16 PM

Post #5 of 10 (4516 views)
Re: [Laurent_R] dircopy maxdepth [In reply to] Can't Post

This is the directory structure that I start with (directories and files):

Code
/tmp/wip0 
---dir1
------dir1a
---------dir1ab
---------txt1ab
------txt1a
------txt1b
------junk1a
---dir2
------dir2a
------txt2a
------txt2b
------junk2a
---dir3
---txt1
---txt2
---chref.log


This is my code:

Code
#!/bin/env perl5 

use strict;
use warnings;
use File::Copy::Recursive qw(dircopy);

my $temp = "/tmp/temp";

chomp (my $path = `pwd`);

opendir my($dh), $path or die "Couldn't open dir '$path': $!\n";

my @directories = readdir $dh;

foreach my $directory (@directories)
{
if(-d $directory && $directory ne "." && $directory ne "..")
{
opendir my($dh_temp), $directory or die "Couldn't open dir '$directory': $!\n";
my @sub_directories = readdir $dh_temp;
print $directory."\n";
if ($#sub_directories > 1)
{
print " not empty ..\n";
$File::Copy::Recursive::MaxDepth = 1;
dircopy($directory, $temp) or die "File cannot be copied: $!";
}
else
{
print " empty\n";
}
closedir($dh_temp);
}
}

closedir($dh);


This is the result:

Code
/tmp/temp 
---dir1a
---dir2a
---junk1a
---junk2a
---txt1a
---txt1b
---txt2a
---txt2b


But what I was hoping to get is:

Code
/tmp/temp 
---dir1
------txt1a
------txt1b
---dir2
------txt2a
------txt2b
---txt1
---txt2



Chris Charley
User

Apr 9, 2014, 7:57 PM

Post #6 of 10 (4455 views)
Re: [AlexPerl] dircopy maxdepth [In reply to] Can't Post

I haven't used File::Copy::Recursive, so I can't comment there and haven't examined the logic of your script but I do see 1 problem. The line of code

Code
opendir my($dh_temp), $directory or die "Couldn't open dir '$directory': $!\n";

This has to be changed to "$path/$directory" instead of just $directory. readdir only returns the relative path, not the full path.

You might need to make a change also to

Code
dircopy($directory, $temp) or die "File cannot be copied: $!";

dircopy("$path/$directory", $temp) ...

Update Those changes would need to be made to the statement below

if(-d $directory && $directory ne "." && $directory ne "..")

if(-d "$path/$directory" && "$path/$directory" ne "." && "$path/$directory" ne "..")

(You might want to check out Path::Tiny instead of readdir, http://search.cpan.org/search?mode=all&query=Path%3A%3ATiny


(This post was edited by Chris Charley on Apr 10, 2014, 8:24 AM)


AlexPerl
Novice

Apr 10, 2014, 3:16 PM

Post #7 of 10 (3983 views)
Re: [Chris Charley] dircopy maxdepth [In reply to] Can't Post

over 8 hours (yeah im new to programming), I've managed to get a little closer to what I want. It copied the directory structure that I want, but on the second sub-directory it didnt copy the contents.


Code
#!/bin/env perl5 

use strict;
use warnings 'all';
use File::Copy::Recursive qw(dircopy);

my $temp = "/tmp/temp/empty_dir";
my $num_files_dirs = 12;
my $num_dirs = 12;
my $depth_traversed = 12;

chomp (my $path = `pwd`);

opendir my($dh), $path or die "Couldn't open dir '$path': $!\n";

my @directories = readdir $dh;

foreach my $directory (@directories)
{
if(-d $directory && $directory ne "." && $directory ne "..")
{
opendir my($dh_temp), $directory or die "Couldn't open dir '$directory': $!\n";
my @sub_directories = readdir $dh_temp;
print $directory."\n";
if ($#sub_directories > 1)
{
print " not empty ..\n";
$File::Copy::Recursive::MaxDepth = 2;
$File::Copy::Recursive::CPRFComp = 1;
my($num_files_dirs,$num_dirs,$depth_traversed) = dircopy($directory,$temp);
}
else
{
print " empty\n";
}
closedir($dh_temp);
}
}

closedir($dh);


INPUT:
/tmp/wip0
---drc
------dir1
---------txt1a
---------txt1b
------dir2
---------txt2a
---------txt2b

OUTPUT:
/tmp/temp/empty_dir
---drc
------dir1
---------txt1a
---------txt1b
------dir2
---------it didn't copy these files.. why?


Chris Charley
User

Apr 10, 2014, 6:17 PM

Post #8 of 10 (3917 views)
Re: [AlexPerl] dircopy maxdepth [In reply to] Can't Post

Well. I was wrong about the changes I recommended - didn't think it through :-(

Because the directories in @directories are relative to the path, all should work.

As I said, I've not used use File::Copy::Recursive so can't help there. Funny that your program prints the first and not the second directory contents.

Your statement 'if ($#sub_directories > 1)' would allow processing if there were 3 or more items in @sub_directories. Is it possible that there are 2 or less?

(Or did you want 'if ($#sub_directories > -1)' to indicate a nonempty array)


(This post was edited by Chris Charley on Apr 11, 2014, 9:15 AM)


AlexPerl
Novice

Apr 11, 2014, 10:38 AM

Post #9 of 10 (3793 views)
Re: [Chris Charley] dircopy maxdepth [In reply to] Can't Post

I tested with 4 directories, and the output is similar on each of those 4 directories, meaning only the dir1 contents get copied but not the dir2 contents.

The reason I have the if ($#sub_directories > 1) is to find non-empty directories and copy them. Don't copy empty directories and ignore empty directories.

Is there a way to ignore coping the junk files from the example below?

INPUT:
/tmp/wip0
---drc
------dir1
---------txt1a
---------txt1b
---------junk1a
------dir2
---------txt2a
---------txt2b
---------junk2a

Wanted OUPUT:
/tmp/wip0
---drc
------dir1
---------txt1a
---------txt1b
------dir2
---------txt2a
---------txt2b


AlexPerl
Novice

Apr 11, 2014, 11:02 AM

Post #10 of 10 (3785 views)
Re: [AlexPerl] dircopy maxdepth [In reply to] Can't Post

I have to write an if statement to sort out the files i want to copy and the files to ignore, but the main problem i still have is why my code only copy the dir1 contents but not the dir2 contents. I've tested out with 2, 3, 4, 5, or even 6 directories and they all fail the same way. Only the first sub-directory contents get copied but not the second, third, .. sub-directory contents.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives