C# - copying directory, rename if exists

odoe

[H]F Junkie
Joined
Oct 10, 2001
Messages
9,796
Hi there.
I was hoping someone could help me out here or point me in the right direction.
I have a .NET service that is used to copy a directory after a user performs a query.
The reason this needs to happen as a copy service is the query is saved in an arcane 24 digit directory I can't control the naming of, so I copy their stuff to a directory name of their choice. I'm trying to find a way to force the directory copy to add a "_" to the end of the directory name if it already exists. The users will be aware that this can happen. I've tried a couple of things, but none seem to work properly.

Code is below, snagged from http://www.codeproject.com/KB/files/copydirectoriesrecursive.aspx

Code:
[SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]// Utility services
[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]// Copy directory structure recursively
[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][[/SIZE][SIZE=2][COLOR=#2b91af][SIZE=2][COLOR=#2b91af]WebMethod[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]]
[/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]public[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]string[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] CopyDirectory([/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]string[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] Src, [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]string[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] Dst)
{
[/SIZE][SIZE=2][COLOR=#2b91af][SIZE=2][COLOR=#2b91af]String[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][] Files;
[/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]string[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] strSuccess = [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]""[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2];
[/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]if[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] (Dst[Dst.Length - 1] != [/SIZE][SIZE=2][COLOR=#2b91af][SIZE=2][COLOR=#2b91af]Path[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].DirectorySeparatorChar)
Dst += [/SIZE][SIZE=2][COLOR=#2b91af][SIZE=2][COLOR=#2b91af]Path[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].DirectorySeparatorChar;
[/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]if[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] (![/SIZE][SIZE=2][COLOR=#2b91af][SIZE=2][COLOR=#2b91af]Directory[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].Exists(Dst))
{
[/SIZE][SIZE=2][COLOR=#2b91af][SIZE=2][COLOR=#2b91af]Directory[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].CreateDirectory(Dst);
strSuccess = [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"Copy succeeded"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2];
}
[/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]else
[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]{
strSuccess = [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"Directory already exist"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2];
[/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]return[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] strSuccess;
[/SIZE][SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]//char[] myCharArray = @"\".ToCharArray();
[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]//Dst = Dst.TrimEnd(myCharArray);
[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]//if (Dst[Dst.Length - 1] != Path.DirectorySeparatorChar)
[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]// Dst += Path.DirectorySeparatorChar;
[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]//Directory.CreateDirectory(Dst + "_");
[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]}
Files = [/SIZE][SIZE=2][COLOR=#2b91af][SIZE=2][COLOR=#2b91af]Directory[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].GetFileSystemEntries(Src);
[/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]foreach[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] ([/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]string[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] Element [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]in[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] Files)
{
[/SIZE][SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]// Sub directories
[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]if[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] ([/SIZE][SIZE=2][COLOR=#2b91af][SIZE=2][COLOR=#2b91af]Directory[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].Exists(Element))
CopyDirectory(Element, Dst + [/SIZE][SIZE=2][COLOR=#2b91af][SIZE=2][COLOR=#2b91af]Path[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].GetFileName(Element));
[/SIZE][SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]// Files in directory
[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]else
[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#2b91af][SIZE=2][COLOR=#2b91af]File[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].Copy(Element, Dst + [/SIZE][SIZE=2][COLOR=#2b91af][SIZE=2][COLOR=#2b91af]Path[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].GetFileName(Element), [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]true[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]);
}
[/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]return[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] strSuccess;
}
[/SIZE]

This is the part I'm trying to make work, the non-working portion has been commented out for now.
Code:
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]else
[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]{
strSuccess = [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"Directory already exist"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2];
[/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]return[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] strSuccess;
[/SIZE][SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]//char[] myCharArray = @"\".ToCharArray();
[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]//Dst = Dst.TrimEnd(myCharArray);
[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]//if (Dst[Dst.Length - 1] != Path.DirectorySeparatorChar)
[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]// Dst += Path.DirectorySeparatorChar;
[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]//Directory.CreateDirectory(Dst + "_");
[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]}
[/SIZE]

Using the code if I remove the comments creates my directory with the appended "_", but doesn't copy the files in it. Am I just doing something out of order here, or am I improperly trimming the directory name or is there an easier way to check if a directory exists from the beginning?

And the reason I don't want to just tell them to use a new name if it already exists is that the process to create the original directory can take a couple of minutes depending on the query, so I'd like to make these steps as transparent to them as possible.

Thanks for any assistance in advance.
 
I am so dumb, I just saw what I was doing wrong.
I was trying to append "_" AFTER assigning the path separator, doh!

char[] myCharArray = @"\".ToCharArray();
Dst = Dst.TrimEnd(myCharArray);
Dst = Dst + "_";
if (Dst[Dst.Length - 1] != Path.DirectorySeparatorChar)
Dst += Path.DirectorySeparatorChar;
Directory.CreateDirectory(Dst);


Thanks

I obvisouly need someone to bounce this stuff off of. :p
 
Back
Top