develooper Front page | perl.beginners | Postings from February 2009

Simplify a script which copies files

Thread Next
From:
Lauri Nikkinen
Date:
February 25, 2009 04:44
Subject:
Simplify a script which copies files
Message ID:
ba8c09910902250444u6c6f2985i821c9d8d49b43a4b@mail.gmail.com
Hi,

I wrote a script which copies files in directory to an another
directory based on user input. Any suggestions to simplify or shorten
this? I'm using Win XP.

-L

--------------------code -------------------------

#!/bin/perl

use warnings;
use strict;
use File::Copy;

my $dire;
my $destination;
my $days;

#Dir from where to copy
while (1){
  print "\nPath to dir for files to copy: ";
  chomp($dire = <STDIN>);
  #Check dir
  if ($dire !~ /^.+\/+$/){
    print "\nGive dir like C:/Temp/\n";
    next;
  }
  last;
}

#Dir to copy
while (1){
  print "\nPath to copy: ";
  chomp($destination = <STDIN>);
  #Check dir
  if ($destination !~ /^.+\/+$/){
    print "\nGive dir like C:/Temp/\n";
    next;
  }
  last;
}

#Days from this day to copy
while (1){
  print "\nPlease enter days from current date of modified files: ";
  chomp($days = <>);
  # Check for non-numeric characters
  if ($days =~ /\D/){
    print "\nPlease enter a number";
    next;
  }
  last;
}

print "\nCopied files:\n\n";

opendir DIR, $dire;
foreach my $filename (readdir(DIR)) {
       next if -d $filename;
       next if -f $filename and -M $filename > $days;
       print "$filename \n";
       copy($filename, $destination) or die "Couldn't copy: $!";
}

-------------------- end -------------------------

Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About