M1M N7
M1M N7

Reputation: 69

Java renaming a old directory or deleting it to make a new one

I am having an issue with a panel i am working on, what I am working on is whenever there is an upgrade from one version of the software to another the older start menu group on the windows machine still shows. I have tried to see if i can delete the old directory first and then make the new one but all that happens is the old one is still staying. Not really sure how to go about it all I need is just one directory in the start menu group to show weather I delete or rename the old.

Here is what I have so far to delete the old system variable. This is something I am working on and i know its not perfect but any help would be appreciated. If you need explanations I would be happy to comply. Thank you

 if ( PrioGlobals.UPGRADE ){
        oldm_txtGrpName.setText(PrioGlobals.StartMenuFolder);
    }

if ( PrioGlobals.UPGRADE ){   
           String oldShortCutPath = PrioGlobals.cre.getShorcutDirectory();
           String oldStartMenuGroupFolderPath = oldShortCutPath + File.separator + BrandStrings.WIN_FOLDER_NAME + 
        File.separator +oldm_txtGrpName.getText(); 

           File oldStartmenu = new File(oldStartMenuGroupFolderPath);
           if (oldStartmenu.exists())
           {
               oldStartmenu.delete();
           }
           else
               oldStartmenu.delete();
           }        

Upvotes: 1

Views: 170

Answers (1)

Guido Simone
Guido Simone

Reputation: 7952

If I am reading your code correctly, oldStartmenu is a directory? In that case, oldStartmenu.delete() will probably fail because the directory is not empty (see File delete) In which case you need to delete the directory and all of it's files recursively.

At which point I would recommend including commons-io FileUtils and use one of the directory utilities such as forceDelete or deleteDirectory

[update]

Once you have added the commons-io library to your project you would

// import FileUtils
import org.apache.commons.io.FileUtils;

// ...

if (oldStartmenu.exists())
{
    FileUtils.deleteDirectory(oldStartmenu);
}

Upvotes: 2

Related Questions