Results 1 to 6 of 6
  1. #1
    Basit56 is offline Member
    Join Date
    Aug 2009
    Posts
    48
    Rep Power
    0

    Default How to check file existence and rename file with little modification

    Hi,
    Hope you all will be fine. I want to ask i created a file named msg0000.wav in a directory named 220. Now i want to check if msg0000.wav already exist in the director then make a new file but with name msg0001.wav in the same directory and after reaching to number 9(msg0009.wav) it becomes msg0010.wav, msg0011.wav and so on. Means each time it check file exist or not and if file exist then simply change the number as i mentioned above.

    Please guide me how can i do it. Checking file is easy i know it is do by a method "exist()" like file.exist() but how can i implement this logic that increment number each time if file exist.

    Thank you.

  2. #2
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    843
    Rep Power
    6

    Default

    Java Code:
    /*Please go thru the code sample*/
    
    import java.io.*;
    class  Test
    {
    	public static void main(String[] args) throws Exception 
    	{
                           //Initially file trial.txt is there in the directory
    		File f = new File("trial.txt");
    		int i=0;
                             
    		while( i < 6)
    		{
    			if(f.exists())
    			{
    				f = new File("trial"+i);
    				f.createNewFile();
    				i++;
    				
    			}
    		}//while
    	}//main
    }//Test
    Ramya:cool:

  3. #3
    gcampton Guest

    Default

    i think you meant to put:

    if(! f.exists())

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,183
    Rep Power
    20

    Default

    Substring the pre-. filename (msg0001) so you grab the last 4 characters, and turn that into an int. Increment that int. String.format() that against a format which will insert a padded number inbetween msg and .wav.

    Of course, this will involve reading every filename in the directory to find the largest id. Better would be to keep track of the last used id (or the next id, possibly) and completely skip the substringing part.

  5. #5
    bbarclay is offline Member
    Join Date
    Oct 2012
    Posts
    1
    Rep Power
    0

    Default Re: How to check file existence and rename file with little modification

    How do you do this with a directory though. So if the directory "folder name" already exists then create a new folder and incriment the name as such. Folder. Folder_1. Folder_2

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,183
    Rep Power
    20

    Default Re: How to check file existence and rename file with little modification

    Um, exactly the same logic?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Unbale to delete and rename file
    By ddatta8 in forum New To Java
    Replies: 0
    Last Post: 01-31-2009, 02:16 PM
  2. How to check whether file is exists or not
    By Java Tip in forum java.io
    Replies: 0
    Last Post: 04-05-2008, 11:13 AM
  3. How to rename a file
    By Java Tip in forum java.io
    Replies: 0
    Last Post: 04-05-2008, 11:11 AM
  4. Replies: 0
    Last Post: 04-05-2008, 11:10 AM
  5. How to Rename a File
    By Ada in forum New To Java
    Replies: 1
    Last Post: 05-26-2007, 01:55 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •