Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Script to move file & increment filename

Posted on 2004-04-26
5
Medium Priority
?
1,177 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 500 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

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…
On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
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…
Six Sigma Control Plans

618 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