Updated the FileViewer form to populate the listing of files and performs pattern matching based on the folder group's pattern regex.

This commit is contained in:
Admin
2020-11-10 23:20:41 -06:00
parent 8abded6731
commit cb0737954a
5 changed files with 176 additions and 5 deletions
Binary file not shown.
+41
View File
@@ -306,6 +306,47 @@ namespace DataOrganizer
return conventions;
}
public string GetNamingPatternById(int id)
{
if (id == 0) return string.Empty;
var query = @"SELECT regex FROM main.naming_pattern WHERE pattern_id = @ID";
using (var connection = new SqliteConnection(ConnectionString))
{
using (var command = new SqliteCommand(query, connection))
{
command.Parameters.AddWithValue("ID", id);
connection.Open();
var reader = command.ExecuteReader();
reader.Read();
return reader["regex"].ToString();
}
}
}
public bool UpdateNamingPatterById(int id, string newPattern)
{
var query = @"UPDATE main.naming_pattern SET regex = @pattern WHERE pattern_id = @ID";
using (var connection = new SqliteConnection(ConnectionString))
{
using (var command = new SqliteCommand(query, connection))
{
command.Parameters.AddWithValue("pattern", newPattern);
command.Parameters.AddWithValue("ID", id);
connection.Open();
command.ExecuteNonQuery();
}
}
return true;
}
public struct Profile
{
public string Name;
+39
View File
@@ -42,10 +42,14 @@
this.label3 = new System.Windows.Forms.Label();
this.SelectedFolderComboBox = new System.Windows.Forms.ComboBox();
this.RootFolderComboBox = new System.Windows.Forms.ComboBox();
this.ScanRootFolderButton = new System.Windows.Forms.Button();
this.StatusStrip = new System.Windows.Forms.StatusStrip();
this.StatusLabel = new System.Windows.Forms.ToolStripStatusLabel();
this.tableLayoutPanel1.SuspendLayout();
this.PanelOne.SuspendLayout();
this.SelectedFolderPanel.SuspendLayout();
this.ActiveFolderGroupBox.SuspendLayout();
this.StatusStrip.SuspendLayout();
this.SuspendLayout();
//
// MainMenu
@@ -85,11 +89,13 @@
this.PrimaryFilesTreeView.Location = new System.Drawing.Point(3, 294);
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.TabIndex = 0;
//
// PanelOne
//
this.PanelOne.Controls.Add(this.ScanRootFolderButton);
this.PanelOne.Controls.Add(this.RootFolderComboBox);
this.PanelOne.Controls.Add(this.label3);
this.PanelOne.Controls.Add(this.ProfileComboBox);
@@ -195,11 +201,39 @@
this.RootFolderComboBox.Size = new System.Drawing.Size(197, 28);
this.RootFolderComboBox.TabIndex = 3;
//
// ScanRootFolderButton
//
this.ScanRootFolderButton.Location = new System.Drawing.Point(186, 238);
this.ScanRootFolderButton.Name = "ScanRootFolderButton";
this.ScanRootFolderButton.Size = new System.Drawing.Size(173, 44);
this.ScanRootFolderButton.TabIndex = 4;
this.ScanRootFolderButton.Text = "Scan Selected Folder";
this.ScanRootFolderButton.UseVisualStyleBackColor = true;
this.ScanRootFolderButton.Click += new System.EventHandler(this.ScanRootFolderButton_Click);
//
// StatusStrip
//
this.StatusStrip.ImageScalingSize = new System.Drawing.Size(24, 24);
this.StatusStrip.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.StatusLabel});
this.StatusStrip.Location = new System.Drawing.Point(0, 874);
this.StatusStrip.Name = "StatusStrip";
this.StatusStrip.Size = new System.Drawing.Size(1472, 32);
this.StatusStrip.TabIndex = 2;
this.StatusStrip.Text = "statusStrip1";
//
// StatusLabel
//
this.StatusLabel.Name = "StatusLabel";
this.StatusLabel.Size = new System.Drawing.Size(179, 25);
this.StatusLabel.Text = "toolStripStatusLabel1";
//
// FileViewer
//
this.AutoScaleDimensions = new System.Drawing.SizeF(9F, 20F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(1472, 906);
this.Controls.Add(this.StatusStrip);
this.Controls.Add(this.tableLayoutPanel1);
this.Controls.Add(this.MainMenu);
this.MainMenuStrip = this.MainMenu;
@@ -212,6 +246,8 @@
this.SelectedFolderPanel.ResumeLayout(false);
this.ActiveFolderGroupBox.ResumeLayout(false);
this.ActiveFolderGroupBox.PerformLayout();
this.StatusStrip.ResumeLayout(false);
this.StatusStrip.PerformLayout();
this.ResumeLayout(false);
this.PerformLayout();
@@ -233,5 +269,8 @@
private System.Windows.Forms.ComboBox SelectedFolderComboBox;
private System.Windows.Forms.Button FolderSelectButton;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Button ScanRootFolderButton;
private System.Windows.Forms.StatusStrip StatusStrip;
private System.Windows.Forms.ToolStripStatusLabel StatusLabel;
}
}
+93 -5
View File
@@ -3,8 +3,10 @@ using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;
@@ -12,18 +14,20 @@ namespace DataOrganizer
{
public partial class FileViewer : Form
{
private List<DB.Profile> Profiles { get; } = new List<DB.Profile>();
private Dictionary<string, DB.Profile> Profiles { get; } = new Dictionary<string, DB.Profile>();
private Dictionary<string, DB.FolderGroup> FolderGroups { get; } = new Dictionary<string, DB.FolderGroup>();
public FileViewer()
{
InitializeComponent();
var db = new DB();
Profiles = db.GetProfiles();
var profiles = db.GetProfiles();
foreach(var profile in Profiles)
foreach(var profile in profiles)
{
ProfileComboBox.Items.Add(profile.Name);
Profiles.Add(profile.Name, profile);
}
ProfileComboBox.SelectedIndexChanged += ProfileComboBox_SelectedIndexChanged;
@@ -33,15 +37,17 @@ namespace DataOrganizer
private void ProfileComboBox_SelectedIndexChanged(object sender, EventArgs e)
{
var profile = Profiles[ProfileComboBox.SelectedIndex];
var profile = Profiles[ProfileComboBox.Text];
RootFolderComboBox.Items.Clear();
FolderGroups.Clear();
if (profile.FolderGroups.Count > 0)
{
foreach (var folder in profile.FolderGroups)
{
RootFolderComboBox.Items.Add(folder.Name);
FolderGroups.Add(folder.Name, folder);
}
RootFolderComboBox.SelectedIndex = 0;
@@ -52,7 +58,89 @@ namespace DataOrganizer
private void FolderSelectButton_Click(object sender, EventArgs e)
{
var folder = FolderGroups[RootFolderComboBox.Text];
var db = new DB();
StatusLabel.Text = $"{db.GetNamingPatternById(folder.PatternId)}";
StatusStrip.Refresh();
}
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 dirs = Directory.EnumerateDirectories(folder.Path);
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);
}
}
private TreeNode CreateNode(DirectoryInfo dir, string pattern)
{
var namingViolation = false;
var node = new TreeNode(dir.Name) { Name = dir.FullName };
var regex = new Regex(pattern);
var files = Directory.EnumerateFiles(dir.FullName);
foreach (var file in files)
{
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))
{
namingViolation = true;
fileNode.ForeColor = Color.DarkOrange;
}
node.Nodes.Add(fileNode);
}
if (namingViolation) node.ForeColor = Color.DarkOrange;
var dirs = Directory.EnumerateDirectories(dir.FullName);
foreach(var folder in dirs)
{
var child = CreateNode(new DirectoryInfo(folder), pattern);
node.ForeColor = child.ForeColor;
node.Nodes.Add(child);
}
return node;
}
private struct Folder
{
}
}
}
+3
View File
@@ -120,4 +120,7 @@
<metadata name="MainMenu.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>17, 17</value>
</metadata>
<metadata name="StatusStrip.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>167, 17</value>
</metadata>
</root>