Solved

Issue renaming a file

Posted on 2013-06-17
2
231 Views
Last Modified: 2013-06-23
I am having an issue renaming a file with the File::Find module.

my @initial_directory = ("/Users/test/Development/sandbox/file_strip_example/files");

find (\&wanted, @initial_directory);

sub wanted {
return unless /\s$/;

my $new_name = $File::Find::name;
$new_name =~ s/ +$//; # Remove trailing spaces in the filename
chomp $new_name;

print "Renaming >>$File::Find::name<< to >>$new_name<<\n";
rename $File::Find::name, $new_name;
}


In that location there is a file named: testFile  with a space at the end of the name. For some reason i am unable to strip the space at the end of the file.
0
Comment
Question by:skingsbu1
[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
2 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 39254992
did the rename return a true value or a false value?
what was $! after it returned?
0
 
LVL 12

Accepted Solution

by:
tel2 earned 500 total points
ID: 39258010
Hi skingsbu1,

> In that location there is a file named: testFile  with a space at the end of the name. For some reason i am unable to strip the space at the end of the file.

A few questions for you.

Q1. In the above paragraph, do you mean that this failed?:
    $new_name =~ s/ +$//; # Remove trailing spaces in the filename

Q2. If so, does this work?:
    $new_name =~ s/\s+$//; # Remove trailing whitespace from the filename

I'm wondering whether the "space" is actually some other kind of whitespace like a "tab".  The above command should also remove any new-line/carriage-return characters making the chomp redundant.

Q3. What is the output of the following command, before and after the above change?
    print "Renaming >>$File::Find::name<< to >>$new_name<<\n";

tel2
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

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 …
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…
Six Sigma Control Plans

695 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