Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Search and replace file name not working

Posted on 2014-01-31
8
Medium Priority
?
194 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 84

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
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!

 

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
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

722 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