Solved

Rename Directory Folder if does not already exist - Help!!!

Posted on 2004-09-25
3
3,616 Views
Last Modified: 2012-05-05
I have the following script - which identifies folders in a directory and renames a folder if it meets a certain criteria - specifically, if the folder name consists of 5 total characters in length.  If it locates the folder that meets this criteria the script will prefix the folder name with a "5" - as all my folders must consist of 6 total characters in length.  

That being said, it appears that some yahoo went ahead and renamed more than a few to 6 total charcters by hand (meaning they took a folder such as "XXXXX" and manually renamed it as "5XXXXX" - now my script is telling me that when it gets to "XXXXX" it cannot rename it to "5XXXXX" as the folder already exists - which brings me to my question - can I identify and exclude such instances so that they are just skipped in the script below:

      private void Button3_Click(object sender, System.EventArgs e)
            {
                  string path = "\\\\dadt01\\dadoc01\\DOCS\\Dallas\\"; //just for an example I use temp

                  string[] dirs = Directory.GetDirectories(path); //retrieve the directories in the given folder

                  foreach (string dir in dirs)  //look at each of those directories
                  {
                        // Get just the directory name by stripping the c:\\temp\ or whatever happens to be there
                        string dirname = dir.Substring(dir.LastIndexOf("\\") + 1);
                        // if it is less than 6 then call the Directory.Move, which renames the folder, with the old location and the new
                        // location
                        if (dirname.Length == 5)
                        {
                              Directory.Move(dir,
                                    dir.Substring(0, dir.LastIndexOf("\\") + 1) + "5" + dirname);
                        }
                  }
                  lbl3.Text = "Complete";

0
Comment
Question by:tbaseflug
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 5

Accepted Solution

by:
tzxie2000 earned 500 total points
ID: 12150732
you can use Directory.Exists to test whether the directory is exist

like code below:
private void Button3_Click(object sender, System.EventArgs e)
          {
               string path = "\\\\dadt01\\dadoc01\\DOCS\\Dallas\\"; //just for an example I use temp

               string[] dirs = Directory.GetDirectories(path); //retrieve the directories in the given folder

               foreach (string dir in dirs)  //look at each of those directories
               {
                    // Get just the directory name by stripping the c:\\temp\ or whatever happens to be there
                    string dirname = dir.Substring(dir.LastIndexOf("\\") + 1);
                    // if it is less than 6 then call the Directory.Move, which renames the folder, with the old location and the new
                    // location
                    if (dirname.Length == 5)
                    {
                         if(!Directory.Exists(dir.Substring(0, dir.LastIndexOf("\\") + 1) + "5" + dirname))//<----test whether the move to direcotry is exists,if exists do not move the directory
                         Directory.Move(dir,
                              dir.Substring(0, dir.LastIndexOf("\\") + 1) + "5" + dirname);
                    }
               }
               lbl3.Text = "Complete";
}
0
 

Author Comment

by:tbaseflug
ID: 12150758
tzxie2000 -

Thanks giving it a shot now.
0
 
LVL 5

Expert Comment

by:tzxie2000
ID: 12151019
then could this solve your problem?
0

Featured Post

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

726 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