Click

Synchronization of text files - a simple Perl script

It so happened that I became really need to synchronize files between a limited set of memory stick and the computer. In this hands copied back and forth, remembering what the last file edited - tedious, and the "monsters" are unnecessary because of synchronization primitive task, because a recursive directory synchronization, and other bells and whistles I do not need.

I decided to sketch a simple Perl script (because the gem I have at home and at work, in contrast to eg bash), which would have passed me on the specified files and synchronize them based on the date of last modification, in other words - the most recent files will be replaced outdated.

No sooner said than done. The script compares a pair of files listed in the hash in the body of the script and if necessary, performs the synchronization. The text of the script below, can be useful to someone other than me:

 #! / Usr / bin / perl
 # =================================================
 # DESCRIPTION: A primitive script for a list of pairwise synchronization
 # Text files.  Synchronization is based on time
 # File was last modified.
 #
 # USAGE: Manual to list% files file pair, between which should
 # Synchronize and run the script.
 #
 # AUTHOR: dimio
 # URL: http://www.dimio.org
 # VERSION: 0.1
 # CREATED: 2010-07-08
 # =================================================
 require 5.8.8;
 use warnings;
 use strict;
 use encoding 'utf8', STDOUT => 'utf8';
 our $ VERSION = '0 .1 ';

 # Pairs of files to sync specified in the list below
 #! The correctness of the names as well as the existence of files is not checked!
 my% files = (
     '/ Cygdrive/e/tst1/doc_accounting.odb' => './tst2/database.odb',
     '/ Cygdrive/e/tst1/table.ods' => './tst2/file_for_test.ods',
     # And so on to victory
 );

 # The process has started ...
 foreach my $ file (keys% files) {

     if ((stat ($ file)) [9]> (stat ($ files {$ file})) [9]) {# if the first file is changed after the second - second update

         open (FROM, '<', $ file) or die "Can't open $ file \ n";
         my @ tmp = <FROM>;
         close (FROM);

         open (TO, '>', $ files {$ file}) or die "Can't open $ files {$ file} \ n";
         print TO @ tmp;
         close (TO);

         print '->'. $ files {$ file}. '  is updated from '. $ file, "\ n";
     }

     elsif ((stat ($ file)) [9] <(stat ($ files {$ file})) [9]) {# if a second later than the first - first update

         open (FROM, '<', $ files {$ file}) or die "Can't open $ files {$ file} \ n";
         my @ tmp = <FROM>;
         close (FROM);

         open (TO, '>', $ file) or die "Can't open $ file \ n";
         print TO @ tmp;
         close (TO);

         print '<-'. $ file. '  is updated from ', $ files {$ file}, "\ n";
     }

     else {print '= updating not required', "\ n";} # otherwise the update is not required
     # But under normal conditions the Windows time change always varies on a millisecond
 }

 exit 0;

PS By the way, once again glad that I use Linux - a couple of minutes made for a script with a shortcut icon for the Gnome panel.
It is also the source of the script is available, as always, in the " Software ".

More on similar topics:

Category Filed under: Linux , Coding | Tag Tags: , , , | Comments No Comments

Leave a Reply