Connecting Tech Pros Worldwide Help | Site Map

how to move one file at a time to another folder

Member
 
Join Date: Nov 2006
Location: USA
Posts: 126
#1: Jan 30 '07
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.


Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2.  
  3. use warnings;
  4. #use strict;
  5.  
  6. use Tie::File;
  7. use File::Copy;
  8.  
  9.  
  10. opendir(DIR, "/home/tibco/Susan/drop/") or die $!;
  11.  
  12. @files = readdir(DIR);
  13. close(DIR); 
  14.  
  15. foreach (@files) {
  16.  
  17.      chomp($_[0]);
  18.    my $old = "/home/tibco/Susan/drop/$_[0]";
  19.    my $new = "/home/tibco/AIS_FTP/3Com/inbound/Staging/";
  20.  
  21.      move($old, $new);    
  22.      print "File Name: $_[0]  moved to FTP - 30 mins for next upload.\n\n";
  23.      sleep 1800;
  24.  
  25.    print "Another is file uploading\n\n";
  26.  
  27. if (@files eq"") {
  28.    print "Script ended all files are in FTP.\n\n";
  29. exit;
  30. }
  31. }
miller's Avatar
Moderator
 
Join Date: Oct 2006
Location: San Francisco, CA
Posts: 830
#2: Jan 30 '07

re: how to move one file at a time to another folder


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:
http://perldoc.perl.org/File/Copy.html

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2.  
  3. use warnings;
  4. use strict;
  5.  
  6. use File::Copy;
  7.  
  8. my $srcdir = "/home/tibco/Susan/drop/";
  9. my $dest = "/home/tibco/AIS_FTP/3Com/inbound/Staging/";
  10.  
  11. opendir(DIR, $srcdir) or die "Can't open $srcdir: $!";
  12. @files = grep {!/^\.+$/} readdir(DIR);
  13. close(DIR);
  14.  
  15. foreach my $file (@files) {
  16.     my $old = "$srcdir/$file";
  17.  
  18.     move($old, $dest) or die "Move $old -> $dest failed: $!";
  19.     print "File Name: $file moved to FTP - 30 mins for next upload.\n\n";
  20.     sleep 1800; # 30 Minutes
  21. }
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:

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2.  
  3. use warnings;
  4. use strict;
  5.  
  6. use File::Copy;
  7.  
  8. my $srcdir = "/home/tibco/Susan/drop/";
  9. my $dest = "/home/tibco/AIS_FTP/3Com/inbound/Staging/";
  10.  
  11. for (;;) { 
  12.     opendir(DIR, $srcdir) or die "Can't open $srcdir: $!";
  13.     @files = grep {!/^\.+$/} readdir(DIR);
  14.     close(DIR);
  15.  
  16.     if (!@files) {
  17.         print "Script ended all files are in FTP.\n\n";
  18.         last;
  19.     }
  20.  
  21.     my $file = $files[0];
  22.     my $old = "$srcdir/$file";
  23.  
  24.     move($old, $dest) or die "Move $old -> $dest failed: $!";
  25.     print "File Name: $file moved to FTP - 30 mins for next upload.\n\n";
  26.     sleep 1800; # 30 Minutes
  27. }
Member
 
Join Date: Nov 2006
Location: USA
Posts: 126
#3: Jan 30 '07

re: how to move one file at a time to another folder


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