Create folderstructure based on file.

  • Thread starter Thread starter iCodeSharp
  • Start date Start date
I

iCodeSharp

Hi,

I am struggling with the following issue, i need to copy files from
one place to another...well that isn't so hard..but i also need to
create the folder(s) in which the file orginaly was located. I have a
listbox from where the user can select the files he or she wants to
copy.

So it could be that while there are more then one file in a particular
folder the user only selects one..so i cannot copy the intire
directory and al its subfolders.

Any one got any idea?

Tnx!
Martijn
 
Hey state your problem with more clarity...so that we can help you!









- Show quoted text -

This piece of code is to explain my problem. The code will create this
folder structure in the folder "Test"
Digibox
_Documentation
_Test
D

but i want it to be
D
_Test
_Documentation
Digibox

folderstructure based on FileInfo.FullName

/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button2_Click(object sender, EventArgs e)
{
CopyDirectory(@"D:\_Test\_Documentatie\DigiBox
\Werkverslag.doc", @"D:\Test");
}
/// <summary>
///
/// </summary>
/// <param name="_src"></param>
/// <param name="_dst"></param>
private void CopyDirectory(string _src, string _dst)
{
FileInfo _file = new FileInfo(_src);
DirectoryInfo _dir = new
DirectoryInfo(_file.Directory.FullName);
DirectoryInfo _dest = new DirectoryInfo(_dst);
GetDirStructure(_dir, _dest);
}
/// <summary>
///
/// </summary>
/// <param name="_dirstrc"></param>
/// <param name="_dest"></param>
private void GetDirStructure(DirectoryInfo _dirstrc,
DirectoryInfo _dest)
{
if (_dirstrc.Parent != null)
{
if (_dest.Exists)
if (_regex.IsMatch(_dirstrc.Parent.Name) == true)
{

_dest.CreateSubdirectory(_regex.Match(_dirstrc.Parent.Name).Result("$
{drive}"));
}
else
{
_dest =
_dest.CreateSubdirectory(_dirstrc.Parent.Name);
}
GetDirStructure(_dirstrc.Parent, _dest);
}
}
 
This piece of code is to explain my problem. The code will create this
folder structure in the folder "Test"
Digibox
_Documentation
_Test
D

but i want it to be
D
_Test
_Documentation
Digibox

folderstructure based on FileInfo.FullName

/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button2_Click(object sender, EventArgs e)
{
CopyDirectory(@"D:\_Test\_Documentatie\DigiBox
\Werkverslag.doc", @"D:\Test");
}
/// <summary>
///
/// </summary>
/// <param name="_src"></param>
/// <param name="_dst"></param>
private void CopyDirectory(string _src, string _dst)
{
FileInfo _file = new FileInfo(_src);
DirectoryInfo _dir = new
DirectoryInfo(_file.Directory.FullName);
DirectoryInfo _dest = new DirectoryInfo(_dst);
GetDirStructure(_dir, _dest);
}
/// <summary>
///
/// </summary>
/// <param name="_dirstrc"></param>
/// <param name="_dest"></param>
private void GetDirStructure(DirectoryInfo _dirstrc,
DirectoryInfo _dest)
{
if (_dirstrc.Parent != null)
{
if (_dest.Exists)
if (_regex.IsMatch(_dirstrc.Parent.Name) == true)
{

_dest.CreateSubdirectory(_regex.Match(_dirstrc.Parent.Name).Result("$
{drive}"));
}
else
{
_dest =
_dest.CreateSubdirectory(_dirstrc.Parent.Name);
}
GetDirStructure(_dirstrc.Parent, _dest);
}
}- Hide quoted text -

- Show quoted text -

This seems to help my me...totally different approach but it works

string filename = @"D:\_Test\_Documentatie\DigiBox\";
string drive = _regex.Match(filename).Result("${drive}");
string folderstr =
filename.Substring(filename.IndexOf(@"\"), filename.Length - 2);

if(!Directory.Exists(@"D:\_Test\" + drive + "\\" +
folderstr))
Directory.CreateDirectory(@"D:\_Test\" + drive + "\\" +
folderstr);
 
Back
Top