?
Solved

Issue renaming a file

Posted on 2013-06-17
2
Medium Priority
?
232 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 2000 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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

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

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

801 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