So I have been making a program in Java to organize all my files. I am starting with movie files since that is what I have the most of, but I plan on being able to checkBox all file types eventually.
Anyway, I had renameTo working fine when moving all files to a new folder. Next step was trying to alphabetize the files. I got this working fine as well, but obviously I needed to be able to remove articles(i.e. “the”, “a”, “an”) in order to properly alphabetize. I was able to do this in my ArrayList pretty easily, and then decided to actually rename the files.
I did a renameTo in order to rename the file the same filename minus the articles. I used:
File newb = new File(folder+""+filelist[i].getName().substring(4));
if(filelist[i].renameTo(newb)){}
else{
System.out.println("FAILED");
}
Where folder is obviously the folder location the file is in and it is a substring of the original string minus 4 characters at the beginning for the word “The_” or “The.”
It then attempts to rename the file from within the same folder to itself minus the substring.
The result of this code is the file disappearing and file size being listed as 0 when calling the length() of that file. The file is not findable in Windows Explorer, BUT there is no extra space on my HDD!
Now to the questions:
- What is wrong with this? Should I be putting a temp file into another folder or something?
- Is there a way to release the files from Windows so they are not forever clogging up space on my HDD?
- Anything else you need to know? Want to add?
I’m not sure what’s going on, but constructing file names by appending strings can cause problems. (In particular, if
folderdoesn’t end with the path separator character, you might find your moved files in the parent folder. That would explain why the files disappeared without freeing any disk space. So instead of renaming, say,C:\movies\The_Sting.avitoC:\movies\Sting.avi, it ended up asC:\moviesSting.avi.)Try this instead:
Obviously, if all the files in
filelistare from the same folder, then you only have to assign toparentonce (before you start looping).