the matt jones website.‎ > ‎Scripts‎ > ‎

Repeat Rename (perl)

This is a perl script that will rename a given file to another name on a loop. The script takes four arguments.

I am still working on this page.. any q's? E-mail me.

#!/usr/bin/perl



# By Matthew Jones, 2010.
# This script renames a given filename
# To another filename every N seconds,
# x number of times.

my $argNum = $#ARGV + 1;

if((($argNum=="1") && ($ARGV[0] eq "H")))
{
        print "Usage:\n";

        print "inputFileName outputFileName retrySeconds retryAttempts\n";
        print "inputFileName - a file name that will exist\n";
        print "outputFileName - output File Name\n";
        print "retrySeconds - wait time before next attempt\n";
        print "retryAttempts - how many times to do this rename\n";

}
elsif($argNum!="4")
{
        print "Not Enough Command Line Arguments! \n";
        print "Usage: inputFileName outputFileName retrySeconds retryAttempts\n";
}
elsif($argNum="4")
{
    # Code logic in here.
    # Variable decs from Command line inputs and variable checking.

    my $inputFile = $ARGV[0];
    my $outputFile = $ARGV[1];
    my $retrySeconds = $ARGV[2];
    my $retryAttempts = $ARGV[3];

    if ($retrySeconds!~ m/^\d+$/)  { print "RetrySeconds is not a number\n"; reportError(1); }
    if ($retryAttempts!~ m/^\d+$/) { print "RetryAttempts is not a number\n"; reportError(2); }
   
   
    # Rename of the file process below
    my $counter = 0;
    while($counter<$retryAttempts)
    {
        doRename($inputFile, $outputFile);
        sleep($retrySeconds);
    $counter++;
    }
}
else
{
        print "Something went wrong\n";
}

# SupProcesses for Execution.

sub doRename
{
    (my $inputFile, my $outputFile) = @_;
    if(((!-e $outputFile)) && (-e $inputFile))
    {
        rename($inputFile,$outputFile) || reportError(3);
    }   
}



# SubProcesses for Error Handling
sub reportError
{
    # Not Yet Implemented.
    (my $errorCode) = @_;
    die;
}

sub helpError
{
    # Not Yet Implemented.
    (my $errorCode) = @_;
}
Comments