http://www.perlmonks.org?node_id=1054610

Jalcock501 has asked for the wisdom of the Perl Monks concerning the following question:

Hey up Guys I'm trying to write a script that searches for certain files on a CD copies them to a temp directory and then renames them and puts them in the correct directory, but the script doesn't work it copied the files to temp but then just errors.
#!/usr/bin/perl -w use strict; use File::Copy; use warning; system("clear"); system("mount /dev/cd0 /mnt"); system("cp /mnt/* /usr/castle/pctemp/."); system("umount /mnt"); my $file; my $solar = "SO_PC"; my $stdpc = "MS_PC"; my $cv = "SO_CV"; my $mb = "ms_mc"; print("\n\n\n\n\n\n\t\t\tEnter File Name:"); my $input = <STDIN>; chomp($input); $file = $input; #Solar PC if(index(uc($file), uc($solar)) != -1) { print "Copying $file to Quotes Engine"; copy($file, "PCMAP.BIN"); system("mv PCMAP.BIN /usr/castle/np_new"); } #Standard PC elsif(index(uc($file), uc($stdpc)) != -1) { print "Copying $file to Quotes Engine"; copy($file, "FULLPC.BIN"); system("mv FULLPC.BIN /usr/castle/np_new"); } # CV elsif(index(uc($file), uc($cv)) != -1) { print "Copying $file to Quotes Engine"; copy($file, "CVFULLPC.BIN"); system("mv CVFULLPC.BIN /usr/castle/np_new"); } #MB elsif(index(uc($file), uc($mb)) != -1) { print "Copying $file to Quotes Engine"; copy($file, "MBFULLPC.BIN"); system("mv MBFULLPC.BIN /usr/castle/np_new"); }
here is my complete code. Could someone please have a look through and help as I have been stuck for ages! Thanks Guys

Replies are listed 'Best First'.
Re: Filename Partial Match function
by Eily (Monsignor) on Sep 18, 2013 at 09:09 UTC

    Why do you copy your file and then move it? It would be simpler to just copy it directly to the right destination. By the way, File::Copy provides both a copy and a move function.

    The fact that you had to copy and paste the same code 4 times should have been a good clue that you could have done it better. And if you are just going to use index to know if a string is included in the other, a regex could do the trick, with case insensitivity made easy.

    use strict; use File::Copy; use warning; system("clear"); system("mount /dev/cd0 /mnt"); system("cp /mnt/* /usr/castle/pctemp/."); system("umount /mnt"); my %dest = (SO_PC => 'PCMAP.BIN', MS_PC => 'FULLPC.BIN', SO_CV => 'CVFULLPC.BIN', MS_MC => 'MBFULLPC.BIN'); print "\n" x 5, "Input required:\n"; my $fileName = <STDIN>; chomp $fileName; for my $search (keys %dest) { die "$fileName does not exist!" unless -e $fileName; copy ($fileName, '/usr/castle/np_new/'.$dest{$search}) if $fileName + =~ /\Q$search/i; }

Re: Filename Partial Match function
by choroba (Cardinal) on Sep 18, 2013 at 08:33 UTC
    then just errors.
    Can you copy and paste the exact error message as well?

    What do you supply as input to the script?

    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
      Unfortunately I cannot remember what the error message was as it is currently on a test server that is unreachable (when I have access, sometime this morning, I will update this post with it). However the input should always be one of the four following:
      SO_PC###.BIN MS_PC###.BIN SO_CV###.BIN MS_MC###.BIN
      The hashes represent unique identifier which are subject to change. So I need to search based on the first 5 chars as they are all BIN files.