Solved

Search and replace file name not working

Posted on 2014-01-31
8
191 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
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 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

Suggested Solutions

Title # Comments Views Activity
Sending email via Perl on Windows 3 210
Need help with bash and/or perl commands on OS X Terminal 9 114
Perl tar error 8 74
Perl Versus AWK? 7 97
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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…

739 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