Rename Directory Folder if does not already exist - Help!!!
Posted on 2004-09-25
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
if (dirname.Length == 5)
dir.Substring(0, dir.LastIndexOf("\\") + 1) + "5" + dirname);
lbl3.Text = "Complete";