in reply to Renaming files sequentially into directory

Simple method (brute force):

#!/usr/bin/env perl use strict; use warnings; my @files = ( '000001.OrderID.html', '000002.OrderID.cgi', '000004.OrderID.js', '000005.OrderID.txt', '000006.OrderID.cgi', 'OrderID.master' ); my $i = 1; for my $infile (sort grep /^\d{6}\./, @files) { my $repl = sprintf ("%6.6i", $i++); (my $outfile = $infile) =~ s/^\d{6}/$repl/; rename $infile, $outfile; }

You can replace the initial set-up of @files with a glob.

Update: comma was absent from penultimate line.

Replies are listed 'Best First'.
Re^2: Renaming files sequentially into directory
by Magnolia25 (Sexton) on Sep 19, 2018 at 14:55 UTC

    Thanks hippo for looking at this.

     push (@renamefiles, $outfile);

    Indeed I can see the files renamed now, but 'OrderID.master' file is skipped, I need this file also in my @renamefiles array and name 'OrderID.master'should remain as is

    Also, Instead of pushing the files to @renamefiles , can I do this change in same array @files and check the file name changed in @files array

    Thanks again for your help.

      This is a new feature - there was no mention of it in your original post. So, instead of re-engineering the solution already provided, you can just populate your @renamefiles array with a fresh glob after the renames have completed.