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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Compare two files with same name in different directories 2

Status
Not open for further replies.

sappleg

Programmer
Apr 15, 2008
31
CA
Hi All,

I need to compare two files with the same name in two different directories. What I want to do is to first look if the file exists in one directory. Then to check if the same file exists in the second directory.

For example: I have two directories, i.e. D:\directory1 and D:\directory2. I receive file in directory1 and after receiving first check if the same file already exists in directory2. If it does, rename the file in the directory2 with the suffix '_1'. The file that I receive in Directory1 is not always the same but is suffixed with a datestamp.

Any idea how to do it?
 
To check files and directories existence.


Code:
if (-d $dir ) { 

}


if (-f $file ) { 

}

To rename files

Code:
Use File::Copy;


dmazzini
GSM/UMTS System and Telecomm Consultant

 
Thank you for the information. However, what I want to do is to first compare two files in different directories.

Here's the scenario. I receive one file, e.g. abc_20081112.dat through ftp which is placed in a directory 'd:\mydata\'. Then it is archived in another location 'd:\archived\'. So, after I receive this file from FTP I want to check if the same file already exists in the archive folder. I want to check the complete file name - word to word. The only catch is I can't compare the file with the wildcards, such as 'abc_*.dat', as it contains the datestamp. So, if the file 'abc_20081112.dat' has already been archived in 'd:\archived' then it should rename the same file in archive to 'abc_20081112_1.dat'.

Any tips please?
 
Code:
if (-f "D:/mydata/$file" && !-f "D:/archived/$file") {

and

Code:
my $file = 'abc_20081112.dat';
my $check = $file;
my $suffix = 1;
while (-f $check) {
   $check = $file;
   $check =~ s/^(.+?)\.(...)$/$1\_$suffix\.$2/;
   $suffix++;
}

-------------
Cuvou.com | My personal homepage
Code:
perl -e '$|=$i=1;print" oo\n<|>\n_|_";x:sleep$|;print"\b",$i++%2?"/":"_";goto x;'
 
Thank you guys. I was able to achieve this. It all looks good now.

Appreciate all your help.
 
A quick followup question:

How could I add 1 to one of the memory buffers? E.g. I have tried:

Code:
my $file = "file_1.ext";
$file =~ s/^(.+?)(_?)([0-9]*)\.(...)$/change($1,$2,$3,$4)/e;
sub change(){
	return $_[0] . $_[1] . $_[2]+1 . '.' . $_[3];
}
print $file; # outputs "1.ext" >>> expected output "file_2.ext"

Thanks,

Chris
 
Code:
return $_[0] . $_[1] . ($_[2]+1) . '.' . $_[3];

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top