Solved

Issue renaming a file

Posted on 2013-06-17
2
229 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
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

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
compress files in RAR using perl 13 85
Check file date before getting folder path 1 80
create a gui in perl 3 98
Log File Creation with Header and Footer 17 130
I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
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…
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…

820 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