Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8809311
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T02:50:32+00:00 2026-06-14T02:50:32+00:00

How do recursively check and rename the file if it already exist by appending

  • 0

How do recursively check and rename the file if it already exist by appending some incrementing number?

I wrote the below function but it gives me an exception

org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object 'E:\Projects\repo1\in_conv1.xml' with class 'java.lang.String' to class 'java.io.File'

Code

//newFilePath = E:\Projects\repo1\old\testcustprops.xml
String newFilePath = checkForExistenceAndRename(newFilePath,false)

private String checkForExistenceAndRename(String newFilePath, boolean flag){
    File f = new File(newFilePath)
    if(!flag){
        if(f.exists()){
            //renaming file
            newFilePath=newFilePath[0..-5]+"_conv${rename_count++}.xml"
            f = checkForExistenceAndRename(newFilePath,false)
        }
        else 
            f = checkForExistenceAndRename(newFilePath,true)
    }
    else
        return newFilePath      
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T02:50:34+00:00Added an answer on June 14, 2026 at 2:50 am

    You are trying to do:

    f = checkForExistenceAndRename(newFilePath,false)
    

    Where f is a File. But your function returns a String

    Not sure if it works or not (I haven’t tested your function), but you could try:

    private String checkForExistenceAndRename(String newFilePath, boolean flag){
        File f = new File(newFilePath)
        if(!flag){
            if(f.exists()){
                //renaming file
                newFilePath = newFilePath[0..-5]+"_conv${rename_count++}.xml"
                newFilePath = checkForExistenceAndRename(newFilePath,false)
            }
            else 
                newFilePath = checkForExistenceAndRename(newFilePath,true)
        }
        return newFilePath      
    }
    

    Also, there’s no need to use recursion…

    Why not just do:

    private String getUniqueName( String filename ) {
      new File( filename ).with { f ->
        int count = 1
        while( f.exists() ) {
          f = new File( "${filename[ 0..-5 ]}_conv${count++}.xml" )
        }
        f.absolutePath
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to recursively get some data using connect by, which for each row
I tried to recursively get all files and folder list.But I can only get
I need to add an header recursively to several file according to the name
I've got the following function which i'm using to check if a directory is
Some times the Facebook resources are not load for eg.all.js file that collapse my
This is a C program I wrote to recursively navigate and output directories and
I like to create a batch file (winxp cmd) that recursively goes through a
I'm doing like this ..... but i'm not able succeed... For the check out
I'm rather new to the community but I've seen some helpful posts on here
I want to check if a file in the directory I pass has a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.