[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Script to rename all *.txt extensions successive numbers

Posted on 2004-10-08
4
Medium Priority
?
208 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 1000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
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
Course of the Month17 days, 15 hours left to enroll

831 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