Link to home
Start Free TrialLog in
Avatar of superfly18
superfly18

asked on

Script to rename all *.txt extensions successive numbers

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?
Avatar of sudhakar_koundinya
sudhakar_koundinya

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));
              }
}
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++)));
                }
               
            }
        }
ASKER CERTIFIED SOLUTION
Avatar of sudhakar_koundinya
sudhakar_koundinya

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
thanks mate :)