Results 1 to 8 of 8
  1. #1
    rgeurts is offline Member
    Join Date
    May 2010
    Posts
    15
    Rep Power
    0

    Default Rename files in a directory

    Hello,

    I am trying to create a java app that scans through a specfic folder and renames all the files in it.

    My code seems to be working but it refuses to rename any files.
    I have spent hours looking at it and i don't know what's wrong.
    Can anyone please have a quick look at it and let me know if you can spot any errors.


    Java Code:
    import java.io.*;
    
    public class Reader {
    
    public static void main(String[] args){
    
    File myDir = new File("c:/temp");
    
    String[] myFiles = myDir.list();
    
    for (int i = 0; i < myFiles.length; i++){
    
    System.out.println("processing file: " + myDir.getAbsolutePath() + "/" + myFiles[i]);
    
    
    File oldFile = new File(myFiles[i]);
    
    File newFile = new File("c:/temp/test"+i);
    
    System.out.println("......The file will be renamed to " + newFile);
    
    boolean isFileRenamed = oldFile.renameTo(newFile);
    
    if(isFileRenamed) System.out.println("......File has been renamed");
    
    else System.out.println("......Error renaming the file");
    
    			}
    
    		}
    
    }
    Last edited by rgeurts; 05-05-2010 at 11:29 PM.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default

    What are the names of your old files?

    kind regards,

    Jos

  3. #3
    rgeurts is offline Member
    Join Date
    May 2010
    Posts
    15
    Rep Power
    0

    Default

    Thanks for taking the time to help Jos.
    The names of my old files are:
    -blabal.txt
    -pleaseworkpieceofshit.txt

    I tried renaming the files numerous times. Also I changed the file types.

    Can you try to run the code on your computer and see what happens?

    Thanks

  4. #4
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    952
    Rep Power
    5

    Default

    1. C:\TEMP is probably safe enough, but there are some versions of Windows and other software that put things there, so be careful.

    2. Take another good long look at the Jave File API document. Particularly look at the renameTo() method.

    -Gary-

  5. #5
    rgeurts is offline Member
    Join Date
    May 2010
    Posts
    15
    Rep Power
    0

    Default

    Hi Gary,

    Thanks for the response.
    i spent hours looking for an anser on this one but I am not smart enough to find anything wrong with my code

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by rgeurts View Post
    Hi Gary,

    Thanks for the response.
    i spent hours looking for an anser on this one but I am not smart enough to find anything wrong with my code
    Suppose you have a file c:/usr/foo/data.txt. It'll show up in your array as a string "data.txt", i.e. no path information is passed. When you reconstruct a new File object for the file you're reconstructing it relative to where your java program runs (the file doesn't exist there). Do this instead:

    Java Code:
    File myDir = new File("c:/tmp/");
    
    File[] myFiles = myDir.listFiles();
    
    for (int i = 0; i < myFiles.length; i++) {
    
    	System.out.println("processing file: " + myDir.getAbsolutePath()
    					+ "\\" + myFiles[i]);
    
    	File oldFile = myFiles[i];
    
    	File newFile = new File("c:/tmp/test" + i);
    
    	System.out.println("......The file will be renamed to " + newFile);
    
    	boolean isFileRenamed = oldFile.renameTo(newFile);
    
    	if (isFileRenamed)
    		System.out.println("......File has been renamed");
    	else
    		System.out.println("......Error renaming the file");
    }
    kind regards,

    Jos
    Last edited by JosAH; 05-06-2010 at 10:14 AM.

  7. #7
    rgeurts is offline Member
    Join Date
    May 2010
    Posts
    15
    Rep Power
    0

    Default

    Hi Jos,

    Thanks very much for the code.
    I will give it a shot tonight and will let you know if it solved my issue.

    Thanks again!

  8. #8
    rgeurts is offline Member
    Join Date
    May 2010
    Posts
    15
    Rep Power
    0

    Default

    Hello Jos,

    Thanks for helping out.
    I have got my first java app working now.

Similar Threads

  1. how to find files in given directory
    By cecily in forum New To Java
    Replies: 7
    Last Post: 07-04-2014, 01:03 PM
  2. Upload all the files from the directory
    By naveen chedella in forum Web Frameworks
    Replies: 0
    Last Post: 03-29-2010, 07:51 AM
  3. How to. Files and Directory
    By ocean in forum New To Java
    Replies: 4
    Last Post: 12-06-2009, 07:23 PM
  4. load all files in a directory
    By moomoo in forum New To Java
    Replies: 1
    Last Post: 04-21-2008, 11:18 AM
  5. How can I get list of files in a directory
    By karma in forum New To Java
    Replies: 2
    Last Post: 12-15-2007, 12:20 AM

Posting Permissions

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