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
karthik_s9Asked:
Who is Participating?
 
Suhas .Connect With a Mentor 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
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

 
mjcoyneConnect With a Mentor Commented:
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
 
TintinConnect With a Mentor Commented:

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
 
alexsergeyevConnect With a Mentor Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.