internal bool ObtainParseSpecifiedTimeGUIBoxes() { thingtoreturn = new NameDateObj(); if (gui.radioGroupBox1SpecifyTime) { thingtoreturn.Name = gui.PathName; var specifiedDate = DateTime.Parse(gui.dateTimePickerDate.Date.ToString("d") + " " + gui.dateTimePickerTime.Hour + ":" + gui.dateTimePickerTime.Minute + ":" + gui.dateTimePickerTime.Second); thingtoreturn.Created = specifiedDate; thingtoreturn.Modified = specifiedDate; thingtoreturn.Accessed = specifiedDate; return(true); } if (!gui.radioGroupBox2CurrentSelect) { return(false); } thingtoreturn.Name = gui.PathName; thingtoreturn.Created = DateTime.Parse(gui.Created); thingtoreturn.Modified = DateTime.Parse(gui.Modified); thingtoreturn.Accessed = DateTime.Parse(gui.Accessed); return(true); }
/// <summary> STATIC. /// Set the date/time for a single file/directory (This works on files and directories) /// Go through the list, skipping H,S,R files, and add all the file+date objects to the Confirmation List /// (Only adds to the confirm list, Form 2 (Confirm) will actually write changes). /// </summary> /// <param name="filePath">Full path to the file/directory</param> /// <param name="fileTime">Date/Time to set the file/directory</param> /// <param name="isDirectory">Is this a directory???</param> internal void AddONEFiletoConfirmList(string filePath, DateTime fileTime, bool isDirectory) { SkipOrAddFile(filePath, isDirectory); //Make a NameDateObj out of 1 filename; Check all 3 date properties, and only write a time on match var currentobject = new NameDateObj { Name = filePath, FileOrDirType = SharedHelper.Bool2Int(isDirectory) }; // Set the Creation date/time if selected if (checkboxes.C) { currentobject.Created = fileTime; } // Set the Modified date/time is selected if (checkboxes.M) { currentobject.Modified = fileTime; } // Set the last access time if selected if (checkboxes.A) { currentobject.Accessed = fileTime; } FilestoConfirmList.Add(currentobject); }
/// <summary> /// Mode 2: Recursive. /// </summary> /// <param name="directoryPath">Path to start in.</param> internal void RecurseSubDirectoryMode2(string directoryPath) { NameDateObj timeInside = DecideWhichTimeMode2(directoryPath); SkipOrAddFile(directoryPath, true); //Make a NameDateObj out of 1 filename; writes each time time to the date attribute that was radiobutton selected. var currentobject = new NameDateObjListViewVMdl(timeInside) { Name = directoryPath, FileOrDirType = 1 }; StoreDateByCMACheckboxes(currentobject); var subFile = new NameDateObj(currentobject.Converter()); FilestoConfirmList.Add(subFile); try { foreach (string subfolder in Directory.GetDirectories(directoryPath)) { RecurseSubDirectoryMode2(Path.Combine(directoryPath, subfolder)); } } catch (UnauthorizedAccessException) { } catch (DirectoryNotFoundException) { } }
/// <summary> /// Mode 3: Process One directory, Process 2nd Dir. with recursive sub-directory support. Calls SetFileDateTime() /// (Only adds to the confirm list, Form 2 will actually write changes). /// </summary> /// <param name="targetPath">Full path to the targetPath directory</param> /// <param name="comparePath">Full path to the comparePath directory</param> /// req, checkBox_Recurse.Checked, checkBoxShouldFiles.Checked internal void RecurseSubDirectoryMode3(string targetPath, string comparePath) { //no point continuing if we have nothing matching to compare to. if (!Directory.Exists(comparePath)) { return; } if (!comparePath.EndsWith(SharedHelper.SeperatorString)) { comparePath += SharedHelper.Seperator; } // Take a snapshot of the paths of the file system. Makes an IEnumerable. IEnumerable <string> destFileList = GetFilesAndDirsSafely(targetPath, "*", true); IEnumerable <string> srcFileList = GetFilesAndDirsSafely(comparePath, "*", true); // Find the common files. It produces a sequence and doesn't execute until the foreach statement. IEnumerable <string> queryCommonFiles = srcFileList.Intersect(destFileList, SharedHelper.explorerStringEqualityComparer(targetPath, comparePath)); foreach (string f in queryCommonFiles) { NameDateQuick srcfiletime = GetCmaTimesFromFilesystem(f); string nameChanged = f.Replace(comparePath, targetPath); bool isDirectory = Directory.Exists(nameChanged); SkipOrAddFile(nameChanged, isDirectory); var currentobject = new NameDateObjListViewVMdl(srcfiletime) { Name = nameChanged, FileOrDirType = SharedHelper.Bool2Int(isDirectory) }; //If Checkbox is selected: writes each time time to the date attribute that was radiobutton selected. StoreDateByCMACheckboxes(currentobject); var item = new NameDateObj(currentobject.Converter()); FilestoConfirmList.Add(item); } }
/// <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); }