Script to move file & increment filename

Posted on 2004-04-26
Last Modified: 2006-11-17
I need a script that will move a file called "file.txt" from one directory to another and increment the filename during the move. So, as the "file.txt" files are moved from the source directory to the destination directory, they are renamed "file1.txt", "file2.txt", "file3.txt", etc.
Question by:2retread
  • 2
  • 2

Expert Comment

ID: 10923523
you're sure you wanna do this with perl?  perl's a little heavy for something like this.

how many files do you wanna keep?  

the easiest way to do this would be in a simple shell script (or bat for windows):


mv $DEST/file4.txt $DEST/file5.txt
mv $DEST/file3.txt $DEST/file4.txt
mv $DEST/file2.txt $DEST/file3.txt
mv $DEST/file1.txt $DEST/file2.txt
mv file.txt $DEST/file1.txt

and voila
LVL 84

Expert Comment

ID: 10923863
my $n = 0;
for( <destination/file*.txt> ){
     $n = $_+1 if /(\d+)\w+$/ && $1 >= $n;
rename "source/file.txt", "destinaton/file$n.txt";

Author Comment

ID: 10929651
Changed the paths and file name to match my environment. Upon executing the script for the first time, “file.txt” is moved to the archive directory and is renamed “file0.txt.” Subsequent executions of the script results in the file being overwritten with the new file, same file name, i.e., “file0.txt.” I would like the script to rename the subsequent files “file1.txt”, “file2.txt”, etc. Thanks for your help!

my $n = 0;
for( <d:/archive/file*.txt> ){
     $n = $_+1 if /(\d+)\w+$/ && $1 >= $n;
rename "d:/datafiles/file.txt", "d:/archive/file$n.txt";
LVL 84

Accepted Solution

ozo earned 125 total points
ID: 10929861
#sorry, that should have been
    $n = $1+1 if /(\d+)\.txt/ && $1 >= $n;

Author Comment

ID: 10930112
Thanks for your help!!!

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (,  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
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 (…
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…
Internet Business Fax to Email Made Easy - With  eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

911 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now