in reply to Re: how to copy source folder excluding specific set of sub folder from parent folder
in thread how to copy source folder excluding specific set of sub folder from parent folder

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

  • Comment on Re^2: how to copy source folder excluding specific set of sub folder from parent folder
  • Download Code

Replies are listed 'Best First'.
Re^3: how to copy source folder excluding specific set of sub folder from parent folder
by soonix (Chancellor) on Aug 11, 2016 at 09:43 UTC
    After
    sub traversedir { my $givendir = $_[0];
    insert
    print "traversedir $givendir\n";
    Probably you are stumbling over "." and ".." …
Re^3: how to copy source folder excluding specific set of sub folder from parent folder
by robby_dobby (Hermit) on Aug 11, 2016 at 10:14 UTC
    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? :-)

      But he would have to be concerned about "ignored" files, wouldn't he?
        Well, if all his "ignored" files were about svn metadata folders and files(.svn folders), why would he need to reimplement svn export?