in reply to Re^3: how to move the folder from one directory to another directory?
in thread how to move the folder from one directory to another directory?

I can't say the name of the folder as it will be created dynamically with the time stamp.... So how can we move that folder without knowing the name of the folder
  • Comment on Re^4: how to move the folder from one directory to another directory?

Replies are listed 'Best First'.
Re^5: how to move the folder from one directory to another directory?
by pme (Monsignor) on Dec 28, 2014 at 10:00 UTC
    Hi vasuperl,

    What can be found in that directory besides the session folder? Is it the only file/folder in the directory?

    Update: You can list all directories in the path_to_folder.

    foreach (<path_to_sessions/*>) { if ( -d ) { print "$_\n"; move(... } }
    Additional filtering can be addded to the file glob and/or regexp match can be added to the if statement.
Re^5: how to move the folder from one directory to another directory?
by james28909 (Deacon) on Dec 28, 2014 at 08:05 UTC
    It is a very simple solution. All you need to do is read an original directory. Then read your new directory. Then If new directory does not contain a file/folder in original directory, then copy it.

    Here is what i came up with a sec ago:
    use strict; use warnings; use File::Copy::Recursive qw(rcopy); use File::Slurp; use Cwd; my $cwd = cwd(); my $dir = $ARGV[0]; my $orig_dir = "$cwd/$dir"; my $new_dir = "$cwd/your/path/"; my @first_scan = read_dir($orig_dir); #this will build an initial l +ist #and put it into @first_scan; re_scan(); sub re_scan { #to rescan over and over whil +e comparing my @new_scan = read_dir($new_dir); foreach my $element (@first_scan) { if ( $element ~~ @new_scan ) { print "$element Already Exists!\n"; } else { print "Copying $element to $new_dir\n"; rcopy( $orig_dir . $element, $new_dir . $element ); } } undef(@first_scan); for (@new_scan) { push( @first_scan, $_ ); } undef(@new_scan); sleep(3); ; re_scan(); }
    This should work for your scenario as you just need to scan the directory, and if the new directory does not contain somethign that the old dir has in it, it copies it.
        Yeah most of the time i dont read a complete file into memory anyway, and in the given example, it uses file copy recursive. File slurp does not do anything except make reading a directory easier and so far it hasnt given me unexpected results (not saying it never will though).

      This code will also fill up the stack as re_scan calls itself endlessly (Perl should even have warned you about "Deep recursion"). I'd really recommend you read up on recursion (e.g. Modern Perl: Recursion) and how to turn it into iteration, for example here: http://www.refactoring.com/catalog/replaceRecursionWithIteration.html

        I think i understand what your saying. I could put all this into an endless loop and not call sub functions (recurse) over and over. Is that what you mean?
Re^5: how to move the folder from one directory to another directory?
by Anonymous Monk on Dec 28, 2014 at 10:14 UTC

    I can't say the name of the folder as it will be created dynamically with the time stamp.... So how can we move that folder without knowing the name of the folder

    Hmm, first step, find out the name, path( $folder )->children