Solved

Script to rename all *.txt extensions successive numbers

Posted on 2004-10-08
4
196 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
Comment Utility
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
Comment Utility
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
Comment Utility
>>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
Comment Utility
thanks mate :)
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

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…
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
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:

771 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

11 Experts available now in Live!

Get 1:1 Help Now