Browse Source

Added tests, better supporting numerical expressions

tags/v1.1
sbarker 3 years ago
parent
commit
32bde72aa8

+ 2
- 0
.gitignore View File

@@ -9,3 +9,5 @@ packages/*
*.swp
Dist/*
*.ide
*.ncrunch*
_NCrunch*

+ 36
- 0
RegexRenamer.Tests/RegexRenamer.Tests.UnitTests/Properties/AssemblyInfo.cs View File

@@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("RegexRenamer.Tests.UnitTests")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("RegexRenamer.Tests.UnitTests")]
[assembly: AssemblyCopyright("Copyright © 2016")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]
// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("7ffb3e10-6130-4caa-9f1a-c1c777dfae28")]
// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]

+ 108
- 0
RegexRenamer.Tests/RegexRenamer.Tests.UnitTests/RegexRenamer.Tests.UnitTests.csproj View File

@@ -0,0 +1,108 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{7FFB3E10-6130-4CAA-9F1A-C1C777DFAE28}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>RegexRenamer.Tests.UnitTests</RootNamespace>
<AssemblyName>RegexRenamer.Tests.UnitTests</AssemblyName>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<ProjectTypeGuids>{3AC096D0-A1C2-E12C-1390-A8335801FDAB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">10.0</VisualStudioVersion>
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
<ReferencePath>$(ProgramFiles)\Common Files\microsoft shared\VSTT\$(VisualStudioVersion)\UITestExtensionPackages</ReferencePath>
<IsCodedUITest>False</IsCodedUITest>
<TestProjectType>UnitTest</TestProjectType>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="xunit.abstractions, Version=2.0.0.0, Culture=neutral, PublicKeyToken=8d05b1bb7a6fdb6c, processorArchitecture=MSIL">
<HintPath>..\..\packages\xunit.abstractions.2.0.0\lib\net35\xunit.abstractions.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="xunit.assert, Version=2.1.0.3179, Culture=neutral, PublicKeyToken=8d05b1bb7a6fdb6c, processorArchitecture=MSIL">
<HintPath>..\..\packages\xunit.assert.2.1.0\lib\dotnet\xunit.assert.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="xunit.core, Version=2.1.0.3179, Culture=neutral, PublicKeyToken=8d05b1bb7a6fdb6c, processorArchitecture=MSIL">
<HintPath>..\..\packages\xunit.extensibility.core.2.1.0\lib\dotnet\xunit.core.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="xunit.execution.desktop, Version=2.1.0.3179, Culture=neutral, PublicKeyToken=8d05b1bb7a6fdb6c, processorArchitecture=MSIL">
<HintPath>..\..\packages\xunit.extensibility.execution.2.1.0\lib\net45\xunit.execution.desktop.dll</HintPath>
<Private>True</Private>
</Reference>
</ItemGroup>
<Choose>
<When Condition="('$(VisualStudioVersion)' == '10.0' or '$(VisualStudioVersion)' == '') and '$(TargetFrameworkVersion)' == 'v3.5'">
<ItemGroup>
<Reference Include="Microsoft.VisualStudio.QualityTools.UnitTestFramework, Version=10.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL" />
</ItemGroup>
</When>
<Otherwise>
<ItemGroup>
<Reference Include="Microsoft.VisualStudio.QualityTools.UnitTestFramework" />
</ItemGroup>
</Otherwise>
</Choose>
<ItemGroup>
<Compile Include="RegexRenamerCoreUnitTests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\RegexRenamer\RegexRenamer.csproj">
<Project>{4086c5e2-518f-4c11-b891-95a36a6e945f}</Project>
<Name>RegexRenamer</Name>
</ProjectReference>
</ItemGroup>
<Choose>
<When Condition="'$(VisualStudioVersion)' == '10.0' And '$(IsCodedUITest)' == 'True'">
<ItemGroup>
<Reference Include="Microsoft.VisualStudio.QualityTools.CodedUITestFramework, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<Private>False</Private>
</Reference>
<Reference Include="Microsoft.VisualStudio.TestTools.UITest.Common, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<Private>False</Private>
</Reference>
<Reference Include="Microsoft.VisualStudio.TestTools.UITest.Extension, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<Private>False</Private>
</Reference>
<Reference Include="Microsoft.VisualStudio.TestTools.UITesting, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<Private>False</Private>
</Reference>
</ItemGroup>
</When>
</Choose>
<Import Project="$(VSToolsPath)\TeamTest\Microsoft.TestTools.targets" Condition="Exists('$(VSToolsPath)\TeamTest\Microsoft.TestTools.targets')" />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>

BIN
RegexRenamer.Tests/RegexRenamer.Tests.UnitTests/RegexRenamer.Tests.UnitTests.v2.ncrunchproject View File


+ 286
- 0
RegexRenamer.Tests/RegexRenamer.Tests.UnitTests/RegexRenamerCoreUnitTests.cs View File

@@ -0,0 +1,286 @@
using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using Xunit;
namespace RegexRenamer.Tests.UnitTests
{
public class RegexRenamerCoreUnitTests
{
public class Singleton
{
[Fact]
public void IsSingleton()
{
var core = RegexRenamerCore.Instance();
var core2 = RegexRenamerCore.Instance();
Assert.Equal(core, core2);
}
}
public class ScanFiles
{
[Fact]
public void UsesAllPatternForExistingDirectory()
{
var core = RegexRenamerCore.Instance();
core.ScanFiles(".");
Assert.Equal(".", core.CurrentPath);
Assert.Equal("*.*", core.CurrentPattern);
}
[Fact]
public void InterpretsPatternFromPath()
{
var core = RegexRenamerCore.Instance();
core.ScanFiles(@"C:\*.jpg");
Assert.Equal(@"C:\", core.CurrentPath);
Assert.Equal("*.jpg", core.CurrentPattern);
}
[Fact]
public void ReturnsFiles()
{
var core = RegexRenamerCore.Instance();
var tempFile = Path.GetTempFileName();
var files = core.ScanFiles(tempFile);
Assert.NotNull(files);
Assert.NotEmpty(files);
Assert.Equal(1, files.Count());
Assert.Equal(tempFile, files.First().FullPath);
}
}
public class PreviewRename
{
[Fact]
public void CorrectlyGeneratesNewName()
{
var core = RegexRenamerCore.Instance();
var tempFile = Path.GetTempFileName();
Debug.Write(tempFile);
var files = core.ScanFiles(tempFile);
Assert.NotNull(files);
Assert.NotEmpty(files);
Assert.Equal(1, files.Count());
Assert.Equal(tempFile, files.First().FullPath);
files = core.PreviewRename(".tmp", ".foo");
Assert.NotNull(files);
Assert.NotEmpty(files);
Assert.Equal(1, files.Count());
Assert.Equal(tempFile, files.First().FullPath);
Assert.EndsWith(".foo", files.First().Renamed);
}
[Fact]
public void ThrowsInvalidRegularExpression()
{
var core = RegexRenamerCore.Instance();
var tempFile = Path.GetTempFileName();
Debug.Write(tempFile);
var files = core.ScanFiles(tempFile);
Assert.NotNull(files);
Assert.NotEmpty(files);
Assert.Equal(1, files.Count());
Assert.Equal(tempFile, files.First().FullPath);
var ex = Assert.Throws<ArgumentException>(() => core.PreviewRename("((tmp", ""));
Assert.Contains("expression is invalid", ex.Message);
}
[Fact]
public void ThrowsOnUnabalancedLeftExpressionMarkers()
{
var core = RegexRenamerCore.Instance();
var tempFile = Path.GetTempFileName();
Debug.Write(tempFile);
var files = core.ScanFiles(tempFile);
Assert.NotNull(files);
Assert.NotEmpty(files);
Assert.Equal(1, files.Count());
Assert.Equal(tempFile, files.First().FullPath);
var ex = Assert.Throws<ArgumentException>(() => core.PreviewRename(".tmp", ".<<2+2>foo"));
Assert.Contains("balanced expression markers", ex.Message);
}
[Fact]
public void ThrowsOnUnabalancedRightExpressionMarkers()
{
var core = RegexRenamerCore.Instance();
var tempFile = Path.GetTempFileName();
Debug.Write(tempFile);
var files = core.ScanFiles(tempFile);
Assert.NotNull(files);
Assert.NotEmpty(files);
Assert.Equal(1, files.Count());
Assert.Equal(tempFile, files.First().FullPath);
var ex = Assert.Throws<ArgumentException>(() => core.PreviewRename(".tmp", ".<2+2>>foo"));
Assert.Contains("balanced expression markers", ex.Message);
}
[Fact]
public void EvaluatesExpressions()
{
var core = RegexRenamerCore.Instance();
var tempFile = Path.GetTempFileName();
Debug.Write(tempFile);
var files = core.ScanFiles(tempFile);
Assert.NotNull(files);
Assert.NotEmpty(files);
Assert.Equal(1, files.Count());
Assert.Equal(tempFile, files.First().FullPath);
files = core.PreviewRename(".tmp", ".<2+2>foo");
Assert.NotNull(files);
Assert.NotEmpty(files);
Assert.Equal(1, files.Count());
Assert.Equal(tempFile, files.First().FullPath);
Assert.EndsWith(".4foo", files.First().Renamed);
}
[Fact]
public void EvaluatesExpressionsWithMatchReferences()
{
var core = RegexRenamerCore.Instance();
var tempFile = Path.GetTempFileName();
File.Move(tempFile, tempFile + "2");
tempFile = tempFile + "2";
Debug.Write(tempFile);
var files = core.ScanFiles(tempFile);
Assert.NotNull(files);
Assert.NotEmpty(files);
Assert.Equal(1, files.Count());
Assert.Equal(tempFile, files.First().FullPath);
files = core.PreviewRename(@".tmp(\d)", ".<2+$1>foo");
Assert.NotNull(files);
Assert.NotEmpty(files);
Assert.Equal(1, files.Count());
Assert.Equal(tempFile, files.First().FullPath);
Assert.EndsWith(".4foo", files.First().Renamed);
}
[Fact]
public void PadsNumbers()
{
var core = RegexRenamerCore.Instance();
var tempFile = Path.GetTempFileName();
File.Move(tempFile, tempFile + "2");
tempFile = tempFile + "2";
Debug.Write(tempFile);
var files = core.ScanFiles(tempFile);
Assert.NotNull(files);
Assert.NotEmpty(files);
Assert.Equal(1, files.Count());
Assert.Equal(tempFile, files.First().FullPath);
Debug.Print(files.First().DisplayName);
files = core.PreviewRename(@".tmp(\d)", ".<$1:000>foo");
Debug.Print(files.First().Renamed);
Assert.NotNull(files);
Assert.NotEmpty(files);
Assert.Equal(1, files.Count());
Assert.Equal(tempFile, files.First().FullPath);
Assert.EndsWith(".002foo", files.First().Renamed);
}
}
public class RenameFiles
{
[Fact]
public void SkipsNonPreviewItems()
{
var core = RegexRenamerCore.Instance();
var tempFile = Path.GetTempFileName();
core.ScanFiles(tempFile);
Assert.True(File.Exists(tempFile));
core.RenameFiles();
Assert.True(File.Exists(tempFile));
}
[Fact]
public void RenamesFile()
{
var core = RegexRenamerCore.Instance();
var tempFile = Path.GetTempFileName();
core.ScanFiles(tempFile);
core.PreviewRename(".tmp", "$&.foo");
Assert.True(File.Exists(tempFile));
core.RenameFiles();
Assert.False(File.Exists(tempFile));
Assert.True(File.Exists(tempFile + ".foo"));
}
[Fact]
public void CreatesDestinationNonExistantDirectory()
{
var core = RegexRenamerCore.Instance();
var tempFile = Path.GetTempFileName();
var guid = Guid.NewGuid().ToString();
core.ScanFiles(tempFile);
var items = core.PreviewRename("^tmp", $"$&\\{guid}");
Assert.True(File.Exists(tempFile));
core.RenameFiles();
Assert.False(File.Exists(tempFile));
Debug.Print($"Renamed: {items.First().Renamed}");
var renamed = Path.Combine(core.CurrentPath, items.First().Renamed);
Assert.True(File.Exists(renamed));
}
[Fact]
public void DoesNotMoveWhenNotRenamed()
{
var core = RegexRenamerCore.Instance();
var tempFile = Path.GetTempFileName();
var files = core.ScanFiles(tempFile);
core.PreviewRename(@"^", "$&");
Assert.True(File.Exists(tempFile));
core.RenameFiles();
Assert.True(File.Exists(tempFile));
}
}
}
}

+ 9
- 0
RegexRenamer.Tests/RegexRenamer.Tests.UnitTests/packages.config View File

@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="xunit" version="2.1.0" targetFramework="net45" />
<package id="xunit.abstractions" version="2.0.0" targetFramework="net45" />
<package id="xunit.assert" version="2.1.0" targetFramework="net45" />
<package id="xunit.core" version="2.1.0" targetFramework="net45" />
<package id="xunit.extensibility.core" version="2.1.0" targetFramework="net45" />
<package id="xunit.extensibility.execution" version="2.1.0" targetFramework="net45" />
</packages>

+ 10
- 2
RegexRenamer.sln View File

@@ -1,6 +1,8 @@

Microsoft Visual Studio Solution File, Format Version 11.00
# Visual Studio 2010
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 14
VisualStudioVersion = 14.0.25420.1
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RegexRenamer", "RegexRenamer\RegexRenamer.csproj", "{4086C5E2-518F-4C11-B891-95A36A6E945F}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = ".nuget", ".nuget", "{C573B28A-46C1-4FD5-88AF-FD79215035DB}"
@@ -10,6 +12,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = ".nuget", ".nuget", "{C573B2
.nuget\NuGet.targets = .nuget\NuGet.targets
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RegexRenamer.Tests.UnitTests", "RegexRenamer.Tests\RegexRenamer.Tests.UnitTests\RegexRenamer.Tests.UnitTests.csproj", "{7FFB3E10-6130-4CAA-9F1A-C1C777DFAE28}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@@ -20,6 +24,10 @@ Global
{4086C5E2-518F-4C11-B891-95A36A6E945F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4086C5E2-518F-4C11-B891-95A36A6E945F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4086C5E2-518F-4C11-B891-95A36A6E945F}.Release|Any CPU.Build.0 = Release|Any CPU
{7FFB3E10-6130-4CAA-9F1A-C1C777DFAE28}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{7FFB3E10-6130-4CAA-9F1A-C1C777DFAE28}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7FFB3E10-6130-4CAA-9F1A-C1C777DFAE28}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7FFB3E10-6130-4CAA-9F1A-C1C777DFAE28}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE

+ 3
- 5
RegexRenamer/DisabledLabel.cs View File

@@ -1,13 +1,11 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace RegexRenamer
{
class DisabledLabel : Label
[ExcludeFromCodeCoverage]
internal class DisabledLabel : Label
{
protected override void OnPaint(PaintEventArgs e)
{

+ 159
- 158
RegexRenamer/Main.Designer.cs View File

@@ -1,4 +1,7 @@
namespace RegexRenamer {
using System.Diagnostics.CodeAnalysis;
namespace RegexRenamer {
[ExcludeFromCodeCoverage]
partial class Main {
/// <summary>
/// Required designer variable.
@@ -23,163 +26,161 @@
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent() {
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Main));
this.labelPath = new System.Windows.Forms.Label();
this.textboxFiles = new System.Windows.Forms.TextBox();
this.buttonScan = new System.Windows.Forms.Button();
this.listPreviews = new System.Windows.Forms.ListView();
this.columnHeader1 = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader()));
this.columnHeader2 = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader()));
this.buttonApply = new System.Windows.Forms.Button();
this.labelPattern = new System.Windows.Forms.Label();
this.textboxPattern = new System.Windows.Forms.TextBox();
this.buttonPreview = new System.Windows.Forms.Button();
this.labelReplace = new System.Windows.Forms.Label();
this.textboxReplace = new System.Windows.Forms.TextBox();
this.checkBoxRecursive = new System.Windows.Forms.CheckBox();
this.buttonBrowse = new System.Windows.Forms.Button();
this.pictureIcon = new System.Windows.Forms.PictureBox();
this.checkboxEvaluateExpressions = new System.Windows.Forms.CheckBox();
this.labelCopyright = new RegexRenamer.DisabledLabel();
this.linkViewLog = new System.Windows.Forms.LinkLabel();
((System.ComponentModel.ISupportInitialize)(this.pictureIcon)).BeginInit();
this.SuspendLayout();
//
// labelPath
//
resources.ApplyResources(this.labelPath, "labelPath");
this.labelPath.Name = "labelPath";
//
// textboxFiles
//
resources.ApplyResources(this.textboxFiles, "textboxFiles");
this.textboxFiles.Name = "textboxFiles";
//
// buttonScan
//
resources.ApplyResources(this.buttonScan, "buttonScan");
this.buttonScan.Name = "buttonScan";
this.buttonScan.UseVisualStyleBackColor = true;
this.buttonScan.Click += new System.EventHandler(this.buttonScan_Click);
//
// listPreviews
//
resources.ApplyResources(this.listPreviews, "listPreviews");
this.listPreviews.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Main));
this.labelPath = new System.Windows.Forms.Label();
this.textboxFiles = new System.Windows.Forms.TextBox();
this.buttonScan = new System.Windows.Forms.Button();
this.listPreviews = new System.Windows.Forms.ListView();
this.columnHeader1 = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader()));
this.columnHeader2 = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader()));
this.buttonApply = new System.Windows.Forms.Button();
this.labelPattern = new System.Windows.Forms.Label();
this.textboxPattern = new System.Windows.Forms.TextBox();
this.buttonPreview = new System.Windows.Forms.Button();
this.labelReplace = new System.Windows.Forms.Label();
this.textboxReplace = new System.Windows.Forms.TextBox();
this.checkBoxRecursive = new System.Windows.Forms.CheckBox();
this.buttonBrowse = new System.Windows.Forms.Button();
this.pictureIcon = new System.Windows.Forms.PictureBox();
this.linkViewLog = new System.Windows.Forms.LinkLabel();
this.labelCopyright = new RegexRenamer.DisabledLabel();
this.labelExpressionDescription = new System.Windows.Forms.Label();
((System.ComponentModel.ISupportInitialize)(this.pictureIcon)).BeginInit();
this.SuspendLayout();
//
// labelPath
//
resources.ApplyResources(this.labelPath, "labelPath");
this.labelPath.Name = "labelPath";
//
// textboxFiles
//
resources.ApplyResources(this.textboxFiles, "textboxFiles");
this.textboxFiles.Name = "textboxFiles";
//
// buttonScan
//
resources.ApplyResources(this.buttonScan, "buttonScan");
this.buttonScan.Name = "buttonScan";
this.buttonScan.UseVisualStyleBackColor = true;
this.buttonScan.Click += new System.EventHandler(this.buttonScan_Click);
//
// listPreviews
//
resources.ApplyResources(this.listPreviews, "listPreviews");
this.listPreviews.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
this.columnHeader1,
this.columnHeader2});
this.listPreviews.Name = "listPreviews";
this.listPreviews.Sorting = System.Windows.Forms.SortOrder.Ascending;
this.listPreviews.UseCompatibleStateImageBehavior = false;
this.listPreviews.View = System.Windows.Forms.View.Details;
//
// columnHeader1
//
resources.ApplyResources(this.columnHeader1, "columnHeader1");
//
// columnHeader2
//
resources.ApplyResources(this.columnHeader2, "columnHeader2");
//
// buttonApply
//
resources.ApplyResources(this.buttonApply, "buttonApply");
this.buttonApply.Name = "buttonApply";
this.buttonApply.UseVisualStyleBackColor = true;
this.buttonApply.Click += new System.EventHandler(this.buttonApply_Click);
//
// labelPattern
//
resources.ApplyResources(this.labelPattern, "labelPattern");
this.labelPattern.Name = "labelPattern";
//
// textboxPattern
//
resources.ApplyResources(this.textboxPattern, "textboxPattern");
this.textboxPattern.Name = "textboxPattern";
//
// buttonPreview
//
resources.ApplyResources(this.buttonPreview, "buttonPreview");
this.buttonPreview.Name = "buttonPreview";
this.buttonPreview.UseVisualStyleBackColor = true;
this.buttonPreview.Click += new System.EventHandler(this.buttonPreview_Click);
//
// labelReplace
//
resources.ApplyResources(this.labelReplace, "labelReplace");
this.labelReplace.Name = "labelReplace";
//
// textboxReplace
//
resources.ApplyResources(this.textboxReplace, "textboxReplace");
this.textboxReplace.Name = "textboxReplace";
//
// checkBoxRecursive
//
resources.ApplyResources(this.checkBoxRecursive, "checkBoxRecursive");
this.checkBoxRecursive.Name = "checkBoxRecursive";
this.checkBoxRecursive.UseVisualStyleBackColor = true;
//
// buttonBrowse
//
resources.ApplyResources(this.buttonBrowse, "buttonBrowse");
this.buttonBrowse.Name = "buttonBrowse";
this.buttonBrowse.UseVisualStyleBackColor = true;
this.buttonBrowse.Click += new System.EventHandler(this.buttonBrowse_Click);
//
// pictureIcon
//
resources.ApplyResources(this.pictureIcon, "pictureIcon");
this.pictureIcon.Image = global::RegexRenamer.Resources.Logo;
this.pictureIcon.Name = "pictureIcon";
this.pictureIcon.TabStop = false;
this.pictureIcon.Click += new System.EventHandler(this.pictureIcon_Click);
//
// checkboxEvaluateExpressions
//
resources.ApplyResources(this.checkboxEvaluateExpressions, "checkboxEvaluateExpressions");
this.checkboxEvaluateExpressions.Name = "checkboxEvaluateExpressions";
this.checkboxEvaluateExpressions.UseVisualStyleBackColor = true;
//
// labelCopyright
//
resources.ApplyResources(this.labelCopyright, "labelCopyright");
this.labelCopyright.Cursor = System.Windows.Forms.Cursors.Hand;
this.labelCopyright.Name = "labelCopyright";
this.labelCopyright.Click += new System.EventHandler(this.labelCopyright_Click);
//
// linkViewLog
//
resources.ApplyResources(this.linkViewLog, "linkViewLog");
this.linkViewLog.Name = "linkViewLog";
this.linkViewLog.TabStop = true;
//
// Main
//
resources.ApplyResources(this, "$this");
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.Controls.Add(this.linkViewLog);
this.Controls.Add(this.checkboxEvaluateExpressions);
this.Controls.Add(this.pictureIcon);
this.Controls.Add(this.labelCopyright);
this.Controls.Add(this.buttonBrowse);
this.Controls.Add(this.checkBoxRecursive);
this.Controls.Add(this.textboxReplace);
this.Controls.Add(this.labelReplace);
this.Controls.Add(this.buttonPreview);
this.Controls.Add(this.textboxPattern);
this.Controls.Add(this.labelPattern);
this.Controls.Add(this.buttonApply);
this.Controls.Add(this.listPreviews);
this.Controls.Add(this.buttonScan);
this.Controls.Add(this.textboxFiles);
this.Controls.Add(this.labelPath);
this.Name = "Main";
this.Load += new System.EventHandler(this.Main_Load);
((System.ComponentModel.ISupportInitialize)(this.pictureIcon)).EndInit();
this.ResumeLayout(false);
this.PerformLayout();
this.listPreviews.Name = "listPreviews";
this.listPreviews.Sorting = System.Windows.Forms.SortOrder.Ascending;
this.listPreviews.UseCompatibleStateImageBehavior = false;
this.listPreviews.View = System.Windows.Forms.View.Details;
//
// columnHeader1
//
resources.ApplyResources(this.columnHeader1, "columnHeader1");
//
// columnHeader2
//
resources.ApplyResources(this.columnHeader2, "columnHeader2");
//
// buttonApply
//
resources.ApplyResources(this.buttonApply, "buttonApply");
this.buttonApply.Name = "buttonApply";
this.buttonApply.UseVisualStyleBackColor = true;
this.buttonApply.Click += new System.EventHandler(this.buttonApply_Click);
//
// labelPattern
//
resources.ApplyResources(this.labelPattern, "labelPattern");
this.labelPattern.Name = "labelPattern";
//
// textboxPattern
//
resources.ApplyResources(this.textboxPattern, "textboxPattern");
this.textboxPattern.Name = "textboxPattern";
//
// buttonPreview
//
resources.ApplyResources(this.buttonPreview, "buttonPreview");
this.buttonPreview.Name = "buttonPreview";
this.buttonPreview.UseVisualStyleBackColor = true;
this.buttonPreview.Click += new System.EventHandler(this.buttonPreview_Click);
//
// labelReplace
//
resources.ApplyResources(this.labelReplace, "labelReplace");
this.labelReplace.Name = "labelReplace";
//
// textboxReplace
//
resources.ApplyResources(this.textboxReplace, "textboxReplace");
this.textboxReplace.Name = "textboxReplace";
//
// checkBoxRecursive
//
resources.ApplyResources(this.checkBoxRecursive, "checkBoxRecursive");
this.checkBoxRecursive.Name = "checkBoxRecursive";
this.checkBoxRecursive.UseVisualStyleBackColor = true;
//
// buttonBrowse
//
resources.ApplyResources(this.buttonBrowse, "buttonBrowse");
this.buttonBrowse.Name = "buttonBrowse";
this.buttonBrowse.UseVisualStyleBackColor = true;
this.buttonBrowse.Click += new System.EventHandler(this.buttonBrowse_Click);
//
// pictureIcon
//
resources.ApplyResources(this.pictureIcon, "pictureIcon");
this.pictureIcon.Image = global::RegexRenamer.Resources.Logo;
this.pictureIcon.Name = "pictureIcon";
this.pictureIcon.TabStop = false;
this.pictureIcon.Click += new System.EventHandler(this.pictureIcon_Click);
//
// linkViewLog
//
resources.ApplyResources(this.linkViewLog, "linkViewLog");
this.linkViewLog.Name = "linkViewLog";
//
// labelCopyright
//
resources.ApplyResources(this.labelCopyright, "labelCopyright");
this.labelCopyright.Cursor = System.Windows.Forms.Cursors.Hand;
this.labelCopyright.Name = "labelCopyright";
this.labelCopyright.Click += new System.EventHandler(this.labelCopyright_Click);
//
// labelExpressionDescription
//
resources.ApplyResources(this.labelExpressionDescription, "labelExpressionDescription");
this.labelExpressionDescription.Name = "labelExpressionDescription";
//
// Main
//
resources.ApplyResources(this, "$this");
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.Controls.Add(this.labelExpressionDescription);
this.Controls.Add(this.linkViewLog);
this.Controls.Add(this.pictureIcon);
this.Controls.Add(this.labelCopyright);
this.Controls.Add(this.buttonBrowse);
this.Controls.Add(this.checkBoxRecursive);
this.Controls.Add(this.textboxReplace);
this.Controls.Add(this.labelReplace);
this.Controls.Add(this.buttonPreview);
this.Controls.Add(this.textboxPattern);
this.Controls.Add(this.labelPattern);
this.Controls.Add(this.buttonApply);
this.Controls.Add(this.listPreviews);
this.Controls.Add(this.buttonScan);
this.Controls.Add(this.textboxFiles);
this.Controls.Add(this.labelPath);
this.Name = "Main";
this.Load += new System.EventHandler(this.Main_Load);
((System.ComponentModel.ISupportInitialize)(this.pictureIcon)).EndInit();
this.ResumeLayout(false);
this.PerformLayout();
}
@@ -201,8 +202,8 @@
private System.Windows.Forms.Button buttonBrowse;
private DisabledLabel labelCopyright;
private System.Windows.Forms.PictureBox pictureIcon;
private System.Windows.Forms.CheckBox checkboxEvaluateExpressions;
private System.Windows.Forms.LinkLabel linkViewLog;
}
private System.Windows.Forms.Label labelExpressionDescription;
}
}

+ 38
- 21
RegexRenamer/Main.cs View File

@@ -3,15 +3,15 @@ using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using System.Linq;
using NCalc;
namespace RegexRenamer
{
public partial class Main : Form
{
private IEnumerable<FileItem> _items;
public Main()
{
InitializeComponent();
@@ -31,15 +31,14 @@ namespace RegexRenamer
);
return;
}
RegexRenamerCore regexRenamerCore = RegexRenamerCore.Instance();
regexRenamerCore.CurrentPath = textboxFiles.Text;
var regexRenamerCore = RegexRenamerCore.Instance();
listPreviews.Items.Clear();
List<ListViewItem> items = (List<ListViewItem>) regexRenamerCore.ScanForFiles(checkBoxRecursive.Checked);
if (items.Count == 0)
_items = regexRenamerCore.ScanFiles(textboxFiles.Text, checkBoxRecursive.Checked);
if (!_items.Any())
{
MessageBox.Show(
string.Format("No files were found matching '{0}'", Path.Combine(regexRenamerCore.CurrentPath, regexRenamerCore.CurrentPattern)),
$"No files were found matching '{Path.Combine(regexRenamerCore.CurrentPath, regexRenamerCore.CurrentPattern)}'",
"No Files Found",
MessageBoxButtons.OK,
MessageBoxIcon.Error
@@ -47,41 +46,49 @@ namespace RegexRenamer
}
else
{
listPreviews.Items.AddRange(items.ToArray());
SetListViewItems(_items);
}
}
private void buttonPreview_Click(object sender, EventArgs e)
{
/*
if (!textboxPattern.Text.IsRegularExpression())
{
MessageBox.Show(
string.Format("Invalid regular expression specified for Pattern:\nPattern: {0}", textboxPattern.Text),
$"Invalid regular expression specified for Pattern:\nPattern: {textboxPattern.Text}",
"Invalid Regular Expression",
MessageBoxButtons.OK,
MessageBoxIcon.Error
);
return;
}
*/
RegexRenamerCore regexRenamerCore = RegexRenamerCore.Instance();
List<ListViewItem> items = regexRenamerCore.PreviewRename(listPreviews.Items.ToList(), textboxPattern.Text, textboxReplace.Text,
checkboxEvaluateExpressions.Checked);
var regexRenamerCore = RegexRenamerCore.Instance();
_items = regexRenamerCore.PreviewRename(textboxPattern.Text, textboxReplace.Text);
SetListViewItems(_items);
}
private void SetListViewItems(IEnumerable<FileItem> items)
{
listPreviews.Items.Clear();
listPreviews.Items.AddRange(items.ToArray());
var listItems = _items.Select(item => new ListViewItem(new [] {item.DisplayName, item.Renamed}));
listPreviews.Items.AddRange(listItems.ToArray());
}
private void buttonApply_Click(object sender, EventArgs e)
{
RegexRenamerCore regexRenamerCore = RegexRenamerCore.Instance();
regexRenamerCore.RenameFiles(listPreviews.Items.ToList());
regexRenamerCore.ScanForFiles(checkBoxRecursive.Checked);
var regexRenamerCore = RegexRenamerCore.Instance();
regexRenamerCore.RenameFiles();
var items = regexRenamerCore.ScanFiles(textboxFiles.Text, checkBoxRecursive.Checked);
SetListViewItems(items);
}
private void buttonBrowse_Click(object sender, EventArgs e)
{
FolderBrowserDialog browse = new FolderBrowserDialog();
var browse = new FolderBrowserDialog();
if (browse.ShowDialog() == DialogResult.OK)
{
textboxFiles.Text = browse.SelectedPath;
@@ -106,10 +113,20 @@ namespace RegexRenamer
private string GetExecutingAssemblyVersion()
{
Assembly assembly = Assembly.GetExecutingAssembly();
FileVersionInfo fvi = FileVersionInfo.GetVersionInfo(assembly.Location);
string version = fvi.FileVersion;
var assembly = Assembly.GetExecutingAssembly();
var fvi = FileVersionInfo.GetVersionInfo(assembly.Location);
var version = fvi.FileVersion;
return version;
}
}
private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
MessageBox.Show(
"Numerical evaluations can be performed by enclosing the expressions inside of angle brackets, < >. Back-references are supported inside of expressions.",
"Numerical Expressions",
MessageBoxButtons.OK,
MessageBoxIcon.Information
);
}
}
}

