Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Search and replace file name not working

Posted on 2014-01-31
8
Medium Priority
?
195 Views
Last Modified: 2014-02-03
Hello,

I have a few files in a directory.  The files may or may not have _ in them but if they do I want to replace _ by -
So far I have following-

for my $files_complete ( @files_complete ) 
{ 

	if (-f $files_complete)
	{
	
	move ($files_complete,"c:\\_test_\\test_perl\\movehere");
	my $files_complete1=$files_complete;
	$files_complete=~ s/_/-/g;
	rename (-f $files_complete1, -f $files_complete);
	
 print $files_complete;
	}
}

Open in new window


It prints the output fine but does not actually rename the file name

Thanks,
0
Comment
Question by:perlwhite
  • 4
  • 3
8 Comments
 
LVL 28

Expert Comment

by:FishMonger
ID: 39825669
There are at least two issues that I see.

1) You moved the file to a new directory, but did not specify that path when renaming the file.

2) You used the -f file test operator in the rename statement, which is not a valid parameter for the rename function.
0
 
LVL 85

Expert Comment

by:ozo
ID: 39825817
#why not
for my $files_complete ( @files_complete )
{

      if (-f $files_complete)
      {
      my $files_complete1=$files_complete;
      $files_complete=~ s/_/-/g;
      move ($files_complete1,"c:\\_test_\\test_perl\\movehere\\$files_complete") or die "$files_complete1,c:\\_test_\\test_perl\\movehere\\$files_complete  $!";
 print $files_complete;
      }
}
0
 

Author Comment

by:perlwhite
ID: 39826547
Fishmonger,

I tried following-

if ($status eq 'complete')
{
for my $files_complete ( @files_complete ) 
{ 

	if (-f $files_complete)
	{
	
	my $files_complete1=$files_complete;
      $files_complete=~ s/_/-/g;
 rename ($files_complete1,$files_complete);
	}
}

Open in new window


$files_complete1 and $files_complete have a full path in them.  Why is it still not renaming?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:perlwhite
ID: 39826549
Ozo,

I get error because there is no file with the - in them.
0
 
LVL 28

Expert Comment

by:FishMonger
ID: 39826559
Try adding some error handling to the rename statement.

rename ($files_complete1,$files_complete)
    or warn "failed to rename '$files_complete1' to '$files_complete' <$!>";

Open in new window


You may also want to add an else clause that outputs a message if the -f test fails.
0
 

Author Comment

by:perlwhite
ID: 39827117
Fishmonger.

error thrown 'renaming failed- no such file or directory" but there are files there!
0
 

Author Comment

by:perlwhite
ID: 39827137
I get it now, when I am changing _ to - , it is changing the path too, and that's why it says no such file or directory.

How to do just file's name replacement and not the path?
0
 
LVL 28

Accepted Solution

by:
FishMonger earned 1400 total points
ID: 39827231
use File::Basename;


foreach my $full_path (@files_complete) {
	
    my $path = dirname($full_path);
    my $file = basename($full_path);
    $file =~ s/_/-/g;

    rename($full_path, "$path/$file")
        or die "failed to rename $full_path to $path/$file <$!>";
}

Open in new window

0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans

916 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question