Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

glob a folder of images, and open text doc, find the matches and move the images.

by flieckster (Scribe)
on Feb 09, 2020 at 18:45 UTC ( [id://11112693]=perlquestion: print w/replies, xml ) Need Help??

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

Hello, i'm a little stuck here. I'm working on a script that is designed to glob a folder of images, then open a txt document with matches to the the filenames found, and move said files to the paths in the text document. the code below is a little odd off. when i glob the filenames i get a list of files
010011510005801_0669_main.psd 010011510005801_0964_main.psd 010011520006618_0964_main.psd 010011520006618_5030_main.psd 010011520006812_0670_main.psd 010011520006812_0990_main.psd
and here is the same filenames with paths where to put back the images.
/Volumes/photorepos/Partners/Christopher_Banks/post/CB_instock/4/01001 +1510005801_0669_main.psd /Volumes/photorepos/Partners/Christopher_Banks/post/CB_instock/4/01001 +1520006618_5030_main.psd /Volumes/photorepos/Partners/Christopher_Banks/post/CB_instock/4/01001 +1520006812_0670_main.psd /Volumes/photorepos/Partners/Christopher_Banks/post/CB_instock/9/01001 +1510005801_0964_main.psd /Volumes/photorepos/Partners/Christopher_Banks/post/CB_instock/9/01001 +1520006618_0964_main.psd /Volumes/photorepos/Partners/Christopher_Banks/post/CB_instock/9/01001 +1520006812_0990_main.psd
but when i run the script below, i can't seem to chomp the paths correctly so its searching line by line. insted it is searching the entire string, and removing parts of the filename in the paths. see below 010011510005801, should be 010011510005801_0669_main.psd
010011510005801_0669_main.psd /Volumes/photorepos/Partners/Christopher_Banks/post/CB_instock/4/01001 +1510005801/Volumes/photorepos/Partners/Christopher_Banks/post/CB_inst +ock/4/010011520006618/Volumes/photorepos/Partners/Christopher_Banks/p +ost/CB_instock/4/010011520006812/Volumes/photorepos/Partners/Christop +her_Banks/post/CB_instock/9/010011510005801/Volumes/photorepos/Partne +rs/Christopher_Banks/post/CB_instock/9/010011520006618/Volumes/photor +epos/Partners/Christopher_Banks/post/CB_instock/9/010011520006812_099 +0_main.psd
#!/usr/bin/env perl -w use File::Find; use File::Copy; use File::Basename; use File::Basename; use File::Slurp; use POSIX qw(strftime); my $date = strftime("%m-%d-%y",localtime); my $time = strftime("%I:%M:%S",localtime); my $findme = "/Volumes/photorepos/Perl/SLAperlDropback/"; my $file_loc = $findme. '2SLAday'; my $filenames_to_putback = '/Volumes/photorepos/Perl/SLAperlDropback/d +ropback.txt'; chdir($file_loc) or warn "$!"; @filesforeach = glob "*.*"; my $filecount = @filesforeach; print "$filecount\n"; chomp @filesforeach; foreach $file (@filesforeach){ print "$file\n"; open(my $fh, "<", "$filenames_to_putback") or warn "$!\n"; while (my $row = <$fh>) { chomp $row; print "$row\n"; my @line = $file; for (@line) { if ($_ =~ /$row/) { chomp $_; # push @output, "found $_ that matched redo $file $row <br>"; move($_,$row) or warn "shit went wrong yo! $!\n"; } } } } # print "@output\n";

Replies are listed 'Best First'.
Re: glob a folder of images, and open text doc, find the matches and move the images.
by haukex (Archbishop) on Feb 09, 2020 at 20:42 UTC
    my @line = $file; for (@line) { ...

    This is basically just using $_ instead of $file. Is that really what you want to do? In fact, there are a lot of intermediate variables that could be removed to simplify the code. AFAICT, the following does exactly the same as the code you posted. See how much nicer of a SSCCE this is? If you edit your code down like this, I'll help us immensely, and it may even help you find the bug in the process.

    #!/usr/bin/env perl use warnings; use strict; use File::Copy 'move'; my $findme = '/Volumes/photorepos/Perl/SLAperlDropback/'; chdir($findme.'2SLAday') or die "chdir: $!"; foreach my $file (glob "*.*") { open my $fh, '<', $findme.'dropback.txt' or die "open: $!"; while ( my $row = <$fh> ) { chomp $row; if ( $file =~ /$row/ ) { move( $file, $row ) or die "move: $!"; } } }

    From this code, I see a couple of things:

    • Unless what's in dropback.txt are regular expressions, you probably should say /\Q$row/ (see quotemeta).
    • You don't show the contents of dropback.txt. Since $file will probably be plain filenames, if $row is a full pathname, note that if you're checking whether $row contains $file, you've got the regex reversed, and it should be $row =~ /\Q$file/, perhaps with an anchor or other things to prevent it from accidentally matching a string that's just part of a pathname. Or, even better, you already loaded File::Basename but didn't use it - use its fileparse to strip the pathname off, and then do a plain eq on the filenames.
    • You use chomp in several places where it isn't needed. If you're worried about excess whitespace, see the corresponding points in the Basic debugging checklist - use either Data::Dumper with $Data::Dumper::Useqq=1;, or use Data::Dump.

    If that's not the advice you're looking for, then please better explain the issue with the code, i.e. provide some short sample data and show the values of the variables using the debugging methods I mentioned above at different stages in the loop, explaining how they differ from what you expect.

      Hi thanks for the info. the info in the 'dropback.txt' is as follows.
      /Volumes/photorepos/Partners/Christopher_Banks/post/CB_instock/4/01001 +1510005801_0669_main.psd /Volumes/photorepos/Partners/Christopher_Banks/post/CB_instock/4/01001 +1520006618_5030_main.psd /Volumes/photorepos/Partners/Christopher_Banks/post/CB_instock/4/01001 +1520006812_0670_main.psd /Volumes/photorepos/Partners/Christopher_Banks/post/CB_instock/9/01001 +1510005801_0964_main.psd /Volumes/photorepos/Partners/Christopher_Banks/post/CB_instock/9/01001 +1520006618_0964_main.psd /Volumes/photorepos/Partners/Christopher_Banks/post/CB_instock/9/01001 +1520006812_0990_main.psd
      the output of your code is simlar to what i saw:
      010011510005801_0669_main.psd and /Volumes/photorepos/Partners/Christo +pher_Banks/Volumes/photorepos/Partners/Christopher_Banks/post/CB_inst +ock/4/010011520006618/Volumes/photorepos/Partners/Christopher_Banks/p +ost/CB_instock/4/010011520006812/Volumes/photorepos/Partners/Christop +her_Banks/post/CB_instock/9/010011510005801/Volumes/photorepos/Partne +rs/Christopher_Banks/post/CB_instock/9/010011520006618/Volumes/photor +epos/Partners/Christopher_Banks/post/CB_instock/9/010011520006812_099 +0_main.psd 010011510005801_0964_main.psd and /Volumes/photorepos/Partners/Christo +pher_Banks/Volumes/photorepos/Partners/Christopher_Banks/post/CB_inst +ock/4/010011520006618/Volumes/photorepos/Partners/Christopher_Banks/p +ost/CB_instock/4/010011520006812/Volumes/photorepos/Partners/Christop +her_Banks/post/CB_instock/9/010011510005801/Volumes/photorepos/Partne +rs/Christopher_Banks/post/CB_instock/9/010011520006618/Volumes/photor +epos/Partners/Christopher_Banks/post/CB_instock/9/010011520006812_099 +0_main.psd 010011520006618_0964_main.psd and /Volumes/photorepos/Partners/Christo +pher_Banks/Volumes/photorepos/Partners/Christopher_Banks/post/CB_inst +ock/4/010011520006618/Volumes/photorepos/Partners/Christopher_Banks/p +ost/CB_instock/4/010011520006812/Volumes/photorepos/Partners/Christop +her_Banks/post/CB_instock/9/010011510005801/Volumes/photorepos/Partne +rs/Christopher_Banks/post/CB_instock/9/010011520006618/Volumes/photor +epos/Partners/Christopher_Banks/post/CB_instock/9/010011520006812_099 +0_main.psd 010011520006618_5030_main.psd and /Volumes/photorepos/Partners/Christo +pher_Banks/Volumes/photorepos/Partners/Christopher_Banks/post/CB_inst +ock/4/010011520006618/Volumes/photorepos/Partners/Christopher_Banks/p +ost/CB_instock/4/010011520006812/Volumes/photorepos/Partners/Christop +her_Banks/post/CB_instock/9/010011510005801/Volumes/photorepos/Partne +rs/Christopher_Banks/post/CB_instock/9/010011520006618/Volumes/photor +epos/Partners/Christopher_Banks/post/CB_instock/9/010011520006812_099 +0_main.psd 010011520006812_0670_main.psd and /Volumes/photorepos/Partners/Christo +pher_Banks/Volumes/photorepos/Partners/Christopher_Banks/post/CB_inst +ock/4/010011520006618/Volumes/photorepos/Partners/Christopher_Banks/p +ost/CB_instock/4/010011520006812/Volumes/photorepos/Partners/Christop +her_Banks/post/CB_instock/9/010011510005801/Volumes/photorepos/Partne +rs/Christopher_Banks/post/CB_instock/9/010011520006618/Volumes/photor +epos/Partners/Christopher_Banks/post/CB_instock/9/010011520006812_099 +0_main.psd 010011520006812_0990_main.psd and /Volumes/photorepos/Partners/Christo +pher_Banks/Volumes/photorepos/Partners/Christopher_Banks/post/CB_inst +ock/4/010011520006618/Volumes/photorepos/Partners/Christopher_Banks/p +ost/CB_instock/4/010011520006812/Volumes/photorepos/Partners/Christop +her_Banks/post/CB_instock/9/010011510005801/Volumes/photorepos/Partne +rs/Christopher_Banks/post/CB_instock/9/010011520006618/Volumes/photor +epos/Partners/Christopher_Banks/post/CB_instock/9/010011520006812_099 +0_main.psd 010011520006816_3034_main.psd and /Volumes/photorepos/Partners/Christo +pher_Banks/Volumes/photorepos/Partners/Christopher_Banks/post/CB_inst +ock/4/010011520006618/Volumes/photorepos/Partners/Christopher_Banks/p +ost/CB_instock/4/010011520006812/Volumes/photorepos/Partners/Christop +her_Banks/post/CB_instock/9/010011510005801/Volumes/photorepos/Partne +rs/Christopher_Banks/post/CB_instock/9/010011520006618/Volumes/photor +epos/Partners/Christopher_Banks/post/CB_instock/9/010011520006812_099 +0_main.psd 010011520007257_0126_main.psd and /Volumes/photorepos/Partners/Christo +pher_Banks/Volumes/photorepos/Partners/Christopher_Banks/post/CB_inst +ock/4/010011520006618/Volumes/photorepos/Partners/Christopher_Banks/p +ost/CB_instock/4/010011520006812/Volumes/photorepos/Partners/Christop +her_Banks/post/CB_instock/9/010011510005801/Volumes/photorepos/Partne +rs/Christopher_Banks/post/CB_instock/9/010011520006618/Volumes/photor +epos/Partners/Christopher_Banks/post/CB_instock/9/010011520006812_099 +0_main.psd 010011520007259_5031_main.psd and /Volumes/photorepos/Partners/Christo +pher_Banks/Volumes/photorepos/Partners/Christopher_Banks/post/CB_inst +ock/4/010011520006618/Volumes/photorepos/Partners/Christopher_Banks/p +ost/CB_instock/4/010011520006812/Volumes/photorepos/Partners/Christop +her_Banks/post/CB_instock/9/010011510005801/Volumes/photorepos/Partne +rs/Christopher_Banks/post/CB_instock/9/010011520006618/Volumes/photor +epos/Partners/Christopher_Banks/post/CB_instock/9/010011520006812_099 +0_main.psd 010013000007469_4036_alternate1.psd and /Volumes/photorepos/Partners/C +hristopher/Volumes/photorepos/Partners/Christopher_Banks/post/CB_inst +ock/4/010011520006618/Volumes/photorepos/Partners/Christopher_Banks/p +ost/CB_instock/4/010011520006812/Volumes/photorepos/Partners/Christop +her_Banks/post/CB_instock/9/010011510005801/Volumes/photorepos/Partne +rs/Christopher_Banks/post/CB_instock/9/010011520006618/Volumes/photor +epos/Partners/Christopher_Banks/post/CB_instock/9/010011520006812_099 +0_main.psd 010013000007469_4036_main.psd and /Volumes/photorepos/Partners/Christo +pher_Banks/Volumes/photorepos/Partners/Christopher_Banks/post/CB_inst +ock/4/010011520006618/Volumes/photorepos/Partners/Christopher_Banks/p +ost/CB_instock/4/010011520006812/Volumes/photorepos/Partners/Christop +her_Banks/post/CB_instock/9/010011510005801/Volumes/photorepos/Partne +rs/Christopher_Banks/post/CB_instock/9/010011520006618/Volumes/photor +epos/Partners/Christopher_Banks/post/CB_instock/9/010011520006812_099 +0_main.psd 010013000007471_0990_alternate1.psd and /Volumes/photorepos/Partners/C +hristopher/Volumes/photorepos/Partners/Christopher_Banks/post/CB_inst +ock/4/010011520006618/Volumes/photorepos/Partners/Christopher_Banks/p +ost/CB_instock/4/010011520006812/Volumes/photorepos/Partners/Christop +her_Banks/post/CB_instock/9/010011510005801/Volumes/photorepos/Partne +rs/Christopher_Banks/post/CB_instock/9/010011520006618/Volumes/photor +epos/Partners/Christopher_Banks/post/CB_instock/9/010011520006812_099 +0_main.psd 010013000007471_0990_main.psd and /Volumes/photorepos/Partners/Christo +pher_Banks/Volumes/photorepos/Partners/Christopher_Banks/post/CB_inst +ock/4/010011520006618/Volumes/photorepos/Partners/Christopher_Banks/p +ost/CB_instock/4/010011520006812/Volumes/photorepos/Partners/Christop +her_Banks/post/CB_instock/9/010011510005801/Volumes/photorepos/Partne +rs/Christopher_Banks/post/CB_instock/9/010011520006618/Volumes/photor +epos/Partners/Christopher_Banks/post/CB_instock/9/010011520006812_099 +0_main.psd 080080520009970_0155_main.psd and /Volumes/photorepos/Partners/Christo +pher_Banks/Volumes/photorepos/Partners/Christopher_Banks/post/CB_inst +ock/4/010011520006618/Volumes/photorepos/Partners/Christopher_Banks/p +ost/CB_instock/4/010011520006812/Volumes/photorepos/Partners/Christop +her_Banks/post/CB_instock/9/010011510005801/Volumes/photorepos/Partne +rs/Christopher_Banks/post/CB_instock/9/010011520006618/Volumes/photor +epos/Partners/Christopher_Banks/post/CB_instock/9/010011520006812_099 +0_main.psd 080081510009702_0001_alternate1.psd and /Volumes/photorepos/Partners/C +hristopher/Volumes/photorepos/Partners/Christopher_Banks/post/CB_inst +ock/4/010011520006618/Volumes/photorepos/Partners/Christopher_Banks/p +ost/CB_instock/4/010011520006812/Volumes/photorepos/Partners/Christop +her_Banks/post/CB_instock/9/010011510005801/Volumes/photorepos/Partne +rs/Christopher_Banks/post/CB_instock/9/010011520006618/Volumes/photor +epos/Partners/Christopher_Banks/post/CB_instock/9/010011520006812_099 +0_main.psd
      are you saying that i need to use /\Q$row/because of the paths?
        the output of your code is simlar to what i saw:

        That's unlikely, since my code doesn't contain any prints. Not even your original code contains the word and that's showing up in this output. And I really don't see which code would cause the pathname to apparently be repeated six times in what appears to be the same string. Use Data::Dumper or Data::Dump for debug output like I suggested and post the actual code you're running, and maybe we'll have a chance to help. See How do I post a question effectively? and I know what I mean. Why don't you? in addition to Short, Self-Contained, Correct Example.

        are you saying that i need to use /\Q$row/because of the paths?

        Not quite, the \Q escapes metacharacters in $row that would otherwise be interpreted as regex characters. Anyway, that's not the only suggestion I made, what about the others?

Re: glob a folder of images, and open text doc, find the matches and move the images.
by Marshall (Canon) on Feb 10, 2020 at 18:04 UTC
    Your code is completely absent any meaningful indenting.
    } } } }
    Is not good programming style. There are number of acceptable indenting methods, I show one of them below. Every closing "curly brace" should be easy to match up with the opening "curly brace". I did not modify your code, but just added whitespace for clarity. (oops, I think you did miss one curly brace) Believe it or not, whitespace is some of most important "code" that you can write.
    foreach $file (@filesforeach) { print "$file\n"; open(my $fh, "<", "$filenames_to_putback") or warn "$!\n"; while (my $row = <$fh>) { chomp $row; print "$row\n"; my @line = $file; for (@line) { if ($_ =~ /$row/) { chomp $_; # push @output, "found $_ that matched redo $file $row <br +>"; move($_,$row) or warn "shit went wrong yo! $!\n"; } } } } # print "@output\n";
    All of the "warn" messages should be "die". "warn" just prints a message and continues. warn "$!\n" You probably don't know this but adding the explicit "\n" suppresses the Perl line number of the error. Don't add the "\n" unless you understand what it does. There are uses for this when dealing with unsophisticated users, but in general leave Perl "line of error" reporting on.
Re: glob a folder of images, and open text doc, find the matches and move the images.
by Anonymous Monk on Feb 09, 2020 at 21:56 UTC
      Hi there, i know, i posted this as a more complete code, rather then all the extra print, and commented out moving just for sake of cleanliness.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11112693]
Approved by LanX
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (6)
As of 2024-04-25 12:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found