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

hi i am not able to copy non empty files only with my code..plz help me out..
my @lines = grep { -s && "$source/*.log" } readdir ($DIR); foreach my $file (@lines){ $newfile =~ s/\.old$/.new/; open READFILE, "$..../$file"; open WRITEFILE, ">$d../$newfile";
20071110 Edited by Co-Rion: Restored node

Replies are listed 'Best First'.
Re: transfering a particular kind of file
by graff (Chancellor) on Nov 05, 2007 at 07:05 UTC
    This line in your code seems wrong:
    my @lines = grep { -s && "$source/*.log" } readdir ($DIR);
    because readdir() returns file names from the given directory handle, and those will never have slashes in them. And because it's just the file name without the directory path, the -s won't work on $_ by itself there.

    You may need to show us the line in your script that does the "opendir()" -- you do have such a line, don't you? (readdir won't work unless you do opendir first)

    In any case, you probably want something like this:

    my @lines = grep { /\.log$/ and -s "$source/$_" } readdir $DIR;
    That's assuming that $source is the name of the directory that was passed to the opendir() call (and that the directory handle was the lexically-scoped variable $DIR rather than a bareword "DIR").
Re: transfering a particular kind of file
by erroneousBollock (Curate) on Nov 05, 2007 at 06:43 UTC
    i am not able to copy non empty files only
    You want to selectively copy only files which aren't empty?

    my @lines = grep { -s && "$source/*.log" } readdir ($DIR); foreach my $file (@lines){ $newfile =~ s/\.old$/.new/; open READFILE, "$..../$file"; open WRITEFILE, ">$d../$newfile";
    You're not actually reading from READFILE, nor writing to WRITEFILE. You've merely opened them for reading and writing.

    What do you mean for && "$source/*.log" to achieve? Perhaps use /pattern/ instead of "pattern".

    Use File::Copy to copy it somewhere after getting the list of files.

    Update: ah, you've really just stuffed the condition.

    -David

      i had to copy each and every file interchange the text and copy to the destionation dir with the extension name of each file changed. 20071110 Edited by Co-Rion: Restored node from backup
    A reply falls below the community's threshold of quality. You may see it by logging in.