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

This may be a very basic question.... but then again I am very much a beginner coding in Perl, so I will ask it anyway:
I am trying to copy files from one directory to another but there are likely to be folders within this directory and I don't want them. Is there a simple way to copy only files from a given directory to another? I figured that I could check for any kind of extention (.whatever) but I was wondering if there is some sort of built in function that would copy just files and leave folders alone???

Any thoughts to help a newbie? Thanks.
Kalimeister

Replies are listed 'Best First'.
Re: Distinguishing files from folders
by MZSanford (Curate) on Jan 15, 2002 at 21:27 UTC
    Use -d ... like :
    # get $filename if (-d $filename) { # a dir } elsif (-f $filename) { # a normal file } else { # something else }

    from the frivolous to the serious
      Most excellent. Thank you very much. I must say that this is a great site in my quest to learn Perl. Everyone is very helpful and quick to offer advice. Thank you.
      Kalimeister
      Also note that on all platforms I'm aware of, directories can have extensions. And files are certainly not required to have extensions.
Re: Distinguishing files from folders
by impossiblerobot (Deacon) on Jan 15, 2002 at 21:52 UTC
    I would also suggest (in case you are tempted to use a system command) that you use File::Copy for the copy operation. It's part of the standard Perl distribution.

    Update: I noticed that I forgot to mention why you should use File::Copy. Besides being portable across multiple operating systems, it is also easier to use than the usual alternatives (such as using `cp` or creating your own routine to read a file and write it in another location).

    Impossible Robot
Re: Distinguishing files from folders
by archen (Pilgrim) on Jan 16, 2002 at 19:19 UTC
    You might also be interested in the slew of such operators that can make your life a lot easier.-X .