Results 1 to 8 of 8
Thread: Rename files in a directory
- 05-05-2010, 08:12 PM #1
Member
- Join Date
- May 2010
- Posts
- 15
- Rep Power
- 0
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.
- 05-05-2010, 09:19 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 28
What are the names of your old files?
kind regards,
Jos
- 05-05-2010, 11:28 PM #3
Member
- Join Date
- May 2010
- Posts
- 15
- Rep Power
- 0
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
- 05-05-2010, 11:47 PM #4
Senior Member
- Join Date
- Mar 2010
- Posts
- 952
- Rep Power
- 10
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-
- 05-06-2010, 09:49 AM #5
Member
- Join Date
- May 2010
- Posts
- 15
- Rep Power
- 0
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
- 05-06-2010, 10:11 AM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 28
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"); }
JosLast edited by JosAH; 05-06-2010 at 10:14 AM.
- 05-06-2010, 03:33 PM #7
Member
- Join Date
- May 2010
- Posts
- 15
- Rep Power
- 0
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!
- 05-12-2010, 01:36 PM #8
Member
- Join Date
- May 2010
- Posts
- 15
- Rep Power
- 0
Similar Threads
-
how to find files in given directory
By cecily in forum New To JavaReplies: 7Last Post: 07-04-2014, 01:03 PM -
Upload all the files from the directory
By naveen chedella in forum Web FrameworksReplies: 0Last Post: 03-29-2010, 07:51 AM -
How to. Files and Directory
By ocean in forum New To JavaReplies: 4Last Post: 12-06-2009, 07:23 PM -
load all files in a directory
By moomoo in forum New To JavaReplies: 1Last Post: 04-21-2008, 11:18 AM -
How can I get list of files in a directory
By karma in forum New To JavaReplies: 2Last Post: 12-15-2007, 12:20 AM
Bookmarks