CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner: Re: [zolteck06] rename files in DIR using autoincrement for the file numbers: Edit Log



Kenosis
User

Apr 9, 2013, 10:09 AM


Views: 381
Re: [zolteck06] rename files in DIR using autoincrement for the file numbers

Always:


Code
use strict; 
use warnings;


at the top of your scripts.

Use lexically-scoped variables (my).

Consider using a glob (<>) instead of reading the directory contents.

It isn't necessary to sort the files--especially through each iteration of the file list. You can safely omit the following:


Code
@files = sort(@files); 
for($i = 0 ; $i <= 2 ; $i++){
shift @files;
}


Consider just concatenating the number to the file name before renaming.

Given the above, consider the following:


Code
use strict; 
use warnings;

my $files = '/root/temp2/perl/test3';

my $i = 1;
for (<$files*>) {
rename $_, $_ . $i++ or print "Error renaming: $_\n";
}


Hope this helps!


(This post was edited by Kenosis on Apr 9, 2013, 10:10 AM)


Edit Log:
Post edited by Kenosis (User) on Apr 9, 2013, 10:10 AM


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives