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

Dear expert, I have tried to use the following code under windows OS in order to get the all files in a Directory Recursively but the operating system could not state path while there a directoriews with whitespaces such as 'D:\My private Docs'
use File::Find; sub process_file { print "$File::Find::name\n" if -f} find(\&process_file, $DIR);
Any idea ? Thanks in advance Azaria

20070921 Janitored by Corion: Added code tags, as per Writeup Formatting Tips

Replies are listed 'Best First'.
Re: Processing All Files in a Directory Recursively under Windows OS
by Corion (Patriarch) on Sep 17, 2007 at 09:15 UTC

    Works for me:

    X:\>perl -MFile::Find -le "sub process_file{print $File::Find::name if + -f };find (\&process_file,'test with whitespace')" test with whitespace/a file.txt
    Q:\>dir "test with whitespace" Datenträger in Laufwerk Q: ist homes_xxxxxxxx$ Volumeseriennummer: DEAD-BEEF Verzeichnis von Q:\test with whitespace 17.09.2007 11:12 <DIR> . 17.09.2007 10:10 <DIR> .. 17.09.2007 11:12 0 a file.txt

    Maybe you can show us some code, the input and the output.

      First thanks for your reply, if i run from the command line
      X:\>perl -MFile::Find -le "sub process_file{print $File::Find::name if + -f };find (\&process_file,'test with whitespace')"
      it works perfectly, the problems came when the I am using it in a file with variables, means :
      use File::Find; print "Enter your root directory\n"; $root = <STDIN> ; sub process_file{ print $File::Find::name if -f ; } find (\&process_file,$root);
      %> a.pl Enter your root directory: D:\DBS %> Can't state D:\DBS

      Any idea what is the reason for this failure ?

      Thanks in advance
      Azaria

      20070918 Janitored by Corion: Added formatting, code tags, as per Writeup Formatting Tips

        Please use proper HTML formatting and preview your node before you post it.

        Your variable $root is not what you think it is. Print it out like this:

        my $root = "Hello world"; print "Root is >>$root<<\n";

        In your case, when you read $root from STDIN it has a newline appended to it. You should remove that by using the chomp function:

        chomp $root;