Re: selcting a specific file
by toolic (Bishop) on Oct 22, 2007 at 20:45 UTC
|
use warnings;
use strict;
my @files = <*.txt>;
for (@files) {
if (-z $_) {
print "File $_ is empty\n";
}
else {
print "File $_ is not empty\n";
}
}
I have 3 files in my current directory. Only the file named "a.txt" is empty. When I run the program above, I get this output:
File a.txt is empty
File b.txt is not empty
File c.txt is not empty
| [reply] [d/l] [select] |
Re: selcting a specific file
by andyford (Curate) on Oct 22, 2007 at 20:50 UTC
|
For us to help you most effectively, you should post your actual code. We don't know what "it doesn't work out" means. Perhaps your file is not zero bytes but contains some whitespace or something?
| [reply] |
|
@files = grep { /\.log$/ } readdir ($DIR);
foreach my $x (@files){
open READFILE, "......";
open WRITEFILE, ">......";
while(<READFILE>){
chomp;
my @parts = split(/\s/, $_);
print WRITEFILE $parts[1], " ", $parts[0], "\n";
}
close WRITEFILE;
close READFILE;
this is the original code | [reply] [d/l] |
|
the file doesn't have any content
| [reply] |
|
[forda@hqnagtl1 ~]$ touch emptytest1
[forda@hqnagtl1 ~]$ touch emptytest2
[forda@hqnagtl1 ~]$ ls -l emptytest*
-rw-rw-r-- 1 forda forda 0 Oct 22 17:19 emptytest1
-rw-rw-r-- 1 forda forda 0 Oct 22 17:19 emptytest2
[forda@hqnagtl1 ~]$ echo ' ' >> emptytest2
[forda@hqnagtl1 ~]$ ls -l emptytest*
-rw-rw-r-- 1 forda forda 0 Oct 22 17:19 emptytest1
-rw-rw-r-- 1 forda forda 15 Oct 22 17:20 emptytest2
I put some spaces into a formerly empty file and now it has a size of 15.
| [reply] [d/l] |
|
|
Re: selcting a specific file
by swampyankee (Parson) on Oct 23, 2007 at 02:58 UTC
|
@files = grep { -s and -f} @files;
I would make sure that it's actually a file; directories on *ix-ish systems have non-zero size. It would probably also make sense to check to see if you have the required permissions for copying the file into another location, e.g. on *ix, you need write access in the target directory.
Copying files can be done with File::Copy or system,
...kudos to blazar for his observation about _ and the -X operators.
emc
Information about American English usage here and here.
Any Northeastern US area jobs? I'm currently unemployed.
| [reply] [d/l] |
|
@files = grep { -s and -f _} @files;
Of course it would be nice to also have chained -X's, and IIRC 5.10 has them.
| [reply] [d/l] [select] |
Re: selcting a specific file
by mwah (Hermit) on Oct 22, 2007 at 21:16 UTC
|
how can i select each non empty files?? and transfer them out i follwed by !-z option but it doesnt work out
Your question is hard to follow but I'll try on it.
From guessing on your riddle, I'd think you should use the File:Copy module like here:
use strict;
use warnings;
use File::Copy;
my @files = qw(Ardbeg.txt Bowmore.txt Bruichladdich.txt Bunnahabhain.t
+xt
Caol_Ila.txt Kilchoman.txt Lagavulin.txt Laphroaig.txt
Port_Charlotte.txt);
my $given_path = '/tmp/allfiles';
my $some_otherpath = '/tmp/nonemptyfiles';
for my $f (@files) {
if( -f "$given_path/$f" && ! -z "$given_path/$f" ) {
copy( "$given_path/$f", "$some_otherpath/$f" )
}
}
BTW: you should watch out for the path separators on your system. On Mac, its the : (column) and not the / (slash) as in Unix/Linux and probably Win32 (maybe you need
even the \\ (backslash) for the latter, but I can't really believe this ;-).
Regards mwa | [reply] [d/l] |
Re: selcting a specific file
by moritz (Cardinal) on Oct 22, 2007 at 20:46 UTC
|
my @nonempty_files = grep { ! -z $_ } @files;
| [reply] [d/l] |
|
...
my @nonempty_files = grep -e && !-z, @files;
...
(especially for non-accessible and non-existent files.)
Regards
mwa | [reply] [d/l] [select] |
|
@files = grep { /\.c$/ } readdir ($DIR);
opening a dir a selcting .c files !!
how to use this !-z!!! | [reply] [d/l] |
|
this is the original code
@files = grep { /\.log$/ } readdir ($DIR);
foreach my $x (@files){
open READFILE, "......";
open WRITEFILE, ">......";
while(<READFILE>){
chomp;
my @parts = split(/\s/, $_);
print WRITEFILE $parts[1], " ", $parts[0], "\n";
}
close WRITEFILE;
close READFILE;
| [reply] [d/l] |
|
use strict;
use warnings;
my $path = '/tmp/allfiles';
my $outpath = '/tmp/nonemptyfiles';
opendir my $dh, $path or die $!;
my @files = grep /\.log$/ && -f "$path/$_" && !-z "$path/$_", readdir
+$dh;
closedir $dh;
for my $fn (@files){
open my $fr, '<', "$path/$fn" or die "IN: $fn $!";
open my $fw, '>', "$outpath/$fn" or die "OUT: $fn $!";;
while( <$fr> ) {
printf $fw "$1 $2\n" if /(\S+)\s+(\S+)/
}
}
Try to study the example and bear some idomatic expressions in mind like indirect file handles and checks for success ...
Regards mwa | [reply] [d/l] |
|
A reply falls below the community's threshold of quality. You may see it by logging in.
|