So I am writing a script that will backup the given file(s) to a Backups directory. I have the portion working for checking for the directory and creating a new one if it doesn't exist, as well as copying the original file to the directory with a new name.
What I want to be able to do now is do this for multiple files that are specified either with ARGV, or if the user forgets that, from either a comma or space separated list from STDIN. I'm not sure how to do this part however. Do I somehow do this with a foreach loop, or am I supposed to use a while? Do I need to push STDIN to an array using the comma or space as the delimiter?
Here's an example of my current code:
my $filename = shift @ARGV; unless ($filename) { print "Enter file name(s) to be backed up:\n"; $filename = <STDIN>; chomp $filename; } if ( -e $filename ) { if ( -d "Backups") { # copy file to directory } else { mkdir "Backups"; # copy file to directory } }
Thanks for any help you can provide.
Edit: I now have the script working to read multiple files from the command line, and I can take the space separated list from STDIN and input into an array. I still cannot figure out how to get it to step through each element of the STDIN array like I have it working for ARGV.
My if statements have been moved into a while loop: while ($filename). I added $filename = shift before I close out the while loop. The initial portion of my code has been updated to this (second try, this time trying an if/else):
if (!@ARGV) { print "Enter file name(s) to be backed up, separated by spaces:\n" +; $filename = <STDIN>; chomp $filename; my @filenames = split(/\ /, $filename); $filename = shift(@filenames); } else { $filename = shift(@ARGV); }
In reply to Process multiple filenames from command line or STDIN by Katanya
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |