-2

I'm using two DateTimePickers to specify a date range, then I'm using a CheckedListBox to specify some strings for filenames with wildcards to enumerate in each day's subdirectory contained within a system environment variable path. I want to copy from that source to a destination using FileInfo.Copy.

I have my code already creating the necessary directories. But I'm having trouble specifying the destination filenames -- they are not being specified at all with how I have this written.

I was thinking of using regular expressions, but after some digging I found this MSDN article that seems to do what I want already. I think I need to alter my code in order to use it. I could use some assistance fitting what I already have into what MSDN shows in its example.

I have been on this part of my program for a month now, which has led me to learn quite a bit about c#, parallel programming, async, lambda expressions, background workers, etc. What seems should be simple has become a big rabbit hole for me. For this question I just need a nudge in the right direction, and I will greatly appreciate it!

Here is my code as it stands:

    private async void ProcessFiles()
    {

        // create a list of topics
        var topics = topicsBox.CheckedItems.Cast<string>().ToList();

        // create a list of source directories based on date range
        var directories = new List<string>();
        var folders = new List<string>();
        for (DateTime date = dateTimePicker1.Value.Date;
            date.Date <= dateTimePicker2.Value.Date;
            date = date.AddDays(1))
        {
            directories.Add(_tracePath + @"\" + date.ToString("yyyy-MM-dd") + @"\");
            folders.Add(@"\" + date.ToString("yyyy-MM-dd") + @"\");
        }

        // create a list of source files to copy and destination
        // revise based on https://msdn.microsoft.com/en-us/library/kztecsys.aspx?f=255&MSPPError=-2147217396
        foreach (var path in directories)
        {
            var path1 = path;
            try
            {
                foreach (var files2 in folders)
                {
                    // create the target directory
                    var destPath = textBox1.Text + @"\" + textBox4.Text + files2;
                    Console.WriteLine("Target directory is {0}", destPath);
                    Console.WriteLine("Destination filename is {0}", files2);
                    foreach (var files in topics)
                    {
                        foreach (string sourcePath in Directory.EnumerateFiles(path1, files + "*.*", SearchOption.AllDirectories))
                        {
                            // copy the files to the temp folder asynchronously
                            Console.WriteLine("Copy {0} to {1}", sourcePath, destPath);
                            Directory.CreateDirectory(sourcePath.Replace(sourcePath, destPath));
                        }
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }
    }
Dshiz
  • 3,099
  • 3
  • 26
  • 53
  • "Simplify my code" is NOT a valid and allowed type of question here. You must ask specific technical questions only. Try reading other peoples questions to see what is and is not allowed. – JK. Jul 07 '15 at 03:48
  • I will update the title. – Dshiz Jul 07 '15 at 03:48
  • @JK. will "Help, I'm stuck; I don't want to break what I have working now." suffice? – Dshiz Jul 07 '15 at 03:59
  • I just noticed in your comment you mentioned "copy .. asynchronously". Do you need to do it asynchronously? I'd suggest doing it synchronously first and making sure your logic is correct. – Sam Jul 07 '15 at 04:02
  • @Sam, I was hoping to do that after I get the destination filename with destination path working. – Dshiz Jul 07 '15 at 04:04

1 Answers1

1

So sourcePath contains the source path and filename. You can easily construct the destination path from that, like so:

// Get just the filename of the source file.
var filename = Path.GetFileName(sourcePath);

// Construct a full path to the destination by combining the destination path and the filename.
var fullDestPath = Path.Combine(destPath, filename);

// Ensure the destination directories exist. Don't pass in the filename to CreateDirectory!
Directory.CreateDirectory(destPath);

Then you can copy the file (synchronously) like this:

File.Copy(sourcePath, fullDestPath);
Sam
  • 3,320
  • 1
  • 17
  • 22
  • Right now, the destPath does not contain the filename. sourcePath has the entire path including the filename. I'm not sure how to pass that into destPath. – Dshiz Jul 07 '15 at 04:02
  • Sorry I should have looked at the code a little closer (when there's a lot of code to explain a seemingly simple problem, I (and I'm sure others) often read it a little too hastily!). I'll edit my answer. – Sam Jul 07 '15 at 04:04
  • 1
    It's okay.. It shows my lack of knowledge / experience, which is why I'm here asking for help! Thank you for looking it over.. – Dshiz Jul 07 '15 at 04:07
  • I'm pretty sure this is what I needed. I'm still working on how to fit this into what I already have, but what I needed was Path.Combine() absolutely! Thank you so much! – Dshiz Jul 07 '15 at 04:27
  • 1
    No problem! If you find yourself manipulating path components by hand, definitely look at `System.IO.Path`. You'll probably find something to do what you need. – Sam Jul 07 '15 at 04:31
  • 1
    I just wanted to let you know this worked like a charm for me! I have the MSDN example incorporated in and it's working flawlessly. Thanks again! – Dshiz Jul 07 '15 at 21:07