Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

Hi,

just to let you know too, it was the carriage return, connected to the IDs within the "bad" text file. After having removed all of them, parsing and matching was no prob anymore.

Thanks again.

How do I feel now?

better ;-)

And here is the code   -Sorry, but I couldn't use strict .-(

#! /usr/local/bin/perl # #Script searches and copies large quantities of files # #Script reads a list of IDs given in a text file, #searches for filenames in a specified directory, # and copies all the files, which have the ID as a part of their name # into a new directory given that file exists in source dir #and does not exist in target dir! # #Results are reported into text file. # #Script doesn't work, if text file is created by parsing #a csv file and writing the IDs into the text file #Solution: Remove carriage return: $/="\r\n"; then: chomp # #tested: --ok! ############################################################### #use strict; use warnings; use File::Copy; #Input Source directory --ok! my $dir = $ARGV[0]; if ( !$ARGV[0] ) { print "Error: Missing input directory!\n"; } if ( !-d $ARGV[0] ) { print "Error: Input is no directory\n"; } #Open filehandle and parse list from a text file --ok! my $ref = "./data/indexIDs.txt"; #Important: Text file must not conta +in carriage returns!!! open(FH, '<', $ref) or die "Cannot open file: \n$!"; my @ids = <FH>; chomp @ids; #print "$_\n" for @ids; # tested --ok! close (FH); #test target directory --ok! my $dirTarget = "/cygdrive/d/bildimport/"; if( !-d $dirTarget) { print "Can not open $dirTarget: $!\n"; } #Scan a directory and write filenames into an array --ok! opendir (SOURCE, $dir)or die "Cannot open dir: $!\n"; foreach my $file (readdir SOURCE) { push (@dirFile, $file); chomp @dirFile; } closedir SOURCE; #Report results of copying process into a text file # 3 possible options: #file copied; #file not copied: file exists; #file not copied: file not found open OUT, '>./data/results.txt' or die "Cannot open file: $!\n"; ###################################################################### +### ###MAIN #Use element of @ds as regex and search for all matches in @dirFile - +-ok! #Copy file, if filename matches foreach my $line (@ids) { if (grep /$line/, @dirFile) { my @result = grep (/$line/, @dirFile); foreach (@result) { #print OUT "$_\n"; #tested: --ok! #Build path to files in source dir and to tar +get directory my $res = $dir.$_, "\n"; my $tar = $dirTarget.$_, "\n"; #Start copy! if (!-e $tar) { copy ($res, $tar) or die "Cannot copy: + $!"; print OUT "$_ ;;;copied!\n"; } else { print OUT "$_ ;;;not copied: file exis +ts!\n"; } } } else { print OUT "$line ;;;not copied: file not found!\n"; } } close OUT;

In reply to Re^6: Looking up elements of an array in another array! by better
in thread Looking up elements of an array in another array! by better

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (4)
As of 2024-03-29 08:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found