Hi All,

I am trying to copy specific files from a share to a local temp folder on a specified date (not yet implemented)

The trouble I am seeing is that I am trying to use a wildcard within the files as they have random numbers after set characters to which it errors with illegal characters in path.

Any help would be greatly appreciated.

        private void WedArrears()
        {
            string nLine = Environment.NewLine;

            DateTime date = DateTime.Now;

            if (date.DayOfWeek.Equals(DayOfWeek.Wednesday))
            {
                string dpPath = @Properties.Settings.Default.DPPath; // target directory    c:\Temp
                string wedTemp = @Properties.Settings.Default.WedTmpFiles; //source directory  \\yhn\spool$\houlive\hou_output

                try
                {
                    string[] patterns = new string[] { "GA1*.rtf", "GA2*.rtf", "INTW*.rtf", "LET1*.rtf", "RA2*.rtf", "RA3*.rtf", "A4*.rtf", "RA5*.rtf" };
                    var matches = patterns.SelectMany(pat => Directory.GetFiles(wedTemp)
                        .Where(path => System.Text.RegularExpressions.Regex.Match(wedTemp, pat).Success));


                    foreach (string file in patterns)
                    {
                        string dest = dpPath + file;
                        string source = wedTemp + file;
                        File.Copy(source, dest);

                        textResults.AppendText(file + nLine);
                    }
                }
                catch (IOException ex)
                {
                    textResults.AppendText(ex + nLine);
                }

                MessageBox.Show("Successfully Copied Files");
            }
        }

One thing I noticed line 19 should probably use matches not patterns.

For searching the directory with multiple search terms straight LINQ would work well:

                string[] patterns = new string[] { "GA1", "GA2", "INTW", "LET1", "RA2", "RA3", "A4", "RA5" };
                var matches = (from string f in Directory.EnumerateFiles(wedTemp)
                               from string s in patterns
                               where f.Contains(s) 
                               select Path.GetFileName(f)).ToList();

                foreach(string file in matches)
                {
                    string dest = dpPath + @"\" + file;
                    string source = wedTemp + @"\" + file;
                    File.Copy(source, dest,true);
                    textResults.AppendText(file + nLine);
                }

This compares each file name with all the patterns you're checking against and returns a collection of just the file names, and no path info. This means that to copy these files you have to build the path again

Thanks Tinstaffle, just added your code and ran the app and it copied the following file types

HRA218_35.rec.Z
HRA218_41.tra.Z
HRA218_63.rif.Z
HRA218_60.hea.Z
HRA218_25.bac.Z
HRA218_15.hea

Total of over 750 files

I needed just .rtf files starting with what I have in the patterns string array.

Did find it strange that there was only a match on RA2 though.

Hi Tinstaffle, found out the file name is case sensitive :) so it brings everything back plus more, just need to filter on brining the .rtf files only.

Possibly the wrong way to go around it but copy all from a given day and delete all but the .rtf files?

Ken

Have got a little bit further, can now get only .rtf file but also I am getting all rtf files with a modified date of every Wednesday.

How am I able to get the current day only?

Thanks again for the help

{
                    //string dateStart = "04/12/2013 00:00:01 AM";
                    //string dateEnd = "04/12/2013 59:59:59 AM";

                    string[] patterns = new string[] { "ga1_", "ga2_", "intw_", "let1_", "ra2_", "ra3_", "ra4_", "ra5_"};
                    var matches = (from string f in Directory.EnumerateFiles(wedTemp)
                                   from string s in patterns
                                   where f.Contains(s)
                                   where File.GetLastWriteTime(f).DayOfWeek.Equals(DayOfWeek.Wednesday)
                                   where Path.GetExtension(f) == @".rtf" 
                                   select Path.GetFileName(f)).ToList();

                    foreach (string file in matches)
                    {
                        string dest = dpPath + @"\" + file;
                        string source = wedTemp + @"\" + file;
                        File.Copy(source, dest, true);
                        textResults.AppendText(file + nLine);
                    }
                }

See if something like this helps:

                var matches = (from string f in Directory.EnumerateFiles(wedTemp)
                               from string s in patterns
                               where Path.GetExtension(f) == ".rtf" &&  f.StartsWith(s) && File.GetLastWriteTime(f).Date == DateTime.Now.Date
                               select Path.GetFileName(f)).ToList();

The order in which you filter the filenames is important. C# uses shortcutting, which means that as soon as it gets a wrong condition it stops processing that statement. Since you have other files with similar naming patterns I changed one of the conditions to check using StartsWith instead.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.