Renaming multiple files

Renaming multiple files

Hi,

I have number of files in a directory. Files are named as: 1DeoyaRohit0705$.mp4,2DeoyaRohit0705$.mp4, 3_DeoyaRohit0705$.mp4. ....

Now I wanted to rename these files as: 1RohitBBorse.mp4, 2RohitBBorse.mp4, 3_RohitBBorse.mp4, ....

Please suggest me through java program how I can fulfill this requirement. Instead of going one by one I want to do this at one shot through Java program.

Thanks in advance. :)

  • Rohit Borse
View Answers

May 2, 2012 at 4:41 PM

import java.io.*;
class RenameFiles 
{
    public static String getFileNameWithoutExtension(String fileName) {
          File file = new File(fileName);
          int index = file.getName().lastIndexOf('.');
          if (index>0&& index <= file.getName().length() - 2 ) {
            System.out.println("Filename without Extension: "+file.getName().substring(0, index));
          }  
          return "";
    } 
    public static void main(String[] args){
        String files[]={"c:/1Angelrose0705$.jpg","c:/2Angelrose0705$.jpg","c:/3Angelrose0705$.jpg","c:/4Angelrose0705$.jpg"};
        File oldfile[]=new File[files.length];
        String st[]=new String[files.length];
        for(int i=0;i<oldfile.length;i++){
        oldfile[i]=new File(files[i]);
        int index = oldfile[i].getName().lastIndexOf('.');
          if (index>0&& index <= oldfile[i].getName().length() - 2 ) {
           st[i]=oldfile[i].getName().substring(0, index);
          } 
        }
        File newfile[] = new File[files.length];
        String filename[]=new String[files.length];
        for(int i=0;i<files.length;i++){
        filename[i]=st[i].replace("Angelrose0705$","RohitBBorse");
        newfile[i]=new File(filename[i]+".jpg");
        }

        for(int i=0;i<files.length;i++){
           boolean Rename = oldfile[i].renameTo(newfile[i]);
           if(Rename){
               System.out.println("Renamed Successfully!");
           }
        }
    }
}

May 3, 2012 at 9:58 PM

Thanks friend :)









