過去のブログのアーカイブ
この記事は前身のブログのアーカイブを引き継いだものです.
画像が正しく表示できないなど,コンテンツの表示に問題がある恐れがあります.ファイル/フォルダの名前を細かく変えたいと思った方はいると思います。ついでに私はそんな経験ないです。
もしものことがあるったらと思いプログラムを作ってみました。

仕様
先に仕様を言っておきますと、正規表現の種類は.NET準拠、動作には.NET Frameworkが必要です。
正規表現でファイル/フォルダの名前を一発で変更、サブディレクトリは変更されませんがソースコードは公開するので調整したければご自由にどうぞ。
ここまで低機能だと改造するより新しく作りなおしたほうが速いかもしれませんが。
ダウンロード
バイナリファイルのみ
[wpdm_file id=19]
ソースコード
[wpdm_file id=20]
どちらもソースコードはこちらでお願いします。
http://yryr.me/works/license
使い方
正規表現は.NET準拠です。検索の場合は検索パターンを、リネームする場合は置き換えパターンを指定してください。
動作はrename.batまたはsearch.batで行ってください。
ひどい投げやりですね…
ソースコード
using System;
using System.IO;
using System.Text.RegularExpressions;
namespace NameChanger
{
class Program
{
static void Main(string[] args)
{
string folder = "";
string pre = "";
string to = "";
bool com_flag = false; string com_name = "";
Action<string, string> addParam = new Action<string, string>((name, value) =>
{
switch (name.ToLower())
{
case "path":
folder = value;
break;
case "pattern":
pre = value;
break;
case "to":
to = value;
break;
default:
throw new NotSupportedException(string.Format("'{0}'はサポートしていません", name));
}
});
for (int i = 0; i < args.Length; i++)
{
string str = args[i];
if (!com_flag && str.IndexOf(':') != -1)
{
int index = str.IndexOf(':');
com_name = str.Substring(0, index);
addParam(com_name, str.Substring(index + 1));
com_name = ""; com_flag = false;
}
else if (com_flag)
{
addParam(com_name, str);
com_name = ""; com_flag = false;
}
else
{
com_name = str;
com_flag = true;
}
}
// 列挙するだけ
if (string.IsNullOrEmpty(to))
{
Console.WriteLine("パターンに合致したフォルダ: ");
foreach (var di in (new DirectoryInfo(folder)).GetDirectories())
{
if (Regex.IsMatch(di.Name, pre))
Console.WriteLine(" " + di.Name);
}
Console.WriteLine("パターンに合致したファイル: ");
foreach (var fi in (new DirectoryInfo(folder)).GetFiles())
{
if (Regex.IsMatch(fi.Name, pre))
Console.WriteLine(" " + fi.Name);
}
}
else
{
int w = (int)Math.Floor((float)Console.BufferWidth / 2 - 1);
foreach (var di in (new DirectoryInfo(folder)).GetDirectories())
{
if (Regex.IsMatch(di.Name, pre))
{
string dt = Regex.Replace(di.Name, pre, to);
string moto_name = di.Name;
di.MoveTo(di.Parent.FullName + "\\" + dt);
Console.Write("{0:" + w + "}", moto_name);
Console.Write("=>");
Console.Write("{0:" + w + "}", dt);
Console.WriteLine();
}
}
foreach (var fi in (new DirectoryInfo(folder)).GetFiles())
{
if (Regex.IsMatch(fi.Name, pre))
{
string dt = Regex.Replace(fi.Name, pre, to);
string moto_name = fi.Name;
fi.MoveTo(fi.Directory.FullName + "\\" + dt);
Console.Write("{0:" + w + "}", moto_name);
Console.Write("=>");
Console.Write("{0:" + w + "}", dt);
Console.WriteLine();
}
}
}
}
}
}


