Script to rename all *.txt extensions successive numbers

Posted on 2004-10-08
Last Modified: 2010-03-31
I have a directory of over 700,000 text files.  I need to run them through another program, however in order for that program to work each text file must have numerical extension in the format .00x where x is any number.  What that program does is it takes info in each of the text files and dumps it into a spreadsheet.  What I need to do is figure out how to rename all of the *.txt extensions in the directory so that each file has a different number, and what number they have is not important.  

For example


would become


Any Ideas?
Question by:superfly18
  • 4
LVL 14

Expert Comment

ID: 12260579
File dir =new File("c:/");

File files[]=dir.listFiles();

for(int i=0;i<files.length;i++)
                  files[i].renameTo(new File(files[i].toString().substring(0,files[i].toString().length()-4));
LVL 14

Expert Comment

ID: 12260639
File dir =new File("c:/");
        File files[]=dir.listFiles();
        int k=0;
        for(int i=0;i<files.length;i++) {
            if(files[i].toString().endsWith(".txt")) {
                String str=files[i].toString();
                int n=str.indexOf(".txt");
                    files[i].renameTo(new File(str+"."+(k++)));
LVL 14

Accepted Solution

sudhakar_koundinya earned 250 total points
ID: 12260671
>>files[i].renameTo(new File(str+"."+(k++)));

change to

files[i].renameTo(new File(str+".00"+(k++)));
LVL 14

Expert Comment

ID: 12261570
thanks mate :)

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

Suggested Solutions

Title # Comments Views Activity
Printing to a specific printer tray (HP 806dn printer) 3 83
Java and GPO 11 71
more than one jdk and one jre 1 34
Error in @AspectJ Based AOP with Spring 2 4
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

867 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