Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Changing File Name Not Working....

 
Ranch Hand
Posts: 483
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Everyone,

I am trying to write a piece of code which changes the file name and removes all the white spaces in it and deletes "-" if it comes for than once in the file name. I have done all the coding part. But the thing when i enter a folder name, none of the file names in the folder change.

Here is my code:


Could you tell me that what i am doing is right or not?
 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Somnath Mallick
I think when you input file is folder name. The first you using method isDirectory() and check folder or file, after that, you list all file in folder and change it.

 
Sheriff
Posts: 22684
128
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First of all, you know that:
a) you are only removing the very last dash, not all but the first
b) you don't need to provide the substring end if you want the remainder; the substring(int) method already takes everything from the given index to the end
c) you can combine the two substring method calls that are used for tempFileName2; simply use lastIndexOf... + 1 in the first call:

There is one flaw in your renaming mechanism, and that is related to the file you rename to. "new File(newFileName)" will create a File object that points to the current directory. That may be different from the directory the file was in. You should use the file's parent:
That said, you should also check whether or not renameTo returns false. It will do so if the renaming fails, among others because the file you want to rename to already exists.
 
Somnath Mallick
Ranch Hand
Posts: 483
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Kia Phia Ben wrote:Hi Somnath Mallick
I think when you input file is folder name. The first you using method isDirectory() and check folder or file, after that, you list all file in folder and change it.


I am doing the check in a different class. This is just part of a larger code!



I modified the code and this seems to work!



That said, you should also check whether or not renameTo returns false. It will do so if the renaming fails, among others because the file you want to rename to already exists.



How can i make the code, do the renaming even if the file already exists?
 
Rob Spoor
Sheriff
Posts: 22684
128
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Somnath Mallick wrote:


That will fail if there is no \ in the absolute file path. Although on Windows this will not occur, on Linux and Mac OS it will most definitely. Just use the parent file as I have shown you. This may return null if there is no parent but the File constructor allows a null parent.

How can i make the code, do the renaming even if the file already exists?


You will have to make sure that the destination file does not exist first. For files you can simply delete() it (if you are allowed to; delete() can also return false). For folders you will need to delete all files and folders in it first.

For folders you may want to merge the folder contents though (your choice: delete, merge or fail). That involves moving (renaming) all files inside the folder you want to rename first, then remove the folder you want to rename (or from the existing to the one you want to rename).

Either way, both require a recursive call. You can find the one for deleting by doing a search around this board. The merging is a bit harder but the same principle holds.
 
Somnath Mallick
Ranch Hand
Posts: 483
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Rob for the suggestions. I have implemented your code change!
 
rubbery bacon. crispy tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic