[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1198
  • Last Modified:

How to check the folder size through Perl script

Hello All,

 I am writing a script to copy the folder from the remote server to my PC.Basically newer folders will be created in the remote server,my PC will check for the new folders in the remote server & copy it to my PC .I want to check the folder size before coping the folder.due to network issues ,if the files are not copied fully it has to start copy once again & check the folder size.

can some one give me the script which will do the above process ?

Thanks
0
karthik_s9
Asked:
karthik_s9
4 Solutions
 
Suhas .QA ManagerCommented:
on windows machine
try this:

@arr = `dir /S`;
($a,$b,$c,$d,$e) = split('\s+',$arr[-2]);
print "$d $e";

it prints the size of current directory.

Suhas
0
 
mjcoyneCommented:
#!/usr/bin/perl -w
use strict;
use File::Copy;

my ($ssize, $tsize);
my $s_dir = "/your/source/directory/";
my $t_dir = "/your/target/directory/";

unless (-e $t_dir) {
    mkdir $t_dir;
}

opendir (SOURCE, "$s_dir") or die "$s_dir open failed: $!";
while (defined (my $file = readdir(SOURCE))) {
    $ssize += -s "$s_dir/$file";
    copy ("$s_dir/$file", "$t_dir/$file") or die "Unable to copy $s_dir/$file: $!";
}

opendir (TARGET, "$t_dir") or die "$t_dir open failed: $!";
while (defined (my $file = readdir(TARGET))) {
    $tsize += -s "$t_dir/$file";
}

if ($ssize == $tsize) {
    print "Sucessfully copied $ssize bytes from $s_dir to $t_dir\n";
} else {
    print "Copying $s_dir to $t_dir failed\n";
}
0
 
ps15Commented:
I think mjcoyne solution won't work with subdirectorys ...
if you don't need it to be plain perl you can do something like suhabharadwaj did on unix/linux/OS X too:

sub get_dir_size {
 my $dir = shift;
 if ($^O =~ /win/) {
  #suhasbharadwaj 's way
 return join "",(split('\s+',(`dir /S`)[-2]))[3,4];
 }
 else
 {
 #asume that you've got du
 `du -sh .` =~ /^(\S+)/;
  return $1;
 }
}
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
mjcoyneCommented:
ps15 is right -- I didn't consider that the source directory might contain subdirectories as well as files.

In the past, I have done similar things using File::Copy::Recursive (see http://search.cpan.org/~dmuey/File-Copy-Recursive-0.22/Recursive.pm) in place of File::Copy, and transversing the directory for size with something like:

@files = grep (!/^\.\.?$/ , readdir (SOURCE));

foreach (@files) {
 $file = ("$s_dir$_");
 if (-d $file) {
    &sub_dir("$file");  # jump to sub to read sub directory
} else {
   $ssize += -s ("$file");
}
0
 
TintinCommented:

This sounds like a perfect job for 'rsync'.  See http://rsync.samba.org/
0
 
mjcoyneCommented:
It does, doesn't it?  I'm not clear what operating system(s) we're dealing with here.  If Windows is involved, there's some more info on rsync at http://www.gaztronics.net/rsync.php.  If you're just trying to back up a Windows directory to a Linux server (or a Linux directory to a Windows machine), you should also check out SyncBack (see http://www.2brightsparks.com/syncback/syncback-hub.html).  There's a freeware version available...
0
 
mjcoyneCommented:
Oh, and I guess I should include that SyncBack does Windows to Windows transfers as well, of course...

Can we have a few more specifics on what you're trying to do and what computers you're trying to move these files between, karthik_s9?
0
 
alexsergeyevCommented:
it's definetely job for rsync and and it has perl interface as well - File::Rsync
0
 
alexsergeyevCommented:
(imho) Tintin and mjcoyne should get equal points unless author would object.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now