Jump to content
Cartman.

Perl script for making multiple copies

Recommended Posts

This is a simple perl script that will make several copies of a file and put it into a

directory of your choosing.

This will only work on a *NIX system for now. I am currently planning a Win version.

I am not a professional coder so my code may not be as elegant as it could be.

Save the code to your *NIX system and name it what ever you like, just be sure to

add '.pl ' as the extension.

Go to the directory you saved it in and open a terminal and type: perl -w filename.

Just follow the directions and your good to go.

I've tested it and it works for copying most file types, including image files.

Your millage may vary.

I've commented the code pretty well so it's easy to figure out.

I've used system(ls-xxx) for listing files as it tells you who owns the file and

lists the files permissions as this only works for *NIX this script won't run on Windows.

But that should be easy to change.

If you want to change the code to suite your needs be my guest, I only ask you post

any changes you made as I'm interested in learning different and better ways of doing things.

#!/usr/bin/perl

use 5.006;
use strict;
use warnings;
use Cwd;

#Variables
my @stars\n";
#End Banner







#Declare source dir function
sub source_dir{rr
print("Type a directory path for the source file:\n");
$sd=<STDIN>;
chomp $sd;
#Check if directory path is correct
if (opendir(SD, $sd)) { #If path is correct then procede
chdir $sd;
system("ls -l --group-directories-first"); #Use system call 'ls' to list files,*nix only
print "You are now in directory:", cwd, "\n";

print (" \nPlease type a source file name from the above list: \n");
$sf=<STDIN>; #User inputs file to copy
chomp($sf) #Remove \n character
}

else { #If path is not correct print error messg and re-start function
print("No such directory, please check the path and try again\n");
source_dir();
}
#Check if file name is correct
if (open(SF, $sf)) { #If file name is correct then procede to copy
print ("How many copies would like to make?\n");
$nc=<STDIN>; #User inputs # of copies to make
}

else { #If file name is not correct then...
print("Cannot find file, please check file name and re-submitt\n");
source_dir(); # Re-run function to get file name
}






}
#end function

#Declare destination dir function
sub des_dir{
print("Type a directory path to save the copies to:\n");# Input diretory path you want to save the copies to
$dd=<STDIN>;
chomp $dd;
if (opendir(DD, $dd)){

print "Your files have been saved to directory:", $dd, "\n";


}

else {
print("No such directory, please re-start and try again\n");#Re-run function if path is incorrect
des_dir();;
}
}
#End function

#Run functions
source_dir();
des_dir();


#Initiate count for loop
$count=0;
#Magic happens here in a 'while loop'
while ($count < $nc){
chdir $sd; #Change directory to soucre dir in order to read file for copying
open SF, $sf;
chdir $dd; #Chage directory to destination folder in order to send copied files
open (DF,">$sf($count)");#Opens Destination File Handler to write to new file and use the current value of '$count' to add to file name
print (DF <SF>); #Copies Source file to Destination file
$count++;
}
#End loop
chdir $dd; #Change dir to display new files
system("ls -l --group-directories-first");
print "\nSuccess!\n";
print "Check above list for your files, they will be listed as 'filename.ext(x)'\n";

#Close File and Dir handlers
closedir(DD);
closedir(SD);
close(SD);
close(SF);

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...