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

changing graphic name in one dir and writing to different dir. 2

Status
Not open for further replies.

rshandy

Technical User
Dec 26, 2003
91
US
Hi:

I'm reading a text file which has 2 fields, the existing graphic name, and the corresponding new name. I want to use this file to compare each graphic in a directory and write the newly named graphic to a different directory.

I've gotten up to reading and comparing the text file, but I can't seem write the newly named graphic to a different directory.

Here's what I have so far:

my $dirname_new= "//xyz/Site_graphics/ICS/ICS_new";
opendir(DIR, "$dirname_new") or die "can't opendir $dirname_new: $!";
open (FILE, "$imageswap") or die "Cannot open database ($imageswap):$!\n";
##
###open swap file which contains comma delimited data, namely, graphic name and sku
##
while () {
chomp($in = <FILE>);
if ($in) {
($image_name,$newsku)=split(',',$in);
my $dirname="//xyz/Site_graphics/ICS";
opendir(DIR, "$dirname") or die "can't opendir $dirname: $!";
while ($filename = readdir(DIR)){
if ($filename eq $image_name) {
($root,$ext)=$filename=~/(.*)\.(.*)/; # get the root/ext of the file
my $filename_new = "$newsku.$ext";

### this is where I'm stumped. I tried many variations but can't get how to write this to $dirname_new.

}
}


You help would be much appreciated.

Thanks,

Rich

 
use the rename() function

rename($oldname,$newname);

if $newname has a different directory than $oldname, it will "move" the file to the new directory.
 
Be warned though, I don't think rename is supported across disks

--Paul

cigless ...
 
Thanks for the reply guys. I'll try the rename and post back. Paul what did you mean by not support across disks?

Rich
 
I added these 2 lines:

$filename_new = "$dirname_new/$filename_new";
rename ($filename,$filename_new);

When I ran the script is seemed to process without errors but didn't write the files to the new directory.


Rich
 
directory A on disk A, then directory B has to be on Disk A also

HTH
--Paul

cigless ...
 
Thanks Paul.

I'm doing this on the same drive but I can't seem to get it to work. I can't even get it to write the newly named file to the same directory.

Am I not opening/reading the directory properly?

Rich
 
try

Code:
rename ($filename,$filename_new) or die "Can't rename file $!";

--Paul


cigless ...
 
Aha! gave me an error no such file or directory - but I'm reading the filename as I have put some print functions to print to the browser:

while ($filename = readdir(DIR)){
print "<br>";
print $filename;
print "<br>";
if ($filename eq $image_name) {
print "cool !!! <br><br>";
($root,$ext)=$filename=~/(.*)\.(.*)/; # get the root/ext of the file
print "#########<br>";
print $root;
print "<br>#########<br><br>";
print $ext;
print "<br>#########<br><br>";
my $filename_new = "$newsku.$ext";
print "#########<br>";

???

Rich
 
Be warned though, I don't think rename is supported across disks

Good point. His code didn't seem to indicate a write across disks but ya never know. ;-)


 
Yes, I did create the directory.

My bad!, I think I found the problem. I added the complete path in the $filename variable and the $filename_new and then it renamed the file.

If I wanted to copy the file instead of rename/replace (so I can keep the original file in the old directory), can I use the File::Copy module?

Thanks for getting me there...

Rich
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top