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

Hi people,

I have a CGI script that uploads files, performs a bundle of checks upon then and then if they are valid I want to move all files in that directory into a processing directory where a cron job operates on them once a day

(I have uploaded a bundle of files in a tar file and unziped them using Archive::Tar into this directory)

The uploaded files look like..

[277] mf139823@esweb: pwd /apps/webroot/service/servicelist-automation/sl-submission/submitted-f +iles/2002-10-28-11-42-09-ss-mrf2 [278] mf139823@esweb: ls -l total 66 -rwxrwxrwx 1 nobody nobody 17408 Oct 28 11:42 siteresse-sg-eng +.tar -rwxrwxrwx 1 nobody nobody 7361 Oct 22 09:49 siteressse-sg-en +g-20020510.sxw -rwxrwxrwx 1 nobody nobody 7357 Oct 22 09:49 siteressse-sg-en +g-20020918.sxw

...now the code I have to move them looks like this...

sub moveTarFilesToProcessDir{ # $fileFrom = "$curSubmitDir/siteressse-sg-eng-20020918.sxw"; $fileFrom = "$curSubmitDir/*"; $fileTo = "$thisFilesDir/new-files/$user_LOB/"; print $cgi->p("Moving files from <h4>($fileFrom)</h4> to <h4>($fil +eTo)</h4>"); move($fileFrom, $fileTo) or die "($!)"; }

... you'll noitice that one line is commented out. If I use this line instead of the line under it as the $fileFrom variable it will move just that file to the correct place(thats one of the files I'm testing it with)

But I want to move all files in this directory. So why does $fileFrom = "$curSubmitDir/*"; not work? How can I move all of the files in this directory.

At the moment I get the error..

(No such file or directory)

... when I try to use $curSubmitDir/*

Is this a stupid mistake that has kept me puzzled for a the last 3hours or is it not possible with File::Copy?

Thanks everyone,

M

Replies are listed 'Best First'.
Re: Move multiple files?
by nothingmuch (Priest) on Oct 28, 2002 at 19:17 UTC
    Since no shell interpolation is performed on the filename, the filesystem is queried for an actual file called '*'. You may be interested in peerforming a chdir($curSubmitDir); @filesFrom = glob("*");, which will use the shell to get the list of files, and store them in an array. Then you have to copy the files one by one:
    foreach my $fileFrom (@filesFrom){ move ($fileFrom, $fileTo) or die "$!"; }


    You may also use a different approach - which is a bit more efficient, and secure:
    opendir SUBM, $curSubmitDir; while (defined (my $fileFrom = readdir DIR)){ move ($fileFrom, $fileTo) or die "$!"; }
    Which will open the directory, and read it item by item, instead of performing a glob. This will also avoid an array in memory, and will not change your working directory.

    -nuffin
    zz zZ Z Z #!perl
      Another benefit of the second approach is that it will also copy files with leading dots, which globbing will not do.

      kelan


      Perl6 Grammar Student

Re: Move multiple files?
by Kanji (Parson) on Oct 28, 2002 at 19:17 UTC

    It doesn't look like File::Copy supports globing, so you'll have to handle that part of it yourself and iterate over the results...

    foreach my $file ( glob($fileFrom) ) { move($file, $fileTo); )

        --k.


Re: Move multiple files?
by joe++ (Friar) on Oct 28, 2002 at 19:14 UTC
    Try perldoc -f glob, or, better yet, perldoc File::Glob if you're using Perl 5.6.x or newer.

    --
    Cheers, Joe