Solved

Script to rename all *.txt extensions successive numbers

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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
ejb wildfly example 2 30
Which non-HTML GUI front end to use with Java? 3 24
jboss 7.1 start up error 1 42
Tagging and Merging on Branch 1 30
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
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…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

807 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