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
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
  • 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
web application structure 18 135
servlet and mdb, jms error 1 81
Java Eclipse Loop 3 47
maven not picking latest jar instead picking old jar from .m2 12 74
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses

734 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