renaming sub dirs problem

Posted on 2004-11-08
Medium Priority
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
  • 3
LVL 86

Expert Comment

ID: 12527826
>>any ideas why this only rename one dir?

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

Accepted Solution

CEHJ earned 800 total points
ID: 12527836
>>*****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

ID: 12527943
my bad...
LVL 86

Expert Comment

ID: 12527950

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
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…
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses
Course of the Month16 days, 19 hours left to enroll

862 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