Avatar of TyBay
TyBay

asked on 

Search for files and retrieve file size recursively

Using below snippet - is there a way to retrieve the size of the file and print in the below line:
   print "$dir/$_\n" if (/\.exe/

Snippet:

$BASEDIR =  "\\\\remote/path/basedirectory/";    #give your required base directory
$temp = $BASEDIR;
#$outfile = "$temp/list";    # give the location of the output file
#open OUT, ">$outfile" or die "Cannot open $outfile";
 
chdir $BASEDIR or die "Cannot cd to $BASEDIR: $!n";
traverse($BASEDIR,(stat('.'))[3]);
 
 
# @FILES now contains a list of file paths.
 
sub traverse {
my ($dir,$nlink) = @_;
my (@filenames,@subdirs);
 
opendir DIR, '.' or die "cannot opendir $dir: $!n";
@filenames = grep { !/^..?$/ && !(-l $_) } readdir DIR;
closedir DIR;
 
for (@filenames) {
next unless stat($_) && -r _;
-d _ && push(@subdirs,$_);
-f _ && -s _ && -T _ && m{..?html?$} && push(@FILES,"$dir/$_");
 if($_ =~/\./)
  {
   print "$dir/$_\n" if (/\.exe/);
   #print OUT "$a"
  }
 
}
 
# has subdirs
if ($nlink != 2) {
for (@subdirs) {
chdir $_ or die "cannot cd to $_: $!n";
traverse("$dir/$_",(stat('.'))[3]);
chdir '..';
}
}
}

Perl

Avatar of undefined
Last Comment
Tintin
Avatar of nedfine
nedfine
Flag of India image

try this.

$size = -s $_ , print "$dir/$_ \t size = $size\n" if (/\.txt/);


Thanks
Ned
ASKER CERTIFIED SOLUTION
Avatar of nedfine
nedfine
Flag of India image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of TyBay
TyBay

ASKER

Many Thanks
Avatar of Adam314
Adam314

print("$dir/$_     ", -s _, "\n") if (/\.exe/);
Avatar of Tintin
Tintin

Ugg.  You really should use the standard File::Find module rather than a hand rolled version.  It makes it more consistent and will work across multiple platforms.
Perl
Perl

Perl is a high-level, general-purpose, interpreted, dynamic programming languages with over 25 years of development. Perl 5 runs on over 100 platforms from portables to mainframes and is suitable for both rapid prototyping and large-scale development projects. Perl gained widespread popularity as a Common Gateway Interface (CGI) scripting language, in part due to its regular expression and string parsing abilities. In addition to CGI, Perl is used for graphics programming, system administration, network programming, finance, bioinformatics, and other applications.

26K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo