Solved

Issue renaming a file

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
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…
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 Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

770 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