Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

transfering a particular kind of file

by asdfghjkl (Initiate)
on Nov 05, 2007 at 06:28 UTC ( [id://648956]=perlquestion: print w/replies, xml ) Need Help??

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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://648956]
Approved by andreas1234567
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (2)
As of 2024-04-26 00:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found