Renaming multiple files

Ads
 

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 :)

Ads









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
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
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
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
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
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
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
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   
files
/core/files/storeobjectsinFile.html
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 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
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
Mutliple files upload
Mutliple files upload  Hi Sir, Am doing a project in Jsp and Servlets, i want to upload multiple files inside the grid of the table, and submit the form.Send em the answers as soon as possible..   Please visit
header files
header files   do you have a all header files of java
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
multiple file upload at a time like gmail
multiple file upload at a time like gmail  I want to upload multiple files at a time like gmail. is there any possiblity/solution to do that?   Hi Friend, Please visit the following link: Upload Multiple Files Thanks
Add multiple UIBarButtonItem
Add multiple UIBarButtonItem  add multiple uibarbuttonitem
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

Ads