Skip to content

Instantly share code, notes, and snippets.

@flogvit
Created December 3, 2016 15:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save flogvit/626f61078f1fce4e6b64eae675214871 to your computer and use it in GitHub Desktop.
Save flogvit/626f61078f1fce4e6b64eae675214871 to your computer and use it in GitHub Desktop.
Slow copy files to let Google Drive keep up
#!/usr/bin/perl
#
# Created by Vegard Hanssen <Vegard.Hanssen@menneske.no>
# 2016-12-03
#
# The script will move the files recursive from one directory to another
# For each $sleepcount it will sleep for $sleepmin min so google drive
# can finish the syncjob
use File::Copy::Recursive qw(fmove);
use strict;
my $fromdir = $ARGV[0];
my $todir = $ARGV[1];
my $timer = $ARGV[2];
my $sleepcount = $ARGV[3]; # Number to copy before sleeping for 1 min
my $sleepmin = $ARGV[4];
my $count = 0;
if ($timer eq "") {
$timer = 1;
}
if ($sleepcount eq "") {
$sleepcount = 100;
}
if ($sleepmin eq "") {
$sleepmin = 1;
}
if (!-d $fromdir) {
print "Fromdir $fromdir not a directory\n";
exit;
} else {
print "Using fromdir: $fromdir\n";
}
if (!-d $todir) {
print "Todir $todir not a directory\n";
exit;
} else {
print "Using todir: $todir\n";
}
&doDir("");
sub doDir {
my ($dir) = @_;
print "Working with dir: $dir\n";
if (!(-d "$todir$dir")) {
print "Creating dir: $todir$dir\n";
mkdir "$todir$dir";
}
opendir DIR, "$fromdir$dir";
my @files = readdir DIR;
closedir DIR;
for my $file (@files) {
next if $file eq "." || $file eq "..";
if (-f "$fromdir$dir$file") {
print "Moving file $fromdir$dir$file -> $todir$dir$file\n";
my $time = time;
fmove("$fromdir$dir$file", "$todir$dir$file");
my $sleep = $timer - (time - $time);
if ($sleep>0) {
print "Sleeping $sleep\n";
sleep $sleep;
}
if (++$count==$sleepcount) {
print "Sleeping for ".($sleepmin*60)." seconds\n";
sleep $sleepmin*60;
$count=0;
}
} elsif (-d "$fromdir$dir$file") {
&doDir("$dir$file/");
} else {
print "Unknown type on file: $fromdir$dir$file\n";
}
}
if ($dir ne "") {
print "Deleting dir: $fromdir$dir\n";
rmdir "$fromdir$dir";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment