Solved

Script to move file & increment filename

Posted on 2004-04-26
5
1,155 Views
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.
0
Comment
Question by:2retread
  • 2
  • 2
5 Comments
 
LVL 7

Expert Comment

by:fim32
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):

#!/bin/sh
DEST=/path/to/destination/

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
0
 
LVL 84

Expert Comment

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

Author Comment

by:2retread
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";
0
 
LVL 84

Accepted Solution

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

Author Comment

by:2retread
ID: 10930112
Thanks for your help!!!
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
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…

832 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