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
Solved

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

Posted on 2004-09-25
3
3,613 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
  • 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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
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…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

839 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