how to move one file at a time to another folder

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jonathan184
    New Member
    • Nov 2006
    • 154

    how to move one file at a time to another folder

    how to move one file at a time to another folder.

    I am trying to one file at a time for every 30 minutes.

    So far the script is transferring all files at the same time and the source folder disappears afterwards. Could somebody help me out with this problem please.


    Code:
    #!/usr/bin/perl
    
    use warnings;
    #use strict;
    
    use Tie::File;
    use File::Copy;
    
    
    opendir(DIR, "/home/tibco/Susan/drop/") or die $!;
    
    @files = readdir(DIR);
    close(DIR); 
    
    foreach (@files) {
               
         chomp($_[0]);
       my $old = "/home/tibco/Susan/drop/$_[0]";
       my $new = "/home/tibco/AIS_FTP/3Com/inbound/Staging/";
    
         move($old, $new);    
         print "File Name: $_[0]  moved to FTP - 30 mins for next upload.\n\n";
         sleep 1800;
         
       print "Another is file uploading\n\n";
    
    if (@files eq"") {
       print "Script ended all files are in FTP.\n\n";
    exit;
    }
    }
  • miller
    Recognized Expert Top Contributor
    • Oct 2006
    • 1086

    #2
    Issues:
    #1 - Don't comment out "use strict"
    #2 - $_[0] is the wrong variable. Should be $_. If you are unsure about default variables, don't use them. Instead assign a name to things to avoid confusion.
    #3 - readdir will give you the default dir references '.' and '..'. This is the source of your problem with moving the source directory. You must grep those out.
    #4 - chomp'ing a filename serves no purpose.
    #5 - Your (@files eq "") serves no purpose. That is a string comparison and will always fail. If you want to test if @files contains anything just do if (@files). But even that statement is pointless as you are not changing the value of @files ever.

    General Readme:


    Code:
    #!/usr/bin/perl
    
    use warnings;
    use strict;
    
    use File::Copy;
    
    my $srcdir = "/home/tibco/Susan/drop/";
    my $dest = "/home/tibco/AIS_FTP/3Com/inbound/Staging/";
    
    opendir(DIR, $srcdir) or die "Can't open $srcdir: $!";
    @files = grep {!/^\.+$/} readdir(DIR);
    close(DIR);
    
    foreach my $file (@files) {
    	my $old = "$srcdir/$file";
    
    	move($old, $dest) or die "Move $old -> $dest failed: $!";
    	print "File Name: $file moved to FTP - 30 mins for next upload.\n\n";
    	sleep 1800; # 30 Minutes
    }
    The above is a cleaned up version of your code that should work now. I believe that most likely your logic is incomplete though. If you are waiting 30 minutes after each file, what happens if the source directory changes? I believe that you should search that directory before each move, which would make the code the following:

    Code:
    #!/usr/bin/perl
    
    use warnings;
    use strict;
    
    use File::Copy;
    
    my $srcdir = "/home/tibco/Susan/drop/";
    my $dest = "/home/tibco/AIS_FTP/3Com/inbound/Staging/";
    
    for (;;) { 
    	opendir(DIR, $srcdir) or die "Can't open $srcdir: $!";
    	@files = grep {!/^\.+$/} readdir(DIR);
    	close(DIR);
    
    	if (!@files) {
    		print "Script ended all files are in FTP.\n\n";
    		last;
    	}
    	
    	my $file = $files[0];
    	my $old = "$srcdir/$file";
    
    	move($old, $dest) or die "Move $old -> $dest failed: $!";
    	print "File Name: $file moved to FTP - 30 mins for next upload.\n\n";
    	sleep 1800; # 30 Minutes
    }

    Comment

    • jonathan184
      New Member
      • Nov 2006
      • 154

      #3
      Thank you for showing me the issues. That really helped out alot.
      I really appreciate your help.

      Comment

      Working...