Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

File comparison from 2 directorise

Status
Not open for further replies.

Fezbro

Technical User
Jul 9, 2002
17
0
0
AU
I was wondering if anyone has something that can do the following:

1. Compare the file names of each file in one directory against each file in another. eg Compare files in Temp against files in Live
2. If the file from Temp DOESN'T exist in Live, delete it from temp.
3. Copy all files from Live into Temp

Hope someone can help

Chris
 
If you are using UNIX I would suggest using rsync to perform this task. Not only will it compare the files at two separate locations, it will keep them in sync as well.

M. Brooks
 
Sorry - this is in a windows 2k3 environment....
 
What have you tried so far?

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Crsync works on windows :) I use it to keep ~1.5Tb of data in sync in 2 different cities.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those Who Say It Cannot Be Done Are Usually Interrupted by Someone Else Doing It; Give the wrong symptoms, get the wrong solutions;
 
Though I haven't tested to see if this script works, it might be along the lines of what I have written below. However instead of checking which files exist in temp and not in live then I suggest that the find and replace code isn't the most practical...

Instead maybe use something like this...
unless (grep @log1 eq $_, @log2)

Here is my code anyways...

#! /usr/bin/perl
use strict;
use CGI ':standard';

#################################################
## Open TEMP directory AND open LIVE directory ##
#################################################
opendir (LOGDIR, "Path_To_Temp_Directory") || Error ('open', 'directory');
my @log1 = readdir (LOGDIR);
closedir (LOGDIR);

opendir (LOGDIR, "Path_To_Live_Directory") || Error ('open', 'directory');
my @log2 = readdir (LOGDIR);
closedir (LOGDIR);

##############################################################
## Check which files do not exist within the LIVE directory ##
##############################################################
@log1 =~ s/@log2//;

###################################################################
## Delete the files which do not exist within the LIVE directory ##
###################################################################
unlink ("Path_To_Temp_Directory/@log1") || Error('delete', 'file');

#####################################################
## Use copy module to move files from LIVE to TEMP ##
#####################################################
$templocation = "Location_Of_Temp_Directory";
$livelocation = "Location_Of_Live_Directory";

use File::Copy;

move($livelocation, $templocation);

#########
## END ##
#########
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top