Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

finding odd characters in a string that I am creating in the script

by donkeykong (Novice)
on Jul 28, 2009 at 02:26 UTC ( [id://783715] : perlquestion . print w/replies, xml ) Need Help??

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

Hello PerlMonks, I'm using File::Util to create directory trees in the event they don't exist and copy a file into the child directories. I create the $directory variable dynamically, but the module keeps telling me I'm passing illegal characters, and I'm just not seeing what I'm doing wrong. Here's the code:
BEGIN { use strict; use File::Next; use File::Util; use File::Copy; } my ($file_util) = File::Util->new(); move_files() sub move_files { open (FILE, "<ad_archive.rtf") or die "$!\n"; while (<FILE>) { next if ($_ !~ /\w/); my ($full_path) = ($_ =~ /\d*\s(.*)/); my ($par_dir, $file_name) = ($full_path =~ /.*\\(.*)\\(.*)/); my ($file_no_ext) = ($file_name =~ /(.*)\.pdf/); my ($first_five) = ($file_no_ext =~ /(\d{5}).*/); $first_five .= 'xxx'; if ($par_dir =~ $file_no_ext) { my $directory = "\\\\filervoca\\e\$\$\\activeads\\adwatchf +olders\\$first_five\\$file_no_ext"; #print "path: $full_path\npar_dir: $par_dir\nfile_name: $f +ile_name\nfirst_five: $first_five\ndirectory: $directory\n"; #print "dir doesn't exist, need to make it\n"; $file_util->make_dir($directory, '--if-not-exists') or die + "$!"; copy($full_path, "$directory\\$file_no_ext"."00.pdf") or d +ie (print "$!"); } sleep 10; } close FILE; }
Here's the error I get:
<b> PROCESS TERMINATED DUE TO ERRORS File::Util can't use this string for (weird character)the name of a di +rectory1/2. (same weird character)\\filervoca\e$$\activeads\adwatchf +olders\00232xxx\00232351(1/2) It contains illegal characters. Illegal characters are: \ (backslash) / (forward slash) : (colon) | (pipe) * (asterisk) ? (question mark) " (double quote) < (less than) > (greater than) \t (tab) \ck (vertical tabulator) \r (newline CR) \n (newline LF) Origin: This is a human error. Solution: A human must remove the illegal characters from this string. ARG _pak = File::Util ARG purpose = the name of a directory ARG string = \\filervoca\e$$\activeads\adwatchfolders\00232xxx\0023235 +1 1. File::Util::_throw -called at line(1322) of blib\lib\File\Util.pm --was called with args --was not called to evaluate anything 2. File::Util::make_dir - called at line (45) of findpdfs.pl --was called with args --was not called to evaluate anything 3. main::move_files -called at line (11) of findpdfs.pl --was called with args --was not called to evaluate anything </b>
Thank you in advance for your help.

Replies are listed 'Best First'.
Re: finding odd characters in a string that I am creating in the script
by graff (Chancellor) on Jul 28, 2009 at 05:47 UTC
    Why are you reading a list of file names from an "rtf" file? I would assume that this text-processing-based file format is not really suitable as input to the kind of procedure you're trying to set up here, because the file is likely to contain a strange assortment of formatting directives in addition to the data you actually need.

    It would be much better if you can supply an unadorned plain-text "flat list" (e.g. as produced by the unix/linux "ls" or "find" utilities), or better yet, use opendir and readdir (or File::Find) to pull file names directly from the source folder(s) where your pdf files are stored.

    As it is, the OP code is being pretty promiscuous about the accepting the contents of "ad_archive.rtf", and assuming that all those ".*" patterns in the regexes will yield sensible matches. I would expect a lot of failures.

      I was helping the OP in CB and we just lost contact for some reason. The first issue was: what do you want to do? And that appeared to be move all .pdf files in one directory to another directory. One issue was "moving" files between file system mount points (which is a copy and delete). We never got to this .rtf file point. But that would have been the next question: how do you get this list of files to move and to where?

      So I second your question.

Re: finding odd characters in a string that I am creating in the script
by Marshall (Canon) on Jul 28, 2009 at 04:36 UTC
    As per CB, I think this is close to what you want.
    #!/usr/bin/perl -w use strict; use File::Copy; my $source_dir = "C:/temp"; my $dest_dir = "C:/temp2"; move_pdf($source_dir, $dest_dir); sub move_pdf { my ($src_path, $dest_path) = @_; opendir (SOURCE_DIR, $src_path) or die "can't open sourcedir"; if (!-e $dest_path) { mkdir $dest_path or die "can't create $dest_path"; } my @pdf2copy = grep{/\.pdf$/} readdir SOURCE_DIR; foreach my $file (@pdf2copy) { copy ( "$src_path/$file", "$dest_path/$file") || die "copy failed $src_path/$file, $dest_path/$file"; } }
    Update: I think that "move" instead of "copy" above is right for you.
Re: finding odd characters in a string that I am creating in the script
by Anonymous Monk on Jul 28, 2009 at 04:07 UTC
    and I'm just not seeing what I'm doing wrong.

    Really? You have Illegal characters , and error messages gives you a list

    \ (backslash) / (forward slash) : (colon) | (pipe) * (asterisk) ? (question mark) " (double quote) < (less than) > (greater than) \t (tab) \ck (vertical tabulator) \r (newline CR) \n (newline LF)
    Origin: This is a human error. Solution: A human must remove the illegal characters from this string.
      but those are all characters I need to get to the directory...I am not adding anything I don't need.
Re: finding odd characters in a string that I am creating in the script
by ikegami (Patriarch) on Jul 28, 2009 at 14:08 UTC

    By the way,
    \\filervoca\e$$\activeads\adwatchfolders\00232xxx\00232351
    should be
    \\filervoca\e$\activeads\adwatchfolders\00232xxx\00232351

    But I think the module imposes rules that aren't reflected in the OS.