Solved

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

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…

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