Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2004-09-25
3
Medium Priority
?
3,621 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 2000 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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Suggested Courses

618 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