From 826d788d9ffec7557b052a5f1b7029387337482b Mon Sep 17 00:00:00 2001 From: Admin Date: Wed, 18 Nov 2020 00:41:17 -0600 Subject: [PATCH] Introduced a concept of folder depth in the database code, and updated the Snapshot class to take advantage of it. Only working for a 'OneLevel' Folder Group type. --- .vs/DataOrganizer/v16/.suo | Bin 138240 -> 95744 bytes DataOrganizer/DB.cs | 39 +++++- DataOrganizer/FileViewer.Designer.cs | 97 ++++++++++++-- DataOrganizer/FileViewer.cs | 184 ++++++++++++++++----------- DataOrganizer/Snapshot.cs | 130 ++++++++++++++++--- 5 files changed, 337 insertions(+), 113 deletions(-) diff --git a/.vs/DataOrganizer/v16/.suo b/.vs/DataOrganizer/v16/.suo index a018daed40397f80642270bee0d19d8ba28f5b0e..d009185a03b12d9523ca60492277ac0b8d57887c 100644 GIT binary patch delta 3991 zcmZqJ!O`%Bb%Kp77Xt$W69WUozyJULGcYhPFfuSO$S^Q4$T2W5Xl-5?V9YqNpkxx0 z52M~>MJ9Q^4SrA3O7~u4lxJXIXt11I#Z<-kbn;iGdR8R{28N51E19cV*E29Me3<-` zS-D;uYOV+a0|Ofa1H=FS|NrwdFfa%+Ffd3mFfi~kFfed1Ffa%)FfgoTU|4>R80~C14A+c149Y}14AkU1H-P#3j?wzab---2;kwE$H2fapMim4at)V^ z3dk=Yypn-|VHE=d!)j>Mg2Z5W@*XZ1f$L}zlLdlRG+-(&fD#E*!DR*phARvV3|FCi zkebOc!6vLA#=nV)*_(Jg7&%~}GFebaWpWAcmqF{Yo=NsPslk6nxyhyS z{*zsJ3|K*hQ_|+2qSefdijzwv>sdjifXL*Zj@~?mNaX=YKjY!arB41lpzI1V5`;m* zY)2Rv7&4hA|8nxW@DM8P{JrZ zxm2Nka*2Y>WC6t#M#;^kiuH_aW(*7rdVG_ADg814<=8S%xe5&*P;kKTz(s{w38VPr zQnPwSvB_V}>KR2QmzvkJfugc+-R57`UQ9d=3=9mQ;tc9-jmZ^u7dUK?!f0}d{RJKi z1_lO@E~q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+I zMDF~J1u2Zv@)!#k|4l9om7o3~no(hUSUzI_@Etn>M@-|}!_3IcI7^dq-FzQdAlg-E_uv^l=E>2}{ z-m{F8|94N`Yd^g~n=x+sx*|rQ45lmO zGO}?(EoWeu{yvM*9K+^mxr_?aW%3vW1VA+sD80hnRFlrA28tOjf!xI6lGGw3=gJ2& z8t}engmki)8CV#KCx7&^n(kiASh@XK7NZx_^bc8#aF0*^7-C5!<9ALj^{Qqp0NFO# z)=SU8(m==1yv8jtr#Kblb`%V1>Vul&pmN(Ku_V#IC_Qm{NGW3uMj%XHkfa8V7yt@} BH!1)C diff --git a/DataOrganizer/DB.cs b/DataOrganizer/DB.cs index 29ecb0c..7117f8f 100644 --- a/DataOrganizer/DB.cs +++ b/DataOrganizer/DB.cs @@ -86,12 +86,29 @@ namespace DataOrganizer while(reader.Read()) { + var type = OrganizationType.Custom; + var patternId = Convert.ToInt32(reader["pattern_id"]); + + switch (patternId) + { + case 1: + type = OrganizationType.SingleLevel; + break; + case 2: + type = OrganizationType.Flat; + break; + default: + type = OrganizationType.Custom; + break; + } + profile.FolderGroups.Add(new FolderGroup { Name = reader["name"].ToString(), Id = Convert.ToInt32(reader["root_id"]), Path = reader["path"].ToString(), - PatternId = Convert.ToInt32(reader["pattern_id"]) + PatternId = Convert.ToInt32(reader["pattern_id"]), + Type = type }); } @@ -365,19 +382,18 @@ namespace DataOrganizer while(reader.Read()) { - var fullPath = string.Format("{0}{1}", reader["path"], reader["full_path"]); - files.Add(new FileSnapshot { Id = Convert.ToInt32(reader["file_id"]), - FullPath = fullPath, + PathNoRoot = reader["full_path"].ToString(), Name = reader["name"].ToString(), ProfileId = profileId, RootFolderId = folderGroupId, Size = Convert.ToInt64(reader["size"]), CreationDate = Convert.ToDateTime(reader["date_created"]), ModifiedDate = Convert.ToDateTime(reader["date_modified"]), - PatternId = Convert.ToInt32(reader["pattern_override_id"]) + PatternId = Convert.ToInt32(reader["pattern_override_id"]), + RootPath = reader["path"].ToString(), }); } } @@ -401,7 +417,7 @@ namespace DataOrganizer { foreach (var file in files) { - command.Parameters.AddWithValue("FullPath", file.FullPath.Remove(0, rootLength)); + command.Parameters.AddWithValue("FullPath", file.PathNoRoot.Remove(0, rootLength)); command.Parameters.AddWithValue("Name", file.Name); command.Parameters.AddWithValue("Size", file.Size); command.Parameters.AddWithValue("DateCreated", file.CreationDate); @@ -435,6 +451,7 @@ namespace DataOrganizer public int Id; public string Name; public string Path; + public OrganizationType Type; public int PatternId; } @@ -449,7 +466,7 @@ namespace DataOrganizer public struct FileSnapshot { public int Id; - public string FullPath; + public string PathNoRoot; public string Name; public int ProfileId; public int PatternId; @@ -457,6 +474,14 @@ namespace DataOrganizer public DateTime CreationDate; public DateTime ModifiedDate; public long Size; + public string RootPath; + } + + public enum OrganizationType + { + Flat = 0, + SingleLevel = 1, + Custom = 2 } } } diff --git a/DataOrganizer/FileViewer.Designer.cs b/DataOrganizer/FileViewer.Designer.cs index 2ca4b6e..73a86cd 100644 --- a/DataOrganizer/FileViewer.Designer.cs +++ b/DataOrganizer/FileViewer.Designer.cs @@ -45,11 +45,18 @@ this.label2 = new System.Windows.Forms.Label(); this.StatusStrip = new System.Windows.Forms.StatusStrip(); this.StatusLabel = new System.Windows.Forms.ToolStripStatusLabel(); + this.FileInfoGroupBox = new System.Windows.Forms.GroupBox(); + this.FileDetailsNameLabel = new System.Windows.Forms.Label(); + this.FileDetailsFullPathLabel = new System.Windows.Forms.Label(); + this.label6 = new System.Windows.Forms.Label(); + this.label7 = new System.Windows.Forms.Label(); + this.label8 = new System.Windows.Forms.Label(); this.tableLayoutPanel1.SuspendLayout(); this.PanelOne.SuspendLayout(); this.SelectedFolderPanel.SuspendLayout(); this.ActiveFolderGroupBox.SuspendLayout(); this.StatusStrip.SuspendLayout(); + this.FileInfoGroupBox.SuspendLayout(); this.SuspendLayout(); // // MainMenu @@ -58,7 +65,7 @@ this.MainMenu.ImageScalingSize = new System.Drawing.Size(24, 24); this.MainMenu.Location = new System.Drawing.Point(0, 0); this.MainMenu.Name = "MainMenu"; - this.MainMenu.Size = new System.Drawing.Size(1472, 24); + this.MainMenu.Size = new System.Drawing.Size(1472, 36); this.MainMenu.TabIndex = 0; this.MainMenu.Text = "menuStrip1"; // @@ -73,34 +80,36 @@ this.tableLayoutPanel1.Controls.Add(this.PrimaryFilesTreeView, 0, 1); this.tableLayoutPanel1.Controls.Add(this.PanelOne, 0, 0); this.tableLayoutPanel1.Controls.Add(this.SelectedFolderPanel, 2, 0); + this.tableLayoutPanel1.Controls.Add(this.FileInfoGroupBox, 1, 0); this.tableLayoutPanel1.Dock = System.Windows.Forms.DockStyle.Fill; - this.tableLayoutPanel1.Location = new System.Drawing.Point(0, 24); + this.tableLayoutPanel1.Location = new System.Drawing.Point(0, 36); this.tableLayoutPanel1.Name = "tableLayoutPanel1"; this.tableLayoutPanel1.RowCount = 3; this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 33F)); this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 34F)); this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 33F)); - this.tableLayoutPanel1.Size = new System.Drawing.Size(1472, 882); + this.tableLayoutPanel1.Size = new System.Drawing.Size(1472, 870); this.tableLayoutPanel1.TabIndex = 1; // // SecondaryTreeView // this.SecondaryTreeView.Dock = System.Windows.Forms.DockStyle.Fill; - this.SecondaryTreeView.Location = new System.Drawing.Point(739, 294); + this.SecondaryTreeView.Location = new System.Drawing.Point(739, 290); this.SecondaryTreeView.Name = "SecondaryTreeView"; this.tableLayoutPanel1.SetRowSpan(this.SecondaryTreeView, 2); - this.SecondaryTreeView.Size = new System.Drawing.Size(362, 585); + this.SecondaryTreeView.ShowNodeToolTips = true; + this.SecondaryTreeView.Size = new System.Drawing.Size(362, 577); this.SecondaryTreeView.TabIndex = 2; // // PrimaryFilesTreeView // this.PrimaryFilesTreeView.Dock = System.Windows.Forms.DockStyle.Fill; this.PrimaryFilesTreeView.FullRowSelect = true; - this.PrimaryFilesTreeView.Location = new System.Drawing.Point(3, 294); + this.PrimaryFilesTreeView.Location = new System.Drawing.Point(3, 290); this.PrimaryFilesTreeView.Name = "PrimaryFilesTreeView"; this.tableLayoutPanel1.SetRowSpan(this.PrimaryFilesTreeView, 2); this.PrimaryFilesTreeView.ShowNodeToolTips = true; - this.PrimaryFilesTreeView.Size = new System.Drawing.Size(362, 585); + this.PrimaryFilesTreeView.Size = new System.Drawing.Size(362, 577); this.PrimaryFilesTreeView.TabIndex = 0; // // PanelOne @@ -113,7 +122,7 @@ this.PanelOne.Dock = System.Windows.Forms.DockStyle.Fill; this.PanelOne.Location = new System.Drawing.Point(3, 3); this.PanelOne.Name = "PanelOne"; - this.PanelOne.Size = new System.Drawing.Size(362, 285); + this.PanelOne.Size = new System.Drawing.Size(362, 281); this.PanelOne.TabIndex = 1; // // ScanRootFolderButton @@ -169,7 +178,7 @@ this.SelectedFolderPanel.Dock = System.Windows.Forms.DockStyle.Fill; this.SelectedFolderPanel.Location = new System.Drawing.Point(739, 3); this.SelectedFolderPanel.Name = "SelectedFolderPanel"; - this.SelectedFolderPanel.Size = new System.Drawing.Size(730, 285); + this.SelectedFolderPanel.Size = new System.Drawing.Size(730, 281); this.SelectedFolderPanel.TabIndex = 3; // // ActiveFolderGroupBox @@ -180,7 +189,7 @@ this.ActiveFolderGroupBox.Dock = System.Windows.Forms.DockStyle.Fill; this.ActiveFolderGroupBox.Location = new System.Drawing.Point(0, 0); this.ActiveFolderGroupBox.Name = "ActiveFolderGroupBox"; - this.ActiveFolderGroupBox.Size = new System.Drawing.Size(730, 285); + this.ActiveFolderGroupBox.Size = new System.Drawing.Size(730, 281); this.ActiveFolderGroupBox.TabIndex = 0; this.ActiveFolderGroupBox.TabStop = false; this.ActiveFolderGroupBox.Text = "Folder Options"; @@ -229,6 +238,66 @@ this.StatusLabel.Size = new System.Drawing.Size(179, 25); this.StatusLabel.Text = "toolStripStatusLabel1"; // + // FileInfoGroupBox + // + this.FileInfoGroupBox.Controls.Add(this.label8); + this.FileInfoGroupBox.Controls.Add(this.label7); + this.FileInfoGroupBox.Controls.Add(this.label6); + this.FileInfoGroupBox.Controls.Add(this.FileDetailsFullPathLabel); + this.FileInfoGroupBox.Controls.Add(this.FileDetailsNameLabel); + this.FileInfoGroupBox.Dock = System.Windows.Forms.DockStyle.Fill; + this.FileInfoGroupBox.Location = new System.Drawing.Point(371, 3); + this.FileInfoGroupBox.Name = "FileInfoGroupBox"; + this.FileInfoGroupBox.Size = new System.Drawing.Size(362, 281); + this.FileInfoGroupBox.TabIndex = 4; + this.FileInfoGroupBox.TabStop = false; + this.FileInfoGroupBox.Text = "File Details"; + // + // FileDetailsNameLabel + // + this.FileDetailsNameLabel.AutoSize = true; + this.FileDetailsNameLabel.Location = new System.Drawing.Point(6, 20); + this.FileDetailsNameLabel.Name = "FileDetailsNameLabel"; + this.FileDetailsNameLabel.Size = new System.Drawing.Size(55, 20); + this.FileDetailsNameLabel.TabIndex = 0; + this.FileDetailsNameLabel.Text = "Name:"; + // + // FileDetailsFullPathLabel + // + this.FileDetailsFullPathLabel.AutoSize = true; + this.FileDetailsFullPathLabel.Location = new System.Drawing.Point(6, 60); + this.FileDetailsFullPathLabel.Name = "FileDetailsFullPathLabel"; + this.FileDetailsFullPathLabel.Size = new System.Drawing.Size(75, 20); + this.FileDetailsFullPathLabel.TabIndex = 1; + this.FileDetailsFullPathLabel.Text = "Full Path:"; + // + // label6 + // + this.label6.AutoSize = true; + this.label6.Location = new System.Drawing.Point(6, 100); + this.label6.Name = "label6"; + this.label6.Size = new System.Drawing.Size(55, 20); + this.label6.TabIndex = 2; + this.label6.Text = "Name:"; + // + // label7 + // + this.label7.AutoSize = true; + this.label7.Location = new System.Drawing.Point(6, 140); + this.label7.Name = "label7"; + this.label7.Size = new System.Drawing.Size(55, 20); + this.label7.TabIndex = 3; + this.label7.Text = "Name:"; + // + // label8 + // + this.label8.AutoSize = true; + this.label8.Location = new System.Drawing.Point(6, 180); + this.label8.Name = "label8"; + this.label8.Size = new System.Drawing.Size(55, 20); + this.label8.TabIndex = 4; + this.label8.Text = "Name:"; + // // FileViewer // this.AutoScaleDimensions = new System.Drawing.SizeF(9F, 20F); @@ -249,6 +318,8 @@ this.ActiveFolderGroupBox.PerformLayout(); this.StatusStrip.ResumeLayout(false); this.StatusStrip.PerformLayout(); + this.FileInfoGroupBox.ResumeLayout(false); + this.FileInfoGroupBox.PerformLayout(); this.ResumeLayout(false); this.PerformLayout(); @@ -273,5 +344,11 @@ private System.Windows.Forms.Button ScanRootFolderButton; private System.Windows.Forms.StatusStrip StatusStrip; private System.Windows.Forms.ToolStripStatusLabel StatusLabel; + private System.Windows.Forms.GroupBox FileInfoGroupBox; + private System.Windows.Forms.Label label8; + private System.Windows.Forms.Label label7; + private System.Windows.Forms.Label label6; + private System.Windows.Forms.Label FileDetailsFullPathLabel; + private System.Windows.Forms.Label FileDetailsNameLabel; } } \ No newline at end of file diff --git a/DataOrganizer/FileViewer.cs b/DataOrganizer/FileViewer.cs index 9be3da7..4306e82 100644 --- a/DataOrganizer/FileViewer.cs +++ b/DataOrganizer/FileViewer.cs @@ -18,6 +18,8 @@ namespace DataOrganizer private Dictionary FolderGroups { get; } = new Dictionary(); private string CurrentNamingPattern { get; set; } = string.Empty; private ContextMenu PrimaryTreeContextMenu { get; set; } = new ContextMenu(); //Copy to default new content location, set new naming rules etc.. + private Snapshot ActiveSnapshotForPrimaryTree { get; set; } + private Dictionary PrimaryTreeNodes { get; set; } = new Dictionary(); public FileViewer() { @@ -34,10 +36,22 @@ namespace DataOrganizer ProfileComboBox.SelectedIndexChanged += ProfileComboBox_SelectedIndexChanged; RootFolderComboBox.SelectedIndexChanged += SelectedFolderComboBox_SelectedIndexChanged; + PrimaryFilesTreeView.NodeMouseClick += PrimaryFilesTreeView_NodeMouseClick; ProfileComboBox.SelectedIndex = 0; } + private void PrimaryFilesTreeView_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e) + { + if(ActiveSnapshotForPrimaryTree.Files.ContainsKey(e.Node.Name)) + { + var file = ActiveSnapshotForPrimaryTree.Files[e.Node.Name]; + + FileDetailsNameLabel.Text = $"Name: {file.Name}"; + FileDetailsFullPathLabel.Text = $"Full Path: {file.PathNoRoot}"; + } + } + private void SelectedFolderComboBox_SelectedIndexChanged(object sender, EventArgs e) { var db = new DB(); @@ -47,14 +61,25 @@ namespace DataOrganizer var profile = Profiles[ProfileComboBox.Text]; PrimaryFilesTreeView.Nodes.Clear(); + PrimaryTreeNodes.Clear(); var files = db.GetFilesForProfileByFolderGroup(profile.Id, folderGroup.Id); - var snapshot = new Snapshot(files, profile, folderGroup); + ActiveSnapshotForPrimaryTree = new Snapshot(files, profile, folderGroup); - foreach(var folder in snapshot.Root.Folders) + foreach(var folder in ActiveSnapshotForPrimaryTree.Root.Folders) { PrimaryFilesTreeView.Nodes.Add(GetNodeFromSnapshot(folder)); } + + //var count = 1; + + //foreach(var file in ActiveSnapshotForPrimaryTree.FileDuplicates) + //{ + // PrimaryTreeNodes[file.Value.File.PathNoRoot].Text = $"{count} ) {file.Value.File.Name}"; + // PrimaryTreeNodes[file.Value.Duplicates[0].PathNoRoot].Text = $"{count} ) {file.Value.Duplicates[0].Name}"; + + // count++; + //} } private TreeNode GetNodeFromSnapshot(KeyValuePair folder) @@ -65,6 +90,7 @@ namespace DataOrganizer foreach(var f in folder.Value.Folders) { var folderNode = GetNodeFromSnapshot(f); + //PrimaryTreeNodes.Add(f.Key, folderNode); node.Nodes.Add(folderNode); if (folderNode.ForeColor == Color.Orange) { @@ -76,13 +102,17 @@ namespace DataOrganizer foreach(var file in folder.Value.Files) { var fileNode = new TreeNode(file.Value.Name) { Name = file.Key, ToolTipText = file.Key }; + //PrimaryTreeNodes.Add(file.Key, fileNode); if (!regex.IsMatch(file.Value.Name)) fileNode.ForeColor = Color.Orange; node.Nodes.Add(fileNode); - node.ForeColor = fileNode.ForeColor; + if(node.ForeColor != Color.Orange) node.ForeColor = fileNode.ForeColor; + PrimaryTreeNodes.Add(file.Key, fileNode); } + PrimaryTreeNodes.Add(folder.Key, node); + return node; } @@ -91,6 +121,7 @@ namespace DataOrganizer var profile = Profiles[ProfileComboBox.Text]; RootFolderComboBox.Items.Clear(); + PrimaryTreeNodes.Clear(); FolderGroups.Clear(); if (profile.FolderGroups.Count > 0) @@ -132,83 +163,84 @@ namespace DataOrganizer private void ScanRootFolderButton_Click(object sender, EventArgs e) { - var folder = FolderGroups[RootFolderComboBox.Text]; - //var db = new DB(); - //var pattern = db.GetNamingPatternById(folder.PatternId); - //var regex = new Regex(pattern); - //PrimaryFilesTreeView.Nodes.Clear(); + var folderGroup = GetActiveFolderGroup(); + var rootDepth = folderGroup.Path.Split(new char[] { Path.DirectorySeparatorChar }).Length; + var newFiles = new List(); + var rootFolder = new DirectoryInfo(folderGroup.Path); - //var dirs = Directory.EnumerateDirectories(folder.Path); + foreach (var dir in rootFolder.EnumerateDirectories()) newFiles.AddRange(ScanDirectory(dir)); - //foreach (var dir in dirs) - //{ - // var dirInfo = new DirectoryInfo(dir); - // PrimaryFilesTreeView.Nodes.Add(CreateNode(dirInfo, pattern)); - //} - - //foreach(var file in Directory.EnumerateFiles(folder.Path)) - //{ - // var fileInfo = new FileInfo(file); - // var fileNode = new TreeNode(fileInfo.Name) { Name = fileInfo.FullName }; - - // if (!regex.IsMatch(fileInfo.Name)) - // { - // fileNode.ForeColor = Color.DarkOrange; - // } - - // PrimaryFilesTreeView.Nodes.Add(fileNode); - //} - - WriteFilesToDb(folder.Path); - } - - private TreeNode CreateNode(DirectoryInfo dir, string pattern) - { - var folder = FolderGroups[RootFolderComboBox.Text]; - var profile = Profiles[ProfileComboBox.Text]; - var namingViolation = false; - var node = new TreeNode(dir.Name) { Name = dir.FullName }; - var regex = new Regex(pattern); - var db = new DB(); - - var files = Directory.EnumerateFiles(dir.FullName); - - foreach (var file in files) + foreach (var file in rootFolder.EnumerateFiles()) { - var fileInfo = new FileInfo(file); - var fileNode = new TreeNode(fileInfo.Name) { Name = fileInfo.FullName }; - //var match = regex.Match(fileInfo.Name); - //var stringBuilder = new StringBuilder(); - - //if (!match.Groups[1].Success) - // stringBuilder.Append($"Failed to match {regex.GroupNameFromNumber(1)} ");//match.Groups[0].Name}"); - //if (!match.Groups[2].Success) - // stringBuilder.Append($"Failed to match {regex.GroupNameFromNumber(2)} "); - - if (!regex.IsMatch(fileInfo.Name)) + if (!ActiveSnapshotForPrimaryTree.Files.ContainsKey(file.FullName)) { - namingViolation = true; - fileNode.ForeColor = Color.DarkOrange; + newFiles.Add(file); + } + } + + foreach (var newFile in newFiles) + { + var folders = newFile.DirectoryName.Split(new char[] { Path.DirectorySeparatorChar }); + var currentPath = folderGroup.Path; + + for(var i = rootDepth; i < folders.Length; i++) + { + var previousPath = currentPath; + currentPath = Path.Combine(currentPath, folders[i]); + + if(!PrimaryTreeNodes.ContainsKey(currentPath)) + { + var node = new TreeNode(folders[i]) { ForeColor = Color.Blue, ToolTipText = currentPath }; + PrimaryTreeNodes[currentPath] = node; + if (previousPath == folderGroup.Path) + { + PrimaryFilesTreeView.Nodes.Add(node); + } + else + { + PrimaryTreeNodes[previousPath].Nodes.Add(node); + } + } } - node.Nodes.Add(fileNode); - db.InsertNewFileRecord(GetFileSnapshots(dir.FullName), profile.Id, folder.Id, folder.Path); + PrimaryTreeNodes[currentPath].Nodes.Add(new TreeNode(newFile.Name) { ForeColor = Color.Blue }); } - - if (namingViolation) node.ForeColor = Color.DarkOrange; - - var dirs = Directory.EnumerateDirectories(dir.FullName); - - foreach(var f in dirs) - { - var child = CreateNode(new DirectoryInfo(f), pattern); - node.ForeColor = child.ForeColor; - node.Nodes.Add(child); - } - - return node; } + private List ScanDirectory(DirectoryInfo dir) + { + var newFiles = new List(); + + foreach (var d in dir.EnumerateDirectories()) + { + UpdateStatusStrip($"Processing {d.FullName}"); + newFiles.AddRange(ScanDirectory(d)); + } + + foreach(var file in dir.EnumerateFiles()) + { + if (!ActiveSnapshotForPrimaryTree.Files.ContainsKey(file.FullName)) + { + newFiles.Add(file); + } + else + { + //TODO: check for modifications + } + } + + foreach(var file in ActiveSnapshotForPrimaryTree.Files) + { + UpdateStatusStrip($"Checking for the existance of {file.Value.Name}"); + + if(File.Exists(file.Key)) + { + //TODO: mark this file as deleted. Also scan the folders. + } + } + + return newFiles; + } private void WriteFilesToDb(string root) { var dir = new DirectoryInfo(root); @@ -241,8 +273,8 @@ namespace DataOrganizer private List GetFileSnapshots(string path) { var dir = new DirectoryInfo(path); - var profile = GetCurrentProfile(); - var folderGroup = GetCurrentFolderGroup(); + var profile = GetActiveProfile(); + var folderGroup = GetActiveFolderGroup(); var snapshots = new List(); @@ -250,7 +282,7 @@ namespace DataOrganizer { snapshots.Add(new DB.FileSnapshot { - FullPath = file.FullName, + PathNoRoot = file.FullName, Name = file.Name, CreationDate = file.CreationTimeUtc, ModifiedDate = file.LastWriteTimeUtc, @@ -264,12 +296,12 @@ namespace DataOrganizer return snapshots; } - private DB.Profile GetCurrentProfile() + private DB.Profile GetActiveProfile() { return Profiles[ProfileComboBox.Text]; } - private DB.FolderGroup GetCurrentFolderGroup() + private DB.FolderGroup GetActiveFolderGroup() { return FolderGroups[RootFolderComboBox.Text]; } diff --git a/DataOrganizer/Snapshot.cs b/DataOrganizer/Snapshot.cs index e46fb05..88413b4 100644 --- a/DataOrganizer/Snapshot.cs +++ b/DataOrganizer/Snapshot.cs @@ -4,6 +4,7 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; +using System.Security.Cryptography.X509Certificates; namespace DataOrganizer { @@ -12,42 +13,112 @@ namespace DataOrganizer public Folder Root { get; private set; } public Dictionary Files { get; private set; } = new Dictionary(); public Dictionary Directories { get; private set; } = new Dictionary(); + public Dictionary FileDuplicates { get; private set; } = new Dictionary(); + //Contains files associated with particular, first level folders. + //So if the root folder group is X:\Anime and we have a folder path of x:\Anime\Death Note, then indexing this dictonary + //with the text "Death Note" would return all files in that folder. + public Dictionary> OrganizationFolders { get; private set; } = new Dictionary>(); public Snapshot(List fileRecords, DB.Profile profile, DB.FolderGroup folderGroup) { Root = new Folder(folderGroup.Name, folderGroup.Path); Directories.Add(Root.FullPath, Root); - var rootDepth = folderGroup.Path.Split(new char[] { Path.DirectorySeparatorChar }).Length; + if(folderGroup.Type == DB.OrganizationType.SingleLevel) + ProcessFilesAsOneLevel(fileRecords, folderGroup); - foreach (var file in fileRecords) + //var rootDepth = folderGroup.Path.Split(new char[] { Path.DirectorySeparatorChar }).Length; + + //foreach (var file in fileRecords) + //{ + // var folders = file.PathNoRoot.Split(new char[] { Path.DirectorySeparatorChar }); + // var currentPath = folderGroup.Path; + + // if (folders.Length == rootDepth + 1) + // { + // Root.AddFile(file); + // Files.Add(file.PathNoRoot, file); + // continue; + // } + + // for (var i = rootDepth; i < folders.Length - 1; i++) + // { + // string previousPath = currentPath; + // currentPath = Path.Combine(currentPath, folders[i]); + + // if (!Directories.ContainsKey(currentPath)) + // { + // var newFolder = new Folder(folders[i], currentPath); + // Directories[previousPath].AddDirectory(newFolder); + // Directories.Add(currentPath, newFolder); + // } + // } + + // Directories[currentPath].AddFile(file); + + // var duplicates = Files.Values.Where(x => x.Size == file.Size && x.Id != file.Id); + + // Files.Add(file.PathNoRoot, file); + + // foreach (var duplicate in duplicates) + // { + // FileDuplicates.Add(file.PathNoRoot, new Duplicate() { File = file, Duplicates = new List { duplicate } }); + // } + //} + } + + private void ProcessFilesAsOneLevel(List files, DB.FolderGroup folderGroup) + { + Folder firstLevelFolder = null; + + foreach (var file in files) { - var folders = file.FullPath.Split(new char[] { Path.DirectorySeparatorChar }); - var currentPath = folderGroup.Path; + var folders = file.PathNoRoot.Split(new char[] { Path.DirectorySeparatorChar }, StringSplitOptions.RemoveEmptyEntries); + var orgFolder = Path.Combine(folderGroup.Path, folders[0]); + var currentPath = Path.Combine(folderGroup.Path, folders[0]); - if (folders.Length == rootDepth + 1) + if(firstLevelFolder == null || firstLevelFolder.Name != folders[0]) { - Root.AddFile(file); - Files.Add(file.FullPath, file); - continue; + firstLevelFolder = new Folder(folders[0], Path.Combine(folderGroup.Path, folders[0])); + Directories.Add(orgFolder, firstLevelFolder); + OrganizationFolders.Add(orgFolder, new List()); + Root.AddDirectory(firstLevelFolder); } - for (var i = rootDepth; i < folders.Length - 1; i++) + for(var i = 1; i < folders.Length - 1; i++) { - string previousPath = currentPath; + var parentFolder = currentPath; currentPath = Path.Combine(currentPath, folders[i]); - - if (!Directories.ContainsKey(currentPath)) + if(!Directories.ContainsKey(currentPath)) { var newFolder = new Folder(folders[i], currentPath); - Directories[previousPath].AddDirectory(newFolder); Directories.Add(currentPath, newFolder); + Directories[parentFolder].AddDirectory(newFolder); } } + var duplicates = OrganizationFolders[orgFolder].Where(x => x.Size == file.Size && x.Id != file.Id); + + foreach(var dup in duplicates) + { + if(FileDuplicates.ContainsKey(file.PathNoRoot)) + { + FileDuplicates[file.PathNoRoot].Duplicates.Add(dup); + } + else FileDuplicates.Add(file.PathNoRoot, new Duplicate() { File = file, Duplicates = new List() { dup } }); + } + + OrganizationFolders[orgFolder].Add(file); Directories[currentPath].AddFile(file); + Files.Add(file.PathNoRoot, file); } } + + public struct Duplicate + { + public DB.FileSnapshot File; + public List Duplicates; + } } public class Folder @@ -56,7 +127,7 @@ namespace DataOrganizer public string FullPath { get; private set; } public Dictionary Files { get; private set; } = new Dictionary(); public Dictionary Folders { get; private set; } = new Dictionary(); - public List Duplicates { get; private set; } + public Folder OrganizationalFolder { get; private set; } public Folder(string name, string fullPath) { @@ -66,7 +137,16 @@ namespace DataOrganizer public void AddFile(DB.FileSnapshot file) { - Files.Add(file.FullPath, file); + //var possibleDuplicates = Files.Values.Where(x => x.Size == file.Size); + + Files.Add(file.PathNoRoot, file); + + //foreach (var f in possibleDuplicates) + //{ + // if (f.Id == file.Id) continue; + + // Duplicates.Add(new Duplicate() { FileA = file, FileB = f }); + //} } public void AddDirectory(Folder folder) @@ -74,10 +154,20 @@ namespace DataOrganizer Folders.Add(folder.FullPath, folder); } - public struct Duplicate - { - DB.FileSnapshot FileA; - DB.FileSnapshot FileB; - } + //public struct Duplicate + //{ + // public DB.FileSnapshot FileA; + // public DB.FileSnapshot FileB; + //} + } + + public class FileSnapshot + { + + } + + public class SnapshotComparsion + { + } }