Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

regex renaming with existing files

by ArifS (Beadle)
on Nov 03, 2014 at 21:45 UTC ( [id://1105950]=perlquestion: print w/replies, xml ) Need Help??

ArifS has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to rename my subFolders, so that if folder1 exists, it will rename folder1.1, and folder1.2 respectively -
use strict; use warnings; my $existingFile; my $filename; print "provide filename: "; $existingFile = <STDIN>; $filename = "folder1"; chomp $existingFile; chomp $filename; if (-e $filename) { print "File Exists! Renaming with .[x]\n"; my $counter++; rename "$existingFile", "$filename.[$counter]"; } else { rename "$existingFile", "$filename"; } exit;
OUTPUT provide filename: folder1 - Copy File Exists! Renaming with .[x]
It does the first 1, as folder1.1, but doesn't do the 2nd, 3rd, or 4th etc.

Please let me know.

Replies are listed 'Best First'.
Re: regex renaming with existing files
by GrandFather (Saint) on Nov 03, 2014 at 22:21 UTC

    You have been banging on essentially the same issue for at least two months now (see Rename filenames in perl using regex). Maybe it's time for you to step back and gain some fundamental skills? The most important immediate skill you could hone is using resources such as perldoc to figure out how Perl works. Of immediate interest are perlretut, perlre and perlreref.

    Spending a little time and effort now to learn about the tools Perl offers and to learn how to efficiently find the information you need solve problems will save you much more time and effort in the long run than using PerlMonks as a crutch every time you hit a minor issue.

    Perl is the programming world's equivalent of English

      I wish I could up-vote GrandFather's comment twice again!

      You must always remember that the primary goal is to drain the swamp even when you are hip-deep in alligators.
        im just glad no one hazarded to responded with a perl script. shocking!
Re: regex renaming with existing files
by wjw (Priest) on Nov 03, 2014 at 22:07 UTC

    What you are attempting to do is not clear to me. The way you have this written, it is only going to find one folder called "folder1" and rename it as you have specified. To do multiple folders I would think that you would have to have a loop there somewhere.

    Perhaps you want all folders below folder1 renamed? What you are asking for does not seem to line up with what you are coding.

    I think perhaps what you might find helpful is to list out what exactly you have for folders, then write out what you expect to happen to each one. Then you will have an outline of what you have, what you want to have, and psudo-code to write against.

    Then if you have questions, it will be clear what the question is about...

    Hope that is helpful...

    ...the majority is always wrong, and always the last to know about it...

    Insanity: Doing the same thing over and over again and expecting different results...

    A solution is nothing more than a clearly stated problem...otherwise, the problem is not a problem, it is simply an inconvenient fact

Re: regex renaming with existing files
by james28909 (Deacon) on Nov 04, 2014 at 09:00 UTC
    first thing i see is your counter. i could be way off, but you declare my $counter++ without assigning a value to $counter.
    use strict; use warnings; my $existingFile; my $filename; print "provide filename: "; $existingFile = <STDIN>; $filename = "folder1"; chomp $existingFile; chomp $filename; my $counter = 1; #declare counter if (-e $filename) { print "File Exists! Renaming with .[x]\n"; $counter++; #increment already declared counter with value +assigned rename "$existingFile", "$filename.[$counter]"; } else { rename "$existingFile", "$filename"; } exit;
    its a basic counter. you can also put in two counters and rename both sides of the if statement. there is also File::Find and its pretty easy to adjust to :)

    EDIT: changed "my $counter = '1';" to "my $counter = 1;"

      Hmm, not such a good illustration. You initialise $counter with the string '1', not with the number 1. Perl is smart enough to Do What You Mean, but you are telling people who read your code the wrong thing.

      You have also carried over the OP's bad habit of needlessly interpolating variables into strings. Using interpolation for "$filename.[$counter]" is appropriate but elsewhere you should simply use the variable: rename $existingFile, $filename.

      If there is no algorithmic reason to use the post increment operators, always use pre increment. Sticking the ++ or -- out the front (++$counter) makes them easier to see which is a Good Thing.

      Perl is the programming world's equivalent of English
        ah ok, i did not even know you could put ++ or -- on the opposite side. also ill change '1' to 1 so it is a better example.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1105950]
Approved by GrandFather
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (5)
As of 2024-03-29 10:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found