Solved

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

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
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…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

746 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

10 Experts available now in Live!

Get 1:1 Help Now