Solved

Search and replace file name not working

Posted on 2014-01-31
8
184 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
Comment Utility
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
Comment Utility
#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
Comment Utility
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
 

Author Comment

by:perlwhite
Comment Utility
Ozo,

I get error because there is no file with the - in them.
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 28

Expert Comment

by:FishMonger
Comment Utility
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
Comment Utility
Fishmonger.

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

Author Comment

by:perlwhite
Comment Utility
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 350 total points
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
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…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

743 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now