+ 99
- 75
RegexRenamer/Main.resx View File

@@ -127,10 +127,13 @@
</data>
<assembly alias="System.Drawing" name="System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
<data name="labelPath.Location" type="System.Drawing.Point, System.Drawing">
<value>10, 14</value>
<value>15, 22</value>
</data>
<data name="labelPath.Margin" type="System.Windows.Forms.Padding, System.Windows.Forms">
<value>4, 0, 4, 0</value>
</data>
<data name="labelPath.Size" type="System.Drawing.Size, System.Drawing">
<value>32, 13</value>
<value>46, 20</value>
</data>
<data name="labelPath.TabIndex" type="System.Int32, mscorlib">
<value>0</value>
@@ -151,10 +154,13 @@
<value>15</value>
</data>
<data name="textboxFiles.Location" type="System.Drawing.Point, System.Drawing">
<value>91, 11</value>
<value>136, 17</value>
</data>
<data name="textboxFiles.Margin" type="System.Windows.Forms.Padding, System.Windows.Forms">
<value>4, 5, 4, 5</value>
</data>
<data name="textboxFiles.Size" type="System.Drawing.Size, System.Drawing">
<value>330, 20</value>
<value>493, 26</value>
</data>
<data name="textboxFiles.TabIndex" type="System.Int32, mscorlib">
<value>1</value>
@@ -175,10 +181,13 @@
<value>NoControl</value>
</data>
<data name="buttonScan.Location" type="System.Drawing.Point, System.Drawing">
<value>640, 9</value>
<value>960, 14</value>
</data>
<data name="buttonScan.Margin" type="System.Windows.Forms.Padding, System.Windows.Forms">
<value>4, 5, 4, 5</value>
</data>
<data name="buttonScan.Size" type="System.Drawing.Size, System.Drawing">
<value>75, 23</value>
<value>112, 35</value>
</data>
<data name="buttonScan.TabIndex" type="System.Int32, mscorlib">
<value>4</value>
@@ -205,7 +214,7 @@
<value>Original</value>
</data>
<data name="columnHeader1.Width" type="System.Int32, mscorlib">
<value>300</value>
<value>513</value>
</data>
<data name="columnHeader2.Text" xml:space="preserve">
<value>Renamed</value>
@@ -214,10 +223,13 @@
<value>265</value>
</data>
<data name="listPreviews.Location" type="System.Drawing.Point, System.Drawing">
<value>12, 92</value>
<value>18, 142</value>
</data>
<data name="listPreviews.Margin" type="System.Windows.Forms.Padding, System.Windows.Forms">
<value>4, 5, 4, 5</value>
</data>
<data name="listPreviews.Size" type="System.Drawing.Size, System.Drawing">
<value>702, 388</value>
<value>1053, 597</value>
</data>
<data name="listPreviews.TabIndex" type="System.Int32, mscorlib">
<value>11</value>
@@ -241,10 +253,13 @@
<value>NoControl</value>
</data>
<data name="buttonApply.Location" type="System.Drawing.Point, System.Drawing">
<value>638, 485</value>
<value>959, 750</value>
</data>
<data name="buttonApply.Margin" type="System.Windows.Forms.Padding, System.Windows.Forms">
<value>4, 5, 4, 5</value>
</data>
<data name="buttonApply.Size" type="System.Drawing.Size, System.Drawing">
<value>75, 23</value>
<value>112, 35</value>
</data>
<data name="buttonApply.TabIndex" type="System.Int32, mscorlib">
<value>12</value>
@@ -271,10 +286,13 @@
<value>NoControl</value>
</data>
<data name="labelPattern.Location" type="System.Drawing.Point, System.Drawing">
<value>10, 42</value>
<value>15, 65</value>
</data>
<data name="labelPattern.Margin" type="System.Windows.Forms.Padding, System.Windows.Forms">
<value>4, 0, 4, 0</value>
</data>
<data name="labelPattern.Size" type="System.Drawing.Size, System.Drawing">
<value>44, 13</value>
<value>65, 20</value>
</data>
<data name="labelPattern.TabIndex" type="System.Int32, mscorlib">
<value>5</value>
@@ -295,10 +313,13 @@
<value>10</value>
</data>
<data name="textboxPattern.Location" type="System.Drawing.Point, System.Drawing">
<value>91, 39</value>
<value>136, 60</value>
</data>
<data name="textboxPattern.Margin" type="System.Windows.Forms.Padding, System.Windows.Forms">
<value>4, 5, 4, 5</value>
</data>
<data name="textboxPattern.Size" type="System.Drawing.Size, System.Drawing">
<value>330, 20</value>
<value>493, 26</value>
</data>
<data name="textboxPattern.TabIndex" type="System.Int32, mscorlib">
<value>6</value>
@@ -319,10 +340,13 @@
<value>NoControl</value>
</data>
<data name="buttonPreview.Location" type="System.Drawing.Point, System.Drawing">
<value>640, 63</value>
<value>960, 97</value>
</data>
<data name="buttonPreview.Margin" type="System.Windows.Forms.Padding, System.Windows.Forms">
<value>4, 5, 4, 5</value>
</data>
<data name="buttonPreview.Size" type="System.Drawing.Size, System.Drawing">
<value>75, 23</value>
<value>112, 35</value>
</data>
<data name="buttonPreview.TabIndex" type="System.Int32, mscorlib">
<value>10</value>
@@ -349,10 +373,13 @@
<value>NoControl</value>
</data>
<data name="labelReplace.Location" type="System.Drawing.Point, System.Drawing">
<value>10, 68</value>
<value>15, 105</value>
</data>
<data name="labelReplace.Margin" type="System.Windows.Forms.Padding, System.Windows.Forms">
<value>4, 0, 4, 0</value>
</data>
<data name="labelReplace.Size" type="System.Drawing.Size, System.Drawing">
<value>75, 13</value>
<value>108, 20</value>
</data>
<data name="labelReplace.TabIndex" type="System.Int32, mscorlib">
<value>7</value>
@@ -373,10 +400,13 @@
<value>7</value>
</data>
<data name="textboxReplace.Location" type="System.Drawing.Point, System.Drawing">
<value>91, 65</value>
<value>136, 100</value>
</data>
<data name="textboxReplace.Margin" type="System.Windows.Forms.Padding, System.Windows.Forms">
<value>4, 5, 4, 5</value>
</data>
<data name="textboxReplace.Size" type="System.Drawing.Size, System.Drawing">
<value>330, 20</value>
<value>493, 26</value>
</data>
<data name="textboxReplace.TabIndex" type="System.Int32, mscorlib">
<value>8</value>
@@ -400,10 +430,13 @@
<value>NoControl</value>
</data>
<data name="checkBoxRecursive.Location" type="System.Drawing.Point, System.Drawing">
<value>427, 13</value>
<value>640, 20</value>
</data>
<data name="checkBoxRecursive.Margin" type="System.Windows.Forms.Padding, System.Windows.Forms">
<value>4, 5, 4, 5</value>
</data>
<data name="checkBoxRecursive.Size" type="System.Drawing.Size, System.Drawing">
<value>102, 17</value>
<value>146, 24</value>
</data>
<data name="checkBoxRecursive.TabIndex" type="System.Int32, mscorlib">
<value>2</value>
@@ -427,10 +460,13 @@
<value>NoControl</value>
</data>
<data name="buttonBrowse.Location" type="System.Drawing.Point, System.Drawing">
<value>559, 9</value>
<value>838, 14</value>
</data>
<data name="buttonBrowse.Margin" type="System.Windows.Forms.Padding, System.Windows.Forms">
<value>4, 5, 4, 5</value>
</data>
<data name="buttonBrowse.Size" type="System.Drawing.Size, System.Drawing">
<value>75, 23</value>
<value>112, 35</value>
</data>
<data name="buttonBrowse.TabIndex" type="System.Int32, mscorlib">
<value>3</value>
@@ -457,13 +493,10 @@
<value>NoControl</value>
</data>
<data name="pictureIcon.Location" type="System.Drawing.Point, System.Drawing">
<value>12, 485</value>
</data>
<data name="pictureIcon.Margin" type="System.Windows.Forms.Padding, System.Windows.Forms">
<value>2, 2, 2, 2</value>
<value>18, 748</value>
</data>
<data name="pictureIcon.Size" type="System.Drawing.Size, System.Drawing">
<value>36, 39</value>
<value>54, 60</value>
</data>
<data name="pictureIcon.SizeMode" type="System.Windows.Forms.PictureBoxSizeMode, System.Windows.Forms">
<value>StretchImage</value>
@@ -483,34 +516,25 @@
<data name="&gt;&gt;pictureIcon.ZOrder" xml:space="preserve">
<value>2</value>
</data>
<data name="checkboxEvaluateExpressions.AutoSize" type="System.Boolean, mscorlib">
<value>True</value>
</data>
<data name="checkboxEvaluateExpressions.ImeMode" type="System.Windows.Forms.ImeMode, System.Windows.Forms">
<value>NoControl</value>
</data>
<data name="checkboxEvaluateExpressions.Location" type="System.Drawing.Point, System.Drawing">
<value>426, 67</value>
</data>
<data name="checkboxEvaluateExpressions.Size" type="System.Drawing.Size, System.Drawing">
<value>127, 17</value>
<data name="linkViewLog.Location" type="System.Drawing.Point, System.Drawing">
<value>0, 0</value>
</data>
<data name="checkboxEvaluateExpressions.TabIndex" type="System.Int32, mscorlib">
<value>9</value>
<data name="linkViewLog.Size" type="System.Drawing.Size, System.Drawing">
<value>100, 23</value>
</data>
<data name="checkboxEvaluateExpressions.Text" xml:space="preserve">
<value>E&amp;valuate Expressions</value>
<data name="linkViewLog.TabIndex" type="System.Int32, mscorlib">
<value>16</value>
</data>
<data name="&gt;&gt;checkboxEvaluateExpressions.Name" xml:space="preserve">
<value>checkboxEvaluateExpressions</value>
<data name="&gt;&gt;linkViewLog.Name" xml:space="preserve">
<value>linkViewLog</value>
</data>
<data name="&gt;&gt;checkboxEvaluateExpressions.Type" xml:space="preserve">
<value>System.Windows.Forms.CheckBox, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
<data name="&gt;&gt;linkViewLog.Type" xml:space="preserve">
<value>System.Windows.Forms.LinkLabel, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</data>
<data name="&gt;&gt;checkboxEvaluateExpressions.Parent" xml:space="preserve">
<data name="&gt;&gt;linkViewLog.Parent" xml:space="preserve">
<value>$this</value>
</data>
<data name="&gt;&gt;checkboxEvaluateExpressions.ZOrder" xml:space="preserve">
<data name="&gt;&gt;linkViewLog.ZOrder" xml:space="preserve">
<value>1</value>
</data>
<data name="labelCopyright.Anchor" type="System.Windows.Forms.AnchorStyles, System.Windows.Forms">
@@ -520,19 +544,16 @@
<value>NoControl</value>
</data>
<data name="labelCopyright.Location" type="System.Drawing.Point, System.Drawing">
<value>52, 486</value>
</data>
<data name="labelCopyright.Margin" type="System.Windows.Forms.Padding, System.Windows.Forms">
<value>2, 0, 2, 0</value>
<value>78, 750</value>
</data>
<data name="labelCopyright.Size" type="System.Drawing.Size, System.Drawing">
<value>370, 39</value>
<value>555, 60</value>
</data>
<data name="labelCopyright.TabIndex" type="System.Int32, mscorlib">
<value>13</value>
</data>
<data name="labelCopyright.Text" xml:space="preserve">
<value>Version ${version}. Copyright © 2012-2013 Scott D. Barker
<value>Version ${version}. Copyright © 2012-2016 Scott D. Barker
Licensed under the MIT License, http://license.sdbarker.com/regexrenamer
Source code is available at http://git.sdbarker.com/sdbarker/regexrenamer</value>
</data>
@@ -540,7 +561,7 @@ Source code is available at http://git.sdbarker.com/sdbarker/regexrenamer</value
<value>labelCopyright</value>
</data>
<data name="&gt;&gt;labelCopyright.Type" xml:space="preserve">
<value>RegexRenamer.DisabledLabel, RegexRenamer, Version=1.1.4920.17873, Culture=neutral, PublicKeyToken=null</value>
<value>RegexRenamer.DisabledLabel, RegexRenamer, Version=1.1.6088.32205, Culture=neutral, PublicKeyToken=null</value>
</data>
<data name="&gt;&gt;labelCopyright.Parent" xml:space="preserve">
<value>$this</value>
@@ -548,41 +569,41 @@ Source code is available at http://git.sdbarker.com/sdbarker/regexrenamer</value
<data name="&gt;&gt;labelCopyright.ZOrder" xml:space="preserve">
<value>3</value>
</data>
<data name="linkViewLog.AutoSize" type="System.Boolean, mscorlib">
<data name="labelExpressionDescription.AutoSize" type="System.Boolean, mscorlib">
<value>True</value>
</data>
<data name="linkViewLog.Location" type="System.Drawing.Point, System.Drawing">
<value>655, 512</value>
<data name="labelExpressionDescription.Location" type="System.Drawing.Point, System.Drawing">
<value>640, 105</value>
</data>
<data name="linkViewLog.Size" type="System.Drawing.Size, System.Drawing">
<value>51, 13</value>
<data name="labelExpressionDescription.Size" type="System.Drawing.Size, System.Drawing">
<value>309, 20</value>
</data>
<data name="linkViewLog.TabIndex" type="System.Int32, mscorlib">
<value>14</value>
<data name="labelExpressionDescription.TabIndex" type="System.Int32, mscorlib">
<value>15</value>
</data>
<data name="linkViewLog.Text" xml:space="preserve">
<value>View &amp;Log</value>
<data name="labelExpressionDescription.Text" xml:space="preserve">
<value>Use &lt; and &gt; to evaluate math expressions.</value>
</data>
<data name="&gt;&gt;linkViewLog.Name" xml:space="preserve">
<value>linkViewLog</value>
<data name="&gt;&gt;labelExpressionDescription.Name" xml:space="preserve">
<value>labelExpressionDescription</value>
</data>
<data name="&gt;&gt;linkViewLog.Type" xml:space="preserve">
<value>System.Windows.Forms.LinkLabel, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
<data name="&gt;&gt;labelExpressionDescription.Type" xml:space="preserve">
<value>System.Windows.Forms.Label, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</data>
<data name="&gt;&gt;linkViewLog.Parent" xml:space="preserve">
<data name="&gt;&gt;labelExpressionDescription.Parent" xml:space="preserve">
<value>$this</value>
</data>
<data name="&gt;&gt;linkViewLog.ZOrder" xml:space="preserve">
<data name="&gt;&gt;labelExpressionDescription.ZOrder" xml:space="preserve">
<value>0</value>
</data>
<metadata name="$this.Localizable" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>True</value>
</metadata>
<data name="$this.AutoScaleDimensions" type="System.Drawing.SizeF, System.Drawing">
<value>6, 13</value>
<value>9, 20</value>
</data>
<data name="$this.ClientSize" type="System.Drawing.Size, System.Drawing">
<value>722, 531</value>
<value>1085, 819</value>
</data>
<data name="$this.Icon" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>
@@ -1084,6 +1105,9 @@ Source code is available at http://git.sdbarker.com/sdbarker/regexrenamer</value
AAAmAAAAJgAAACYAAAAmAAAAJgAAACYAAAAmAAAAJgAAACYA
</value>
</data>
<data name="$this.Margin" type="System.Windows.Forms.Padding, System.Windows.Forms">
<value>4, 5, 4, 5</value>
</data>
<data name="$this.Text" xml:space="preserve">
<value>RegexRenamer</value>
</data>

