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

Is there an equivelent to File::Find that finds directories as well as files? I have a task where I need to copy a directory sub-tree and modify some of the copied files.

At present I'm working with Filesys::Tree and walking the tree to get the job done. A File::Find type callback would clean up the code a lot.

Note: the complication is that some of the leaf dirs may be empty, but they still need to be created.


Perl is Huffman encoded by design.

Replies are listed 'Best First'.
Re: Is there a Dir/File::Find
by merlyn (Sage) on Jul 29, 2005 at 03:18 UTC
    Uh, File::Find calls wanted for everything: files, directories, and anything else in there. You have to work at it to ignore those things, rather than include them!

    Can you show an example of some code that you thought wasn't finding directories?

    -- Randal L. Schwartz, Perl hacker
    Be sure to read my standard disclaimer if this is a reply.

      Hmmm, yesish. I get the following odd results:

      use File::Find; find (\&ProcessDirTree, './_NewExtensionTemplate'); sub ProcessDirTree { print "Dir: $File::Find::name\n" if -d $File::Find::name; print "File: $File::Find::name\n" unless -d $File::Find::name; } File: ./_NewExtensionTemplate File: ./_NewExtensionTemplate/0_0 File: ./_NewExtensionTemplate/0_0/Inputs File: ./_NewExtensionTemplate/0_0/Inputs/ReadMe''.rtf File: ./_NewExtensionTemplate/0_0/Inputs/Build File: ./_NewExtensionTemplate/0_0/Inputs/Build/build.bld File: ./_NewExtensionTemplate/0_0/Inputs/Build/Build.macros File: ./_NewExtensionTemplate/0_0/Inputs/Build/NotATag.txt

      where I would expect:

      Dir: ./_NewExtensionTemplate Dir: ./_NewExtensionTemplate/0_0 Dir: ./_NewExtensionTemplate/0_0/Inputs File: ./_NewExtensionTemplate/0_0/Inputs/ReadMe''.rtf Dir: ./_NewExtensionTemplate/0_0/Inputs/Build File: ./_NewExtensionTemplate/0_0/Inputs/Build/build.bld File: ./_NewExtensionTemplate/0_0/Inputs/Build/Build.macros File: ./_NewExtensionTemplate/0_0/Inputs/Build/NotATag.txt

      Perl is Huffman encoded by design.
        Within "wanted", you want to use "$_", not "$File::Find::name", because you are chdir'ed down into the directory. Just use "-d" for example, because it defaults to $_.

        So, your code would look like:

        use File::Find; find (\&ProcessDirTree, './_NewExtensionTemplate'); sub ProcessDirTree { print "Dir: $File::Find::name\n" if -d; print "File: $File::Find::name\n" unless -d; }

        -- Randal L. Schwartz, Perl hacker
        Be sure to read my standard disclaimer if this is a reply.