Solved

Issue renaming a file

Posted on 2013-06-17
2
226 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
2 Comments
 
LVL 84

Expert Comment

by:ozo
Comment Utility
did the rename return a true value or a false value?
what was $! after it returned?
0
 
LVL 11

Accepted Solution

by:
tel2 earned 500 total points
Comment Utility
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

Highfive Gives IT Their Time Back

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

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…
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…
This video discusses moving either the default database or any database to a new volume.

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