Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

File Backup Utility

by misterMatt (Novice)
on Jul 26, 2009 at 03:04 UTC ( #783254=sourcecode: print w/replies, xml ) Need Help??
Category: Utility Scripts
Author/Contact Info Matthew Young mabufo@gmail.com
Description:

I wrote this utility to assist me in backing up files from my hard drive to an external drive. This is my first perl script!

I tested this on the latest activeperl installation on windows vista. This script requires File::Copy::Recursive.

If anyone has suggestions on how to improve this script either by code improvements, or feature additions post your ideas here! I would appreciate it. Sorry about the formatting.

=head1 NAME

File autobackup utility

=head1 SYNOPSIS

  autobackup -d:
          The d flag backs up files to/from the locations hardcoded in
+to 
          the script. Only use this if you have edited in your own loc
+ations.          
  autobackup -h:
          The h flag displays usage information. Use it for instructio
+ns.
  
  autobackup -p FILEPATH:
          The p flag should be used if you have not set up the script 
+to work with the d flag. 
          When calling the script with the p flag, you should specifiy
+ the filepath of the files you want 
          to be copied as an argument: "autobackup -p C:\myfiles". You
+ will then be asked for a target 
          location for the files in the directory that you specified w
+hen you called the program. 
          
  I'M ASSUMING WE ALL KNOW HOW TO ENTER PROPPER FILEPATHS... 

=head1 DESCRIPTION

In a nutshell, this script copies files from one location to another. 
I wrote this script to transfer files from my computer to an external 
+drive easily more easily.

=head1 DEPENDENCIES

This script requires the File::Copy::Recursive module. 

=head1 AUTHOR

Matthew Young (mabufo@gmail.com) [www.theangrywalrus.com/blog]

=cut


#!usr/bin/perl
use strict;
use warnings;
use File::Copy::Recursive qw(dircopy);
use Getopt::Std;
use Pod::Usage;

#--------file copy vars (uncomment if you want)
#local $File::Copy::Recursive::SkipFlop = 1; #keeps the script going i
+f somethign fails
#$File::Copy::Recursive::MaxDepth #you can set this to change the maxi
+mum depth of the copy.

#I get to play with hashes a bit...
#grabs our argv values and stores them in a hash (key is the flag, val
+ue is the actual argument)
my %options=();

getopts('dp:h', \%options); 
pod2usage(2) if (keys %options != 1);

if ($options{d}){
    #if 'default' flag: 
    #paths can be changed by editing what's in qw() - on the left is l
+ocation of files, 
    #on the right is where you want to put them.
    my ($original, $targetlocation) = qw/C:\test C:\test1/;
    my ($numberoffiles, $numberofdirs, $depth) = dircopy($original, $t
+argetlocation) or die $!;
    print "$numberoffiles files copied, across $numberofdirs directori
+es, at a level of depth of $depth.";
}
if ($options{p}){
    #if 'path' flag:
    my $original = $options{p};
    print "Target directory: ";
    my $targetlocation = <STDIN>;
    chomp $targetlocation;
    my ($numberoffiles, $numberofdirs, $depth) = dircopy($original, $t
+argetlocation) or die $!;
    print "$numberoffiles files copied, across $numberofdirs directori
+es, at a level of depth of $depth.";
}
if ($options{h}){
    pod2usage(2); #YES!
}
Once again I apologize for the formatting - it looks a lot better inside of eclipse :). This is my first actual script - so if you guys see any glaring errors or bad practices - point them out! EDIT: Now with Pod::Usage!!
Replies are listed 'Best First'.
Re: File Backup Utility
by toolic (Bishop) on Jul 26, 2009 at 03:18 UTC
    Rather than rolling your own usage handling, consider the core module Pod::Usage. Format that really long help documentation print line as POD, and you're ready to go. POD turns your useful header comments into a manpage using perldoc.

    Another alternative is to use a "here-document" (see perlop).

    Add use warnings;.

    You could check the return status of getopts and die if you do not want the user to pass bogus options.

    Update:

    scalar @ARGV < 0
    I do not think this will ever evaluate to 'true' because I'm not sure if it is possible for an array to have a negative number of elements. Maybe scalar @ARGV == 0?
      Ahh yes, that should definitely be ==!
        and thank you very much for linking that stuff related to usage handling - I had no idea that even existed.
Re: File Backup Utility
by graff (Chancellor) on Jul 26, 2009 at 21:12 UTC
    As you get acquainted with Pod::Usage, consider the normal layout of manual pages, and use it. You'll find that Pod::Usage::pod2usage provides the means to display just the SYNOPSIS portion or the whole manual. As for testing that the user gives you exactly one command-line "option":
    #!/usr/bin/perl =head1 NAME name_of_utility_or_module =head1 SYNOPSIS brief example/template/summary of typical use(s), e.g.: prog_name -d prog_name -h prog_name -p foobar =head1 DESCRIPTION Paragraphs that describe the purpose, usage and various behaviors of the utility or module in suitable detail... =cut use strict; use Getopt::Std; use Pod::Usage; my %opt; getopts('dp:h', \%opt); pod2usage(2) if (keys %opt != 1); # die here if too many options were + given print "ok, here we go...\n"; if ( $opt{d} ) { # do this.. } if ( $opt{p} ) { # do that... } if ( $opt{h} ) { pod2usage(-exitstatus => 0, -verbose => 2); # exit & show full man + page }
      Thank you very much for that reply. That was extremely helpful! I checked the perldoc page, but to me it wasn't really clear how to implement the whole POD thing. I'll give it a try!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: sourcecode [id://783254]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (5)
As of 2023-11-30 04:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?