renaming sub dirs problem

Posted on 2004-11-08
Last Modified: 2010-03-31

I have asked similiar question before, but now I am working on renaming all sub dirs and I am testing the following code, but it renames only one sub dir.
          File parentDir = new File(dir.getParentFile().getParentFile().getParentFile().toString());      
          File[] dirs = parentDir.listFiles();  
          //for (int i = 0; i < dirs.length; i++) {
          System.out.println("dirs[0]: " + dirs[0].toString());
          File oldDir = new File(dirs[0].toString());
          System.out.println("*****oldDir " + oldDir.toString());
          File newDir = new File(dirs[0].getParentFile().toString()+"\\"+"SBJ_"+getSubjectID());
          System.out.println("*****newDir  " + newDir.toString());
                  boolean success = oldDir.renameTo(newDir);
                     if (!success)
                           System.out.println("The first renaming was not successful");
                System.out.println("dirs[1]: " + dirs[1].toString());
                File oldDir1 = new File(dirs[1].toString());
                System.out.println("*****oldDir1 " + oldDir1.toString());
                File newDir1 = new File(dirs[1].getParentFile().toString()+"\\"+"SBJ_"+getSubjectID());
                System.out.println("*****newDir1  " + newDir1.toString());
                  boolean success1 = oldDir1.renameTo(newDir1);
                  if (!success1)
                     System.out.println("The second renaming was not successful");      
this is some output from print out statememts.
dirs[0]: C:\scans_test\Scans_for_Header_ext\cd_133\DirOne
*****oldDir C:\scans_test\Scans_for_Header_ext\cd_133\DirOne
*****newDir  C:\scans_test\Scans_for_Header_ext\cd_133\SBJ_null
dirs[1]: C:\scans_test\Scans_for_Header_ext\cd_133\DirTwo
*****oldDir1 C:\scans_test\Scans_for_Header_ext\cd_133\DirTwo
*****newDir1  C:\scans_test\Scans_for_Header_ext\cd_133\SBJ_null
The second renaming was not successful

any ideas why this only rename one dir?
Question by:dkim18
    LVL 86

    Expert Comment

    >>any ideas why this only rename one dir?

    Because you are trying to rename to a name that already exists
    LVL 86

    Accepted Solution

    >>*****newDir  C:\scans_test\Scans_for_Header_ext\cd_133\SBJ_null


    >>*****newDir1  C:\scans_test\Scans_for_Header_ext\cd_133\SBJ_null

    Author Comment

    my bad...
    LVL 86

    Expert Comment


    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Suggested Solutions

    An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
    For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
    Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
    Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now