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

I am writing a script that needs to recurse through subdirectories on a WinNT server and produce a text document with names of files that are older than 30 days. Our server is being used by true Windows users. So there are spaces in the Directory names. When my script hits a directory with a space it dies. Does anyone know a side step to spaces in directories on a Win32 system? The File::Find works fine. It is when it tests the file. When it tests a file in a path like "m:\my department\foo.txt" it dies. I know with Linux you can add a \ before the space and it will work just fine.
use strict; use File::Find; open (FOO, ">>filelist.txt"); find(\&TEST_FOR_AGE, '.'); sub TEST_FOR_AGE { print "$File::Find::name\n"; if (!( -e "$File::Find::name")){ print "Help $File::Find::name doesn't exist!!"; exit; } if ((-M "$File::Find::name" >= 30) && (-f "$File::Find::name")) { print FOO "$File::Find::name\n"; } print "\n"; } close FOO;
Thanks

--BigJoe

Learn patience, you must.
Young PerlMonk, craves Not these things.
Use the source Luke.

Replies are listed 'Best First'.
Re: Win32 & File::Find
by Hofmator (Curate) on Aug 16, 2001 at 20:00 UTC

    File::Find automatically chdirs into the different directories, so your routine simplifies to

    sub TEST_FOR_AGE { print "$File::Find::name\n"; if (-f && (-M _ >= 30)) { print FOO "$File::Find::name\n"; } }
    It's also helpful that the filetest operators default to $_ and that the special filehandle _ can be used to save a system call. This works fine also for directory names with spaces in them.

    -- Hofmator

Re: Win32 & File::Find
by arturo (Vicar) on Aug 16, 2001 at 19:57 UTC

    There's a little oddness in the condition you're checking for in the code you posted: why worry about whether a file whose name was found exists? If it didn't exist, it wouldn't be found. Note also that files that don't exist won't pass the other two tests.

    Of course, that doesn't have much to do with the problem. Note that you can use $_ within the TEST_FOR_AGE sub. And you don't need to put double-quotes around the variable names. Try removing those quotes and see what happens. Spaces should only be a problem for programs that use the shell (unless the implementation of File::Find on Windows uses a shell of some sort ... )

    HTH.

    perl -e 'print "How sweet does a rose smell? "; chomp ($n = <STDIN>); +$rose = "smells sweet to degree $n"; *other_name = *rose; print "$oth +er_name\n"'