Permalink
Browse files

Initial commit

  • Loading branch information...
0 parents commit ab922a9e8ffdab4f418d587b62cd7d096a6353f2 @nicjansma committed Jan 30, 2012
Showing with 344 additions and 0 deletions.
  1. +20 −0 LICENSE
  2. +141 −0 Program.cs
  3. +41 −0 Properties/AssemblyInfo.cs
  4. +45 −0 README.md
  5. +97 −0 RenameRegex.csproj
  6. BIN RenameRegex.snk
20 LICENSE
@@ -0,0 +1,20 @@
+Copyright (c) 2012 Nic Jansma, http://nicj.net
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,141 @@
+// <copyright file="Program.cs" company="Nic Jansma">
+// Copyright (c) Nic Jansma 2012 All Right Reserved
+// </copyright>
+// <author>Nic Jansma</author>
+// <email>nic@nicj.net</email>
+namespace RenameRegex
+{
+ using System;
+ using System.Diagnostics;
+ using System.IO;
+ using System.Reflection;
+ using System.Text.RegularExpressions;
+
+ /// <summary>
+ /// RenameRegex command line program
+ /// </summary>
+ public static class Program
+ {
+ /// <summary>
+ /// Main command line
+ /// </summary>
+ /// <param name="args">Command line arguments</param>
+ /// <returns>0 on success</returns>
+ public static int Main(string[] args)
+ {
+ string nameSearch;
+ string nameReplace;
+ string fileMatch;
+ bool pretend;
+ if (!GetArguments(args, out fileMatch, out nameSearch, out nameReplace, out pretend))
+ {
+ Usage();
+ return 1;
+ }
+
+ // enumerate all files
+ string[] files = Directory.GetFiles(System.Environment.CurrentDirectory, fileMatch);
+
+ if (files.Length == 0)
+ {
+ Console.WriteLine(@"No files match!");
+ return 1;
+ }
+
+ string pretendModeNotification = pretend ? " (pretend)" : String.Empty;
+
+ //
+ // loop through each file, renaming via a regex
+ //
+ foreach (string fullFile in files)
+ {
+ // split into file and path
+ string fileName = Path.GetFileName(fullFile);
+ string fileDir = Path.GetDirectoryName(fullFile);
+
+ // rename via a regex
+ string fileNameAfter = Regex.Replace(fileName, nameSearch, nameReplace, RegexOptions.IgnoreCase);
+
+ // write what we changed (or would have)
+ if (fileName != fileNameAfter)
+ {
+ Console.WriteLine(@"{0} -> {1}{2}", fileName, fileNameAfter, pretendModeNotification);
+ }
+
+ // move file
+ if (!pretend && fileName != fileNameAfter)
+ {
+ try
+ {
+ File.Move(fileDir + @"\" + fileName, fileDir + @"\" + fileNameAfter);
+ }
+ catch (IOException)
+ {
+ Console.WriteLine(@"WARNING: Could note move {0} to {1}", fileName, fileNameAfter);
+ }
+ }
+ }
+
+ return 0;
+ }
+
+ /// <summary>
+ /// Gets the program arguments
+ /// </summary>
+ /// <param name="args">Command-line arguments</param>
+ /// <param name="fileMatch">File matching pattern</param>
+ /// <param name="nameSearch">Search expression</param>
+ /// <param name="nameReplace">Replace expression</param>
+ /// <param name="pretend">Whether or not to only show what would happen</param>
+ /// <returns>True if argument parsing was successful</returns>
+ private static bool GetArguments(
+ string[] args,
+ out string fileMatch,
+ out string nameSearch,
+ out string nameReplace,
+ out bool pretend)
+ {
+ // defaults
+ fileMatch = String.Empty;
+ nameSearch = String.Empty;
+ nameReplace = String.Empty;
+ pretend = false;
+
+ // check for all arguments
+ if (args == null || args.Length < 3 || args.Length > 4)
+ {
+ return false;
+ }
+
+ // set from arguments
+ fileMatch = args[0];
+ nameSearch = args[1];
+ nameReplace = args[2];
+ pretend = false;
+
+ if (args.Length == 4 && args[3].Equals("/p", StringComparison.OrdinalIgnoreCase))
+ {
+ pretend = true;
+ }
+
+ return true;
+ }
+
+ /// <summary>
+ /// Program usage
+ /// </summary>
+ private static void Usage()
+ {
+ // get the assembly version
+ Assembly assembly = Assembly.GetExecutingAssembly();
+ FileVersionInfo fvi = FileVersionInfo.GetVersionInfo(assembly.Location);
+ string version = fvi.ProductVersion;
+
+ Console.WriteLine(@"Rename Regex (RR) v{0} by Nic Jansma, http://nicj.net", version);
+ Console.WriteLine();
+ Console.WriteLine(@"Usage: RR file-match search replace [/p]");
+ Console.WriteLine(@" /p: pretend (show what will be renamed)");
+ return;
+ }
+ }
+}
@@ -0,0 +1,41 @@
+// <copyright file="AssemblyInfo.cs" company="Nic Jansma">
+// Copyright (c) Nic Jansma 2012 All Right Reserved
+// </copyright>
+// <author>Nic Jansma</author>
+// <email>nic@nicj.net</email>
+using System;
+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("RenameRegex")]
+[assembly: AssemblyDescription("")]
+[assembly: AssemblyConfiguration("")]
+[assembly: AssemblyCompany("")]
+[assembly: AssemblyProduct("RenameRegex")]
+[assembly: AssemblyCopyright("Copyright © Nic Jansma 2012")]
+[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("2804025c-e5ef-4e79-aa4b-1f48feccea9d")]
+
+// Version information for an assembly consists of the following four values:
+//
+// Major Version
+// Minor Version
+// Build Number
+// Revision
+//
+[assembly: AssemblyVersion("1.0.0.0")]
+[assembly: AssemblyFileVersion("1.0.0.0")]
+
+[assembly:CLSCompliant(true)]
@@ -0,0 +1,45 @@
+Copyright (c) 2012 Nic Jansma
+[http://nicj.net](http://nicj.net)
+
+Introduction
+------------
+
+RenameRegex (RR) is a Windows command-line bulk file renamer, using regular expressions. You can use it as a simple
+file renamer or with a complex regular expression for matching and replacement. See the Examples section for details.
+
+Usage
+------------
+
+ RR.exe file-match search replace [/p]
+ /p: pretend (show what will be renamed)
+
+You can use [.NET regular expressions](http://msdn.microsoft.com/en-us/library/hs600312.aspx) for the search and
+replacement strings, including [substitutions](http://msdn.microsoft.com/en-us/library/ewy2t5e0.aspx) (for example,
+"$1" is the 1st capture group in the search term).
+
+Examples
+------------
+
+Simple rename without a regular expression:
+
+ RR.exe * .ext1 .ext2
+
+Renaming with a replacement of all "-" characters to "_":
+
+ RR.exe * "-" "_"
+
+Remove all numbers from the file names:
+
+ RR.exe * "[0-9]+" ""
+
+Rename files in the pattern of "````124_xyz.txt````" to "````xyz_123.txt````":
+
+ RR.exe *.txt "([0-9]+)_([a-z]+)" "$2_$1"
+
+Version History
+---------------
+v1.0 - 2012-01-30: Initial release
+
+Credits
+-------
+Nic Jansma (http://nicj.net)
@@ -0,0 +1,97 @@
+<?xml version="1.0" encoding="utf-8"?>
+<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="4.0">
+ <PropertyGroup>
+ <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
+ <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
+ <ProductVersion>8.0.50727</ProductVersion>
+ <SchemaVersion>2.0</SchemaVersion>
+ <ProjectGuid>{16A6043E-7F40-476D-B063-F3F9A2351C36}</ProjectGuid>
+ <OutputType>Exe</OutputType>
+ <AppDesignerFolder>Properties</AppDesignerFolder>
+ <RootNamespace>RenameRegex</RootNamespace>
+ <AssemblyName>RR</AssemblyName>
+ <FileUpgradeFlags>
+ </FileUpgradeFlags>
+ <UpgradeBackupLocation>
+ </UpgradeBackupLocation>
+ <OldToolsVersion>3.5</OldToolsVersion>
+ <TargetFrameworkVersion>v2.0</TargetFrameworkVersion>
+ <PublishUrl>publish\</PublishUrl>
+ <Install>true</Install>
+ <InstallFrom>Disk</InstallFrom>
+ <UpdateEnabled>false</UpdateEnabled>
+ <UpdateMode>Foreground</UpdateMode>
+ <UpdateInterval>7</UpdateInterval>
+ <UpdateIntervalUnits>Days</UpdateIntervalUnits>
+ <UpdatePeriodically>false</UpdatePeriodically>
+ <UpdateRequired>false</UpdateRequired>
+ <MapFileExtensions>true</MapFileExtensions>
+ <ApplicationRevision>0</ApplicationRevision>
+ <ApplicationVersion>1.0.0.%2a</ApplicationVersion>
+ <IsWebBootstrapper>false</IsWebBootstrapper>
+ <UseApplicationTrust>false</UseApplicationTrust>
+ <BootstrapperEnabled>true</BootstrapperEnabled>
+ </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>
+ <CodeAnalysisRuleSet>ExtendedDesignGuidelineRules.ruleset</CodeAnalysisRuleSet>
+ </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>
+ <CodeAnalysisRuleSet>ExtendedDesignGuidelineRules.ruleset</CodeAnalysisRuleSet>
+ </PropertyGroup>
+ <PropertyGroup>
+ <SignAssembly>true</SignAssembly>
+ </PropertyGroup>
+ <PropertyGroup>
+ <AssemblyOriginatorKeyFile>RenameRegex.snk</AssemblyOriginatorKeyFile>
+ </PropertyGroup>
+ <ItemGroup>
+ <Reference Include="System" />
+ <Reference Include="System.Data" />
+ <Reference Include="System.Xml" />
+ </ItemGroup>
+ <ItemGroup>
+ <Compile Include="Program.cs" />
+ <Compile Include="Properties\AssemblyInfo.cs" />
+ </ItemGroup>
+ <ItemGroup>
+ <None Include="RenameRegex.snk" />
+ </ItemGroup>
+ <ItemGroup>
+ <BootstrapperPackage Include="Microsoft.Net.Client.3.5">
+ <Visible>False</Visible>
+ <ProductName>.NET Framework 3.5 SP1 Client Profile</ProductName>
+ <Install>false</Install>
+ </BootstrapperPackage>
+ <BootstrapperPackage Include="Microsoft.Net.Framework.3.5.SP1">
+ <Visible>False</Visible>
+ <ProductName>.NET Framework 3.5 SP1</ProductName>
+ <Install>true</Install>
+ </BootstrapperPackage>
+ <BootstrapperPackage Include="Microsoft.Windows.Installer.3.1">
+ <Visible>False</Visible>
+ <ProductName>Windows Installer 3.1</ProductName>
+ <Install>true</Install>
+ </BootstrapperPackage>
+ </ItemGroup>
+ <Import Project="$(MSBuildBinPath)\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>
Binary file not shown.

0 comments on commit ab922a9

Please sign in to comment.