Exemplo n.º 1
0
        /// <summary>
        /// Very long function that does a simple task. Read in the options the user set for the operation, and
        /// Decide on the timestamp it should use, by the end we will have a single object with 3 times.
        /// This will need to be hit with broad strokes if we attempt to do any more work on the program.
        /// </summary>
        internal NameDateObj DecideWhichTimeMode2(string directoryPath)
        {
            var extractlist = new List <string>();

            var timelist = new List <NameDateObj>();

            if (ObtainParseSpecifiedTimeGUIBoxes())
            {
                return(thingtoreturn);
            }
            //Begin checking Conditional for which file is newest oldest etc
            if (!gui.radioGroupBox3UseTimeFrom)
            {
                return(thingtoreturn);
            }
            //decide if they wanted to use time from subfile or subdir
            if (gui.radioButton1_useTimefromFile)
            {
                try
                {
                    // Get List of the subfiles (full path)
                    extractlist.AddRange(from subFile in Directory.GetFiles(directoryPath) let fileAttribs = File.GetAttributes(subFile)
                                                                                                             where (fileAttribs & SharedHelper.SyncSettingstoInvisibleFlag()) == 0 select Path.Combine(directoryPath, subFile));
                } // catch failure of GetAttributes
                catch (FileNotFoundException ex)
                {
                    MessageBox.Show(
                        "Error getting attributes of a file in '" + directoryPath + "': \r\n\r\n" + ex.Message,
                        @"PEBKAC Error ", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }   // catch failure of GetFiles
                catch (UnauthorizedAccessException)
                { } //show nothing because this is normal for this method when encountering inaccessible files
            }
            else if (gui.radioButton2_useTimefromSubdir)
            {
                try
                {
                    // Get List of the subdirs (full path)
                    extractlist.AddRange(Directory.GetDirectories(directoryPath).Select(subDirectory => Path.Combine(directoryPath, subDirectory)));
                }   // catch failure from GetDirs
                catch (UnauthorizedAccessException)
                { } //show nothing because this is normal for this method when encountering inaccessible dirs
            }

            // ## Exit out early:
            // if the list is empty, theres nothing to do, then return an empty object.?????
            if (extractlist.Count == 0)
            {
                return(thingtoreturn);
            }

            // ---------------------------------------------------------------------------------//
            // ## MID WAY POINT ##
            // We have our first list and now we apply it to the 3 other CMA lists per all the files
            // And where we actually decide whether to keep our time or use the new time.
            // ---------------------------------------------------------------------------------//

            foreach (string fullpath in extractlist)
            {
                var decidetemp = new NameDateObj {
                    Name = fullpath
                };
                //grab all 3 times and put them in a decidetemp object
                try
                {
                    decidetemp.Created  = File.GetCreationTime(fullpath); // File Class works on dirs also
                    decidetemp.Modified = File.GetLastWriteTime(fullpath);
                    decidetemp.Accessed = File.GetLastAccessTime(fullpath);
                    //add the temp object to the list of objects
                    timelist.Add(decidetemp);
                }
                catch (UnauthorizedAccessException)
                { }
            }
            //make 3 new lists, one for each date containing every NameDateObj
            var creationtimelist = new List <DateTime?>();
            var modtimelist      = new List <DateTime?>();
            var accesstimelist   = new List <DateTime?>();

            //populate the new seperated lists with the times from the combinedobject list (timelist)
            foreach (NameDateObj timeobject in timelist)
            {
                creationtimelist.Add((DateTime?)timeobject.Created);
                modtimelist.Add((DateTime?)timeobject.Modified);
                accesstimelist.Add((DateTime?)timeobject.Accessed);
            }
            //Make a new list of the lists we just made (Collection initializer)
            var threetimelists = new List <List <DateTime?> > {
                creationtimelist, modtimelist, accesstimelist
            };
            //Instantiate 3 new vars as a new class that processes the min and max date from the 3 lists we just made
            var cre = new DateMinMaxNewOld(creationtimelist);
            var mod = new DateMinMaxNewOld(modtimelist);
            var acc = new DateMinMaxNewOld(accesstimelist);

            ////Create 2 lists(min and max), containing the 3 min/max dates.
            //DateTime?[] minarray = { cre.minDate, mod.minDate, acc.minDate };
            //DateTime?[] maxarray = { cre.maxDate, mod.maxDate, acc.maxDate };
            ////Instantiate themin/themax as the new class that calculates the min and max date from the 3 dates above.
            //var themin = new DatesNewestOldest(new List<DateTime?>(minarray));
            //var themax = new DatesNewestOldest(new List<DateTime?>(maxarray));
            ////Keep track of the min/max indexes in this 1,2,3 format too.
            //int[] minindexesarray = { cre.minIndex, mod.minIndex, acc.minIndex };
            //int[] maxindexesarray = { cre.maxIndex, mod.maxIndex, acc.maxIndex };

            string filenameused = "";

            //var dateToUse = new DateTime?();

            //Decide which to use.

            if (gui.radioButton1Oldest)
            {
                //mode a: set ALL attributes to the oldest date of whichever attribute was oldest.
                //                    dateToUse = (DateTime?)minarray[themin.minIndex];
                //                    filenameused = timelist[minindexesarray[themin.minIndex]].Name;
                //                    thingtoreturn.Created = dateToUse;
                //                    thingtoreturn.Modified= dateToUse;
                //                    thingtoreturn.Accessed = dateToUse;

                //mode b: the more desirable mode:
                //set each attribute to OLDest date from EACH attribute
                thingtoreturn.Name     = "Mode 2: Three Different Filenames"; // note to self.
                thingtoreturn.Created  = cre.MinDate;
                thingtoreturn.Modified = mod.MinDate;
                thingtoreturn.Accessed = acc.MinDate;
            }
            //the above comments obviously can apply to newer mode also with a small edit.
            else if (gui.radioButton2Newest)
            {
                //set each attribute to NEWest date from EACH attribute
                thingtoreturn.Name     = "Mode 2: Three Different Filenames"; // note to self.
                thingtoreturn.Created  = cre.MaxDate;
                thingtoreturn.Modified = mod.MaxDate;
                thingtoreturn.Accessed = acc.MaxDate;
            }
            else if (gui.radioButton3Random)
            {
                //Mode A: (old) - removed the following 4 radio buttons
                //pick a subfile/dir at random, then pick an attribute(C,M,A) at random
                //    int randomindex = random.Next(0, timelist.Count);
                //    filenameused = timelist[randomindex].Name;

                //    if (radioButton1_setfromCreation.Checked)
                //        thingtoreturn.Created = (DateTime?)threetimelists[0][randomindex];
                //    if (radioButton2_setfromModified.Checked)
                //        thingtoreturn.Modified = (DateTime?)threetimelists[1][randomindex];
                //    if (radioButton3_setfromAccessed.Checked)
                //        thingtoreturn.Accessed = (DateTime?)threetimelists[2][randomindex];
                //    if (radioButton4_setfromRandom.Checked)
                //    {
                //        int cmarandomize = random.Next(0, 3);
                //        dateToUse = (DateTime?)threetimelists[cmarandomize][randomindex];

                //        if (cmarandomize == 0)
                //            thingtoreturn.Created = dateToUse;
                //        else if (cmarandomize == 1)
                //            thingtoreturn.Modified = dateToUse;
                //        else if (cmarandomize == 2)
                //            thingtoreturn.Accessed = dateToUse;
                //    }
                //Mode B: (current)
                //Pick a subfile/dir at random, copy all 3 attributes from it, to the return object.
                int randomindex = random.Next(0, timelist.Count);
                filenameused           = timelist[randomindex].Name;
                thingtoreturn.Created  = threetimelists[0][randomindex];
                thingtoreturn.Modified = threetimelists[1][randomindex];
                thingtoreturn.Accessed = threetimelists[2][randomindex];
            }
            //Set the thingtoReturn Name to what we just determined.
            thingtoreturn.Name = filenameused;
            return(thingtoreturn);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Check all the subdirs or subfiles. And decide the time. Called from above: DecideWhichTimeMode1() { radioGroupBox3_UseTimeFrom
        /// </summary>
        internal DateTime?DecideTimeFromSubdirOrSubfile(string path)
        {
            var dateToUse   = new DateTime?();
            var extractlist = new List <string>();

            if (gui.radioButton1_useTimefromFile)
            {
                extractlist = PopulateFileList(path);
            }
            else if (gui.radioButton2_useTimefromSubdir)
            {
                extractlist = PopulateDirList(path);
            }

            // if the resetList is blank due to no files actually existing then we have nothing to do, so stop here.
            if (extractlist.Count == 0)
            {
                return(null);
            }
            //for Any/Random attribute mode, decide which attribute and stick with it. (create before loop)
            // (and seed already exists so we dont regenerate seed through every recursive call to this.
            int randomNumber = random.Next(0, 3);
            var timelist     = new List <DateTime?>();

            //start iterating through
            foreach (string subitem in extractlist)
            {
                var looptempDate = new DateTime();
                try
                {
                    string fullpath     = Path.Combine(path, subitem);
                    string timelisttype = null; //this was made just in case.
                    if (gui.radioButton1_setfromCreated || gui.radioButton4_setfromRandom && randomNumber == 0)
                    {
                        timelisttype = "Created";
                        looptempDate = File.GetCreationTime(fullpath);
                    }
                    else if (gui.radioButton2_setfromModified || gui.radioButton4_setfromRandom && randomNumber == 1)
                    {
                        timelisttype = "Modified";
                        looptempDate = File.GetLastWriteTime(fullpath);
                    }
                    else if (gui.radioButton3_setfromAccessed || gui.radioButton4_setfromRandom && randomNumber == 2)
                    {
                        timelisttype = "Accessed";
                        looptempDate = File.GetLastAccessTime(fullpath);
                    }
                    timelist.Add(looptempDate);
                    string stopbotheringme = timelisttype; //remove "unused var" warning
                }
                //TODO: Check exceptions all over the code, and make sure we have enough
                catch (UnauthorizedAccessException)
                { } //wraps GetFileTimes in error handling
            }
            var minmax = new DateMinMaxNewOld(timelist);

            if (gui.radioButton1Oldest)
            {
                if (minmax.MinDate != null)
                {
                    dateToUse = minmax.MinDate; //explicit typecast from nullable
                }
            }
            else if (gui.radioButton2Newest)
            {
                if (minmax.MaxDate != null)
                {
                    dateToUse = minmax.MaxDate;
                }
            }
            else if (gui.radioButton3Random)
            {
                int randomFile = random.Next(0, minmax.Index);
                if (timelist[randomFile] != null)
                {
                    dateToUse = timelist[randomFile];
                }
            }
            return(dateToUse);
        }