Solved

Script to move file & increment filename

Posted on 2004-04-26
5
1,144 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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
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…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

705 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

16 Experts available now in Live!

Get 1:1 Help Now