+ 2
- 0
RegexRenamer/Program.cs View File

@@ -1,7 +1,9 @@
using System;
using System.Diagnostics.CodeAnalysis;
using System.Windows.Forms;
namespace RegexRenamer {
[ExcludeFromCodeCoverage]
static class Program {
/// <summary>
/// The main entry point for the application.

+ 3
- 0
RegexRenamer/Properties/Resources.Designer.cs View File

@@ -8,6 +8,8 @@
// </auto-generated>
//------------------------------------------------------------------------------
using System.Diagnostics.CodeAnalysis;
namespace RegexRenamer.Properties {
using System;
@@ -22,6 +24,7 @@ namespace RegexRenamer.Properties {
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "4.0.0.0")]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
[global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
[ExcludeFromCodeCoverage]
internal class Resources {
private static global::System.Resources.ResourceManager resourceMan;

+ 3
- 0
RegexRenamer/Properties/Settings.Designer.cs View File

@@ -8,11 +8,14 @@
// </auto-generated>
//------------------------------------------------------------------------------
using System.Diagnostics.CodeAnalysis;
namespace RegexRenamer.Properties {
[global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "10.0.0.0")]
[ExcludeFromCodeCoverage]
internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase {
private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings())));

+ 178
- 113
RegexRenamer/RegexRenamerCore.cs View File

@@ -1,116 +1,181 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using NCalc;

namespace RegexRenamer
{
public class RegexRenamerCore
{

private string _currentPath;
public string CurrentPath
{
get { return _currentPath; }
set { _currentPath = value; }
}

private string _currentPattern;
public string CurrentPattern
{
get { return _currentPattern; }
set { _currentPattern = value; }
}

private static RegexRenamerCore _instance;

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using NCalc;
namespace RegexRenamer
{
public class RegexRenamerCore
{
private IEnumerable<FileItem> _items;
private string _currentPath;
public string CurrentPath
{
get { return _currentPath; }
}
private string _currentPattern;
public string CurrentPattern
{
get { return _currentPattern; }
}
private static RegexRenamerCore _instance;
private RegexRenamerCore()
{
}

public static RegexRenamerCore Instance(bool newInstance = false)
{
if (newInstance || _instance == null)
{
_instance = new RegexRenamerCore();
}
return _instance;
}

public IEnumerable<ListViewItem> ScanForFiles(bool recursive = false)
{
if (Directory.Exists(_currentPath))
{
_currentPattern = "*.*";
}
else
{
_currentPath = Path.GetDirectoryName(_currentPath);
_currentPattern = Path.GetFileName(_currentPath);
}

SearchOption option = recursive ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly;

IEnumerable<ListViewItem> items =
new DirectoryInfo(_currentPath).GetFiles(_currentPattern, option).Select(
fi => new ListViewItem(fi.FullName.Replace(_currentPath + @"\", ""))).ToList();
return items;

}

public List<ListViewItem> PreviewRename(List<ListViewItem> items, string pattern, string replacement, bool evaluate)
{
foreach (ListViewItem item in items)
{
string result = item.Text;
result = Regex.Replace(result, pattern, replacement);

if (evaluate)
{
MatchCollection expressions = Regex.Matches(result, "<(.+?)>");
foreach (Match m in expressions)
{
Expression expression = new Expression(m.Groups[1].Value);
var expressionResult = expression.Evaluate();
result = result.Replace(m.Groups[0].Value, Convert.ToString(expressionResult));
}
}
if (item.SubItems.Count > 1)
{
item.SubItems[1].Text = result;
}
else
{
item.SubItems.Add(result);
}
}
return items;
}

public void RenameFiles(List<ListViewItem> items)
{
foreach (ListViewItem item in items)
{
string source = Path.Combine(_currentPath, item.Text);
string dest = item.SubItems[1].Text;
if (!Directory.Exists(Path.GetDirectoryName(dest)))
{
dest = Path.Combine(_currentPath, dest);
}
if (!string.Equals(source, dest))
{
string destinationPath = Path.GetDirectoryName(dest);
if (!Directory.Exists(destinationPath))
{
Directory.CreateDirectory(destinationPath);
}
File.Move(source, dest);
}
}
}
}
}
}
public static RegexRenamerCore Instance(bool newInstance = false)
{
if (newInstance || _instance == null)
{
_instance = new RegexRenamerCore();
}
return _instance;
}
public IEnumerable<FileItem> ScanFiles(string path, bool recursive = false)
{
_currentPath = path;
if (Directory.Exists(_currentPath))
{
_currentPattern = "*.*";
}
else
{
_currentPattern = Path.GetFileName(_currentPath);
_currentPath = Path.GetDirectoryName(_currentPath);
}
var option = recursive ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly;
if (_currentPath != null && _currentPattern != null)
{
_items = new DirectoryInfo(_currentPath).GetFiles(_currentPattern, option)
.Select(fi => new FileItem(fi.FullName.Replace(_currentPath + @"\", ""), fi.FullName));
}
return _items;
}
public IEnumerable<FileItem> PreviewRename(string pattern, string replacement)
{
ValidateRegularExpression(pattern);
var newItems = new List<FileItem>();
foreach (var item in _items)
{
var result = Regex.Replace(item.DisplayName, pattern, replacement);
var leftExpressionMarkers = replacement.ToCharArray().Count(c => c == '<');
var rightExpressionMarkers = replacement.ToCharArray().Count(c => c == '>');
if (leftExpressionMarkers != rightExpressionMarkers)
{
throw new ArgumentException("The replacement string must have balanced expression markers.");
}
if (leftExpressionMarkers > 0)
{
var expressions = Regex.Matches(result, "<(.+?)>");
foreach (Match m in expressions)
{
var match = m.Groups[0].Value;
var expressionMatch = m.Groups[1].Value;
string format = null;
string expressionString = "";
if (expressionMatch.Contains(":"))
{
var parts = expressionMatch.Split(':');
expressionMatch = parts[0];
var expression = new Expression(expressionMatch);
format = parts[1];
var expressionResult = Convert.ToInt32(expression.Evaluate());
expressionString = expressionResult.ToString(format);
}
else
{
var expression = new Expression(expressionMatch);
var expressionResult = Convert.ToInt32(expression.Evaluate());
expressionString = expressionResult.ToString();
}
result = result.Replace(match, expressionString);
}
}
item.Renamed = result;
newItems.Add(item);
}
_items = newItems;
return _items;
}
private void ValidateRegularExpression(string pattern)
{
if (!pattern.IsRegularExpression())
{
throw new ArgumentException("The specified regular expression is invalid.");
}
}
public void RenameFiles()
{
if (!_items.Any())
{
return;
}
foreach (var item in _items)
{
if (string.IsNullOrEmpty(item.Renamed))
{
continue;
}
var source = Path.Combine(_currentPath, item.DisplayName);
var dest = Path.Combine(_currentPath, item.Renamed);
if (string.Equals(source, dest))
{
continue;
}
var destinationPath = Path.GetDirectoryName(dest);
if (destinationPath != null)
{
if (!Directory.Exists(destinationPath))
{
Directory.CreateDirectory(destinationPath);
}
File.Move(source, dest);
}
}
}
}
public class FileItem
{
public string DisplayName => _displayName;
public string FullPath => _fullPath;
public string Renamed
{
get { return _renamed; }
set { _renamed = value; }
}
private readonly string _displayName;
private readonly string _fullPath;
private string _renamed;
public FileItem(string displayName, string fullPath)
{
_displayName = displayName;
_fullPath = fullPath;
}
}
}

+ 3
- 0
RegexRenamer/Resources.Designer.cs View File

@@ -8,6 +8,8 @@
// </auto-generated>
//------------------------------------------------------------------------------
using System.Diagnostics.CodeAnalysis;
namespace RegexRenamer {
using System;
@@ -22,6 +24,7 @@ namespace RegexRenamer {
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "4.0.0.0")]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
[global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
[ExcludeFromCodeCoverage]
internal class Resources {
private static global::System.Resources.ResourceManager resourceMan;

Loading…
Cancel
Save