Related Tutorials/Questions & Answers:
Renaming multiple files
Renaming multiple files  Hi, I have number of files in a directory. Files are named as: 1DeoyaRohit0705$.mp4,2DeoyaRohit0705$.mp4, 3_DeoyaRohit0705$.mp4. .... Now I wanted to rename these files as: 1RohitBBorse.mp4
Reading and writting multiple files
Reading and writting multiple files  how can i read and write say two different files at the same time using threads
Advertisements
Reading multiple xml files
Reading multiple xml files  How can we read many xml files from a folder? The same procedure as that of "listfiles
Upload and Download multiple files
Upload and Download multiple files  Hello Sir/Madam, I need a simple code for upload and download multiple files(it may be image,doc and txt)which has to be store in database and also retrieve the file from database
Dynamic include of multiple files
Dynamic include of multiple files  I want to include a series of small files, based on a string. For instance, if the string was: String fileString="a b c d e"; I would split it into an array, fileArray, and do something like
Merging multiple PDF files - Framework
Merging multiple PDF files  I m using the iText package to merge pdf files. Its working fine but on each corner of the merged filep there is some... the files are having different font.Please help
save output in multiple text files
save output in multiple text files  Hi..I'm getting problem to store the output of this php code into multiple text files. Can anyone sugeest. Here is my code: <?php for($i=1; $i<=100; $i++) { $html = file_get
Uploading Multiple Files Using Jsp
Uploading Multiple Files Using Jsp  ... to understand how you can upload multiple files by using the Jsp. We should avoid... a file. In this example we are going to tell you how we can upload multiple files
Java read multiple files
Java read multiple files In this section you will learn how to read the data of multiple files. Here we have created four files that includes information.... We have to find the students according to particular school from all the files
Uploading the multiple files
. For uploading the multiple files at a time, first create a table as per required...; <HEAD> <TITLE> Multiple Files Upload </TITLE> <BODY>...;#FFFFFF"> <tr> <td><strong>Multiple Files Upload </strong>
Copy multiple files
Copy multiple files        In this section, you will learn how the data of multiple files.... For copping the data of multiple file, you need all files in a specified directory where
retrieve multiple columns values from multiple csv files in java
retrieve multiple columns values from multiple csv files in java  Suppose there is folder in C:\fileupload which contains these two csv files... these two files and store in oracle database as: VendorID,Name,Address and plz
Source file upload by attaching multiple files
Source file upload by attaching multiple files  Hi, I am trying to develop a file upload with multiple files attached functionality in java(like in gmail or in picasa). The source I found so far will select a single file. When I
Source file upload by attaching multiple files
a provision to attach multiple images/files like in gmail. Is there any solution...Source file upload by attaching multiple files  Thanks for the answer. The answer posted in this link I want to have a single text box and when i
Uploading multiple files in JSP - JSP-Servlet
Uploading multiple files in JSP  Hi, I have this code in JSP for Uploading multiple files : Samples : Simple Upload... modification I need to do in the code to upload all the files attached. 
How to convert multiple files in java to .zip format
How to convert multiple files in java to .zip format  i receive multiple files from a remote URL as for (String reportid : reportList...://Answers//Examples"); File[] files = folder.listFiles(); File file=new File
help me plz:-Merge multiple jasper files into Single one
help me plz:-Merge multiple jasper files into Single one  how to Merge multiple jasper files into Single one word doc
find jar file name from multiple set of jar files
find jar file name from multiple set of jar files   I would like to find jar file name from multiple set of jar files where the given class nameââ?¬Â?com.rational.ft.util.debugââ?¬Â
how to upload multiple files in jsp and saving the path in database and the file in folder
how to upload multiple files in jsp and saving the path in database and the file in folder  how to upload multiple files in jsp and saving the path in database and the file in folder I have created a form for the upload of files
java program to read multiple files from a directory and display them on a jframe
java program to read multiple files from a directory and display them on a jframe  hi guys i want to write a java program that will read all files in a directory and display them one by on on a jframe on jpanel with specific key
reading multiple files from a directory and writing them into a single file
reading multiple files from a directory and writing them into a single file... this: /*this program reads multiple files * from a single directory. */ package elite.tech.com... File(inpath); File files[]; files = Folder.listFiles
Uploading Multiple Files Using Jsp
Uploading Multiple Files Using Jsp  ... to understand how you can upload multiple files by using the Jsp. We should avoid... a file. In this example we are going to tell you how we can upload multiple files
ModuleNotFoundError: No module named 'mayan-document_renaming'
ModuleNotFoundError: No module named 'mayan-document_renaming'  Hi...: No module named 'mayan-document_renaming' How to remove the ModuleNotFoundError: No module named 'mayan-document_renaming' error? Thanks   
ModuleNotFoundError: No module named 'mayan-document_renaming'
ModuleNotFoundError: No module named 'mayan-document_renaming'  Hi...: No module named 'mayan-document_renaming' How to remove the ModuleNotFoundError: No module named 'mayan-document_renaming' error? Thanks   
Multiple files upload code using spring mvc 2.0 with xml configuration without using annotations
Multiple files upload code using spring mvc 2.0 with xml configuration without using annotations  how to write code to attach multiple files and upload using spring mvc 2.o with xml configuration with out annotations? please
files
/core/files/storeobjectsinFile.html
files
files  write a java program to calculate the time taken to read a given number of files. file names should be given at command line.   Hello Friend, Try the following code:ADS_TO_REPLACE_1 import java.io.*; import
how to read the values for text and csv files and store those values into database in multiple rows..means one value for one row
how to read the values for text and csv files and store those values into database in multiple rows..means one value for one row  Hai, I need... table in multiple rows(which means one value for one row). eg: my file containes
multiple inheritance.
multiple inheritance.  hello, can java support multiple inheritance???   hi,ADS_TO_REPLACE_1 java does not support multiple inheritance
multiple inheritance
multiple inheritance  Class A extends Class B but Class A also inherit Super Class Object so it is multiple inheritence give reason in support of your answer
multiple inheritance
multiple inheritance  why java doesn't support multiple inheritance? we can achieve this by using interface but not with classes? what is the main reason that java community implemented like
Multiple Inheritance
Multiple Inheritance  Why Java Doesn't have Multiple Inheritance It is Advantage or Disadvantage.........   Hi Friend, When we extends... and inconsistencies Java does not support Multiple Inheritance. Thanks
Multiple Inheritance
Multiple Inheritance  All are saying java doesn't support multiple inheritance but by default Object class is super class for all the user defined... class so java any how do multiple inheritance? Can you please explain
Add multiple UIBarButtonItem
Add multiple UIBarButtonItem  add multiple uibarbuttonitem
multiple inhertence
multiple inhertence  package start; class A { protected void a() //a method in A { System.out.println("Class A"); } } class B extends A { public B() { super
multiple inhertence
multiple inhertence  package start; class A { protected void a() //a method in A { System.out.println("Class A"); } } class B extends A { public B() { super
Files
files
files
files
files
files
mail with multiple attachments
mail with multiple attachments  code for sending mail with multiple attachments in jsp
uinavigationcontroller add multiple buttons
uinavigationcontroller add multiple buttons  How to add multiple buttons to the UINavigationController
multiple language support iPhone
multiple language support iPhone  Is it possible to create an application in iPhone that can support multiple languages.   Yes, iPhone/iPad... the project location and create two project files for example en.lproj
header files
header files   do you have a all header files of java
Uploading files
Uploading files  Hi, Please provide html code for my question. I need to insert the browsed files temporarily to text area while attaching more than one files during mailing
validation files
validation files  If I place validation files in the folder where the action classes are present,will there any error arise
multiple resultset in one resultset
multiple resultset in one resultset  how to retrive multiple resultsets in one resultset in java.? plz help
multiple select values
multiple select values   can you provide an example for multiple select values for html:select tag

Ads