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

Hi when I run this code

perl filefind.pl testDir

i get the error message

couldn't open the file testDir .... ! No such file or directory at filefind.pl line 44.
couldn't open the file testDir/.DS_Store .... ! No such file or directory at filefind.pl line 44.
couldn't open the file testDir/nitcnitc Folder .... ! No such file or directory at filefind.pl line 44.

I think the problem is due to the find module but Im not sure how to use it correctly in this case. What am I doing wrong please.

#!/usr/local/bin/perl -w # force taint checks, and print warnings #use strict; # install all three strictures # program to open files in a directory and change directory paths fro +m oldpath to $newpath # use stat function to mke file handles available $|++; # force auto flush of output buffer use File::Find; #define full path to folder my $path = " /users/hill/desktop/test_src/"; my $newpath = "/library/webserver/documents/siteroot/installfolder/"; #define string my $newString = 'newDir/newFile'; my $oldString = 'oldDir/oldFile'; # define arrays my @ListofFiles; #define string directory as <STDIN> my $Start_dir = shift or die "usage: $0 <start_dir>\n"; unless (-d $Start_dir){ die "Start directory .'$Start_dir' . is not a directory.\n"; } find(\&process, $Start_dir); sub process{ my $content = ''; # add filename to ListofFiles push(@ListofFiles, $File::Find::name); my $fileopened = $File::Find::name; unless (open FILEOPENED, $fileopened ) { warn "couldn't open the file $fileopened .... ! $!"; return; } my $data =join '', <FILEOPENED>; close(FILEOPENED); return unless $data; print "file is :: $File::Find::name\n"; print "found the file $fileopened \n\n reading the following data \n\ +n $data\n\n\n"; }
  • Comment on Why doesnt the string returned by $File::Find::name find point to files
  • Download Code

Replies are listed 'Best First'.
Re: Why doesnt the string returned by $File::Find::name find point to files
by Thelonius (Priest) on Apr 26, 2006 at 23:40 UTC
    Presumable those a directories (folders). If you want to skip directories, you can start your subroutine like this:
    sub process { return if -d $_; # ... then the rest of your function
Re: Why doesnt the string returned by $File::Find::name find point to files
by jacques (Priest) on Apr 26, 2006 at 23:40 UTC
    Read the error. Try to understand what it is trying to tell you. The error mentions line 44...

    One thing that I think you might be confused about is that $File::Find::name returns the names of directories as well as files.

Re: Why doesnt the string returned by $File::Find::name find point to files
by GrandFather (Saint) on Apr 26, 2006 at 23:53 UTC

    Try removing all the cruft from your test code and report failures in a fashion that shows issues with white space in file names clearly. I notice that your code has fairly arbitary use of white space, including a space at the start of a (unused) path variable.

    I also notice that the result you report can not have come from the code that you provided because the use of white space is different.

    Try the following code and see if you can reproduce the error:

    #!/usr/local/bin/perl -w # force taint checks, and print warnings use strict; use File::Find; my $Start_dir = shift; die ">$Start_dir< is not a directory\n" if ! -d $Start_dir; find(\&process, $Start_dir); sub process { return if -d $File::Find::name; return if -f $File::Find::name; # This is success - the file exist +s print ">$File::Find::name< is not a file or was not found\n"; <p>}


    DWIM is Perl's answer to Gödel
      I used this code. and got the error Substitution replacement not terminated at filefind_pm1.pl line 16. I changed the code to
      #!/usr/local/bin/perl -w # force taint checks, and print warnings use strict; use File::Find; my $Start_dir = shift; die ">$Start_dir< is not a directory\n" if ! -d $Start_dir; find(\&process, $Start_dir); sub process { return if -d $File::Find::name; return if -f $File::Find::name; # This is success - the file exist print ">$File::Find::name< is not a file or was not found\n"; }

      this gave the error for every file in the directory.

      >testdir/nitcnitc Folder/nitcnitc/0wireless.html< is not a file or was not found

      It looks like the -f and the -d flags are not working.

      I'm using osx I maybe i should be using mac:perl. I thought this was just for people using classic but it looks like im wrong.

      Acually running returns the expected results

      #!/usr/local/bin/perl -w # force taint checks, and print warnings use strict; use File::Find; my $Start_dir = shift; die ">$Start_dir< is not a directory\n" if ! -d $Start_dir; find(\&process, $Start_dir); sub process { return if -e $Start_dir # return if -f $File::Find::name; # This is success - the file ex +ist # return if -e $File::Find::name; print ">$File::Find::name < is not a file or was not found\n"; }

      It looks like $File::Find::name cannot be used as a value for a file test.

      If i save the strings returned from £file::find::name, to an array then work through the array then filetests seem to work. Why is this

      #!/usr/local/bin/perl -w # force taint checks, and print warnings use strict; use File::Find; my $Start_dir = shift; my @found =(); die ">$Start_dir< is not a directory\n" if ! -d $Start_dir; find(\&process, $Start_dir); sub process { push(@found, $File::Find::name) } foreach (@found){ if (-d $_ ) { print "This is success - the directory $_ exist\n"}; if ( -f $_) { print "This is success - the file $_ exist\n"}; }
Re: Why doesnt the string returned by $File::Find::name find point to files
by ww (Archbishop) on Apr 27, 2006 at 15:43 UTC
    Your comments appear to reflect some misconceptions. For example, you say:
    001: #!/usr/local/bin/perl -w # force taint checks, and print warnings

    -w will indeed cause your script to spit out warnings... and it will do NOTHING to check taintedness, a description of data obtained from anything other than a known trustworthy source.