Solved

Script to rename all *.txt extensions successive numbers

Posted on 2004-10-08
4
198 Views
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

A.txt
B.txt
C.txt

would become

a.001
b.002
c.003

Any Ideas?
0
Comment
Question by:superfly18
  • 4
4 Comments
 
LVL 14

Expert Comment

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

File files[]=dir.listFiles();

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

Expert Comment

by:sudhakar_koundinya
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");
                if(n!=-1)
                {
                    str=str.substring(0,n);
                    files[i].renameTo(new File(str+"."+(k++)));
                }
               
            }
        }
0
 
LVL 14

Accepted Solution

by:
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++)));
0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 12261570
thanks mate :)
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
how do i compare an object based on two fields 6 53
factorial example 4 39
configure dependency in POM for new database 3 25
Java exception bubble up 2 18
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

776 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