0

I would like to copy my files from source folder to my dest. folder

The location / URL(Name of the column in the table) I got it from here GetDataByGeneralRoot();

Now I would like to copy those file from that URL to a new directory.

What I have done is:

DataSet1.T_DocumentsDataTable docTab = doc.GetDataByGeneralRoot(); string gerneralRootPath = docTab.Rows[0]["URL"].ToString(); gerneralRootPath = gerneralRootPath.Remove(gerneralRootPath.IndexOf("PDF") + 4); string datadirectory = "//ch-s-0001535/G/inetpub/DocAddWeb/DataSource/"; string final = datadirectory + gerneralRootPath; foreach (string path in Directory.GetFiles(final, "*.*", SearchOption.AllDirectories)) { string t = path.Substring(path.IndexOf("\\") + 1); File.Copy(t, t.Replace(final + t, rootFolderAbsolutePath)); } 

My issue / problem is how can I say that I want to get only the files from URL that I got from my method GetDataByGeneralRoot and not all the files what is now happening.

HERE is how my tabel looks like: enter image description here

11
  • It's hard to tell from your source and question but if you are trying to only get the files in the final directory, you need to change SearchOption.AllDirectories to SearchOption.TopDirectoryOnly Commented Jun 13, 2016 at 13:23
  • @Kell I have a ID and this is linked to some URL and I got this id as well as all the URL/ GetDataByGeneralRoot() and now I would like to copy only files that can be associated with the ID Commented Jun 13, 2016 at 13:26
  • In your foreach loop, check whether the file is associated with the ID and if not continue. Commented Jun 13, 2016 at 13:28
  • @Kell YES that is more or less what I want to do but how can I do that ? Commented Jun 13, 2016 at 13:29
  • How are your files associated with the ID? Commented Jun 13, 2016 at 13:31

1 Answer 1

1

I think you want something like this

 public void copyAll(DataSet ds, Doc doc, string rootPath, string rootTargetPath) { ds.T_DocumentsDataTable docTab = doc.GetDataByGeneralRoot(); string datadirectory = "//ch-s-0001535/G/inetpub/DocAddWeb/DataSource/"; string final = datadirectory + rootPath; foreach (var row in docTab.Rows) { var sourceFile = "//ch-s-0001535/G/inetpub/DocAddWeb/DataSource/" + row["URL"].ToString(); string targetPath = rootTargetPath + row["URL"].ToString(); File.Copy(sourceFile, rootTargetPath); } } 
Sign up to request clarification or add additional context in comments.

4 Comments

Instead of +, use Path.Combine() to combine path elements.
@CodeCaster: Agreed.
Thanks allot for you help ! the only issue i have now that rootTargetPath is a directory, not a file.
if you use Path.Combine(rootTargetPath, row["URL"].ToString()) you should get a file.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.