#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    14
    Rep Power
    0

    Find the specific directory from the root and create a new directory at the same loc.


    Hi Perl team,

    I need to find the specific directory from the root and create a new directory at the same location and after that i need to copy the content of previous directory to my new directory.

    for Ex: I need to find a directory mooc-main and it also has other sub directories like abc,def and some files.

    So i need to find mooc-main and create a new directory with name as mooc-main-9 at the same place where the mooc-main is and copy the content of mooc-main to it.

    My code is below but in my code I getting a string consists of all path as one string in variable $file from function process and due to which I was unable to create the new directories.

    my $rootdir = $ARGV[0]; # Project Root Directory Path
    my $newdirname = $ARGV[1];
    my @dirlist = ();
    #find(sub {push(@dirslist,$File::Find::name) if (-d )}, $rootdir);

    find(sub {
    return unless -d;
    if (basename($File::Find::dir) =~ /mooc-main/) {
    process($File::Find::name);
    }
    }, $rootdir);

    sub process {
    my $file = shift;
    push(@dirlist,$file);
    #print $file, "\n";
    }

    foreach my $dir (@dirlist) {
    $dir =~ s/mooc-main/$newdirname/i;
    $dir =~ s/\//\\/g;
    print $dir;
    mkdir($dir) or die "can't chdir $dir\n";
    chdir($dir) or die "can't chdir $dir\n";
    #print "new dir created is $dir \n";
    }

    Please help!!!


    Thanks!!
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2012
    Posts
    835
    Rep Power
    496
    Just for the sake of visibility for the next people visiting this post, I add the code of the previous post with the right tags.

    Perl Code:
    my $rootdir = $ARGV[0];  # Project Root Directory Path
    my $newdirname = $ARGV[1];
    my @dirlist = ();
    #find(sub {push(@dirslist,$File::Find::name)  if (-d )}, $rootdir);
     
    find(sub {
      return unless -d;
      if (basename($File::Find::dir) =~ /mooc-main/) {
        process($File::Find::name);
      }
    }, $rootdir);
     
    sub process {
      my $file = shift;
      push(@dirlist,$file);
      #print $file, "\n";
    }
     
    foreach my $dir (@dirlist) {
    	$dir =~ s/mooc-main/$newdirname/i;
    	$dir =~ s/\//\\/g;
    	print $dir;
    	mkdir($dir) or die "can't chdir $dir\n";
    	chdir($dir) or die "can't chdir $dir\n";
    	#print "new dir created is $dir \n";
    	}


    @vaibhav.shukla: please, next time try to make the effort of using the right tags.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    14
    Rep Power
    0
    Originally Posted by Laurent_R
    Just for the sake of visibility for the next people visiting this post, I add the code of the previous post with the right tags.

    Perl Code:
    my $rootdir = $ARGV[0];  # Project Root Directory Path
    my $newdirname = $ARGV[1];
    my @dirlist = ();
    #find(sub {push(@dirslist,$File::Find::name)  if (-d )}, $rootdir);
     
    find(sub {
      return unless -d;
      if (basename($File::Find::dir) =~ /mooc-main/) {
        process($File::Find::name);
      }
    }, $rootdir);
     
    sub process {
      my $file = shift;
      push(@dirlist,$file);
      #print $file, "\n";
    }
     
    foreach my $dir (@dirlist) {
    	$dir =~ s/mooc-main/$newdirname/i;
    	$dir =~ s/\//\\/g;
    	print $dir;
    	mkdir($dir) or die "can't chdir $dir\n";
    	chdir($dir) or die "can't chdir $dir\n";
    	#print "new dir created is $dir \n";
    	}


    @vaibhav.shukla: please, next time try to make the effort of using the right tags.
    Thanks Laurent_R

IMN logo majestic logo threadwatch logo seochat tools logo