Solved

Search and replace file name not working

Posted on 2014-01-31
8
187 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 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
ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

 

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

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
HTTP Error 502.2 - Bad Gateway 3 216
remove duplicates from the csv file 13 108
Union rows in array that have common elements 2 96
perl syntax 3 4
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
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…
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…

773 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