Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

how to copy source folder excluding specific set of sub folder from parent folder

by mrityunjaynath (Acolyte)
on Aug 09, 2016 at 06:06 UTC ( [id://1169383]=perlquestion: print w/replies, xml ) Need Help??

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

Hello every one, i am new to perl and i am writing a script to copy one folder and its sub folders and files to a target folder recursively. i have used

use File::Copy::Recursive qw(dircopy);
and used it as
my $file = dircopy(my $sourcedir, my $targetdir);
I am able to copy each file and sub folder in source folder to target folder but along with all subfolder one particular folder .svn is also getting copied to target folder location. .svn is present in the source folder but i dont want to exclude its copy to target folder. I have searched many links but havnt got any answer. please help...

Replies are listed 'Best First'.
Re: how to copy source folder excluding specific set of sub folder from parent folder
by hippo (Bishop) on Aug 09, 2016 at 08:12 UTC

    There might be some way to do this using File::Copy::Recursive but I could not spot it in the documentation.

    Contrarily, it is certainly possible with File::Rsync instead. This gives you most of the goodies available in rsync but with a Perlish interface so you can do both local and remote tree copies with exclusions.

      thanks hippo....will look into the material link you provided and try implement it and.... will reply soon

Re: how to copy source folder excluding specific set of sub folder from parent folder
by robby_dobby (Hermit) on Aug 09, 2016 at 07:57 UTC
    Hello mrityunjaynath,

    This is an SVN folder, right? If so, why not just use svn export $srcFolder $targetFolder. I may be wrong on the exact incantation to send to svn export but you get the idea - it'd strip out all .svn folders, exporting only the content at that revision.

    HTH. You may not really need perl for this, unless you plan to do it as an exercise. :-)

    EDIT:

    Here's a link to svn export from svn redbook.

      hi robby_dobby.... in continuation to the above querry i have written a code to copy a directory and delete .svn file in particular from the copy directory. havnt used file::Rsync option but written a subroutine to do so but its doing nothing and stucked in infinite loop. the directory is copied but after that when i called subrouting it was stuck in infinite loop showing - only ...the code is

      use strict; use warnings; use File::Copy; use File::Basename; use Cwd; use File::Copy::Recursive qw(dircopy) ; ############################################################ sub traversedir { my $givendir = $_[0]; opendir(my $findsvn , $givendir) or die "Couldn't read dir: $! \n +"; my @traversedfiles = readdir($findsvn); foreach my $searchfile (@traversedfiles) { $searchfile =~ /^\.svn$/; print "$searchfile\n"; rmdir $searchfile; if ( -d $searchfile) { &traversedir($searchfile); } else { last; } } } ############################################################ my $fullpath = cwd(); my $value = 0; my $file = basename($fullpath); my $dir = dirname($fullpath); print("\nWORK DIRECTORY PATH IS $dir \n"); opendir(my $pathindex, $dir ) or die "Couldn't read index : $!\n"; while (my $currentfile = readdir($pathindex)) { print "\nCurrent Directory File $value\t"; print "$currentfile","\n"; $value++; } print "\nENTER THE PROJECT NAME FROM ABOVE LIST\n\n"; my $projectdir = <STDIN>; chop($projectdir); my $projectdirpath = "/"."$projectdir"; my $workingdirectory = ($dir.$projectdirpath); chdir($workingdirectory."/trunk"); my $newpath = cwd(); my $topprojectdir = $newpath; my $source_dirrtl; my $target_dirrtl ; opendir(my $index, $newpath ) or die "Couldn't read index : $!\n"; while (my $file = readdir($index)) { if ($file eq "rtl" ) { if(chdir($newpath."/rtl")) { my $currentworkingdir = cwd(); $source_dirrtl = $currentworkingdir; $target_dirrtl = ("$newpath"."/rtl2"); } last; } elsif ($file eq "vhdl") { if(chdir($newpath."/vhdl")) { my $currentworkingdir = cwd(); $source_dirrtl = $currentworkingdir; $target_dirrtl = ("$newpath"."/vhdl2"); } last; } } closedir($index); my $source_dirsim = ("$newpath"."/sim"); my $target_dirsim1 = ("$newpath"."/sim2"); my $source_dirsynth; my $target_dirsynth1 ; opendir(my $indexs, $newpath ) or die "Couldn't read indexs : $!\n"; while (my $file = readdir($indexs)) { if ($file eq "par" ) { if(chdir($newpath."/par")) { my $currentworkingdir = cwd(); $source_dirsynth = $currentworkingdir; $target_dirsynth1 = ("$newpath"."/par2"); } last; } elsif ($file eq "synth") { if(chdir($newpath."/synth")) { my $currentworkingdir = cwd(); $source_dirsynth = $currentworkingdir; $target_dirsynth1 = ("$newpath"."/synth2"); } last; } } closedir($indexs); mkdir($target_dirrtl,0777); mkdir($target_dirsim1,0777); mkdir($target_dirsynth1,0777); my $filertl = dircopy($source_dirrtl, $target_dirrtl); my $filesim = dircopy($source_dirsim, $target_dirsim1); my $filesynth = dircopy($source_dirsynth, $target_dirsynth1); &traversedir($target_dirrtl);
      please suggest

        After
        sub traversedir { my $givendir = $_[0];
        insert
        print "traversedir $givendir\n";
        Probably you are stumbling over "." and ".." …
        Hi,

        soonix already pointed out the issue around . and .. directory traversals. You'll need to keep track of visited directories to avoid looping over them again. There's also one more path that you can look into:

        $searchfile =~ /^\.svn$/; print "$searchfile\n"; rmdir $searchfile; if ( -d $searchfile) { &traversedir($searchfile); } else { last; }

        Now, this is not necessarily a problem - but you are not really checking for .svn folders, merely matching and overwriting the $searchfile variable. What do you think would happen if there were no match? :-)

        That said, what's wrong with just doing svn export $srcFolder $targetFolder? It doesn't look like you're doing it as an exercise (those vhdl, rtl, sim folders?) If you have svn installed, you already have this tool at your disposal. Why reinvent the wheel? :-)

      thanks robby_dobby for replying....yes i am trying to copy svn folders from my local directory to a target directory to do some experiment on target directory...... will try your suggestion and will reply u soon

Re: how to copy source folder excluding specific set of sub folder from parent folder
by Marshall (Canon) on Aug 09, 2016 at 06:51 UTC
    I presume that this is a typo: ".svn is present in the source folder but i dont want to exclude include its copy to target folder.

      thanks for replying marshall...you are right ...dont want to include .svn in the target folder

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (2)
As of 2024-04-19 19:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found