Visual Studio の Windows フォームで
「ドラッグ&ドロップしたフォルダのナンバリングを
一括で上げ下げするアプリケーション」を作成しました。
ナンバリングとは
「312_会社A」「313_会社B」「314_会社C」のように
先頭に ID を割り振るフォルダ整理の手法です。
今回は皆さんの役に立てればと思い
ソースと本体を公開したいと思います。
本体はこちら(BOOTH)
本体はこちら(GoogleDrive)
このアプリケーションの説明
「312_会社A」「313_会社B」「314_会社C」のように
ナンバリングでフォルダを管理している場合は
313 にこの会社を挟みたいという事があると思います。
そういった場合に
313 以降のフォルダ名を
手動で 1 個 1 個変えていくのは大変です。
そこでこのアプリケーションを使えば
ドラッグ&ドロップした全てのフォルダの
ID を上げたり下げたりできます。
100 番ごとに種別を変えているならば
400 番台が埋まりそうといった場面でも
500 番以降の全てのフォルダの ID を 100 上げる事で
対応することができます。
このアプリケーションはそういった場合に
ID を手動で 1 個 1 個
変えたくないという趣旨で作りました。
また、ドラック&ドロップしたフォルダのみが名称変更の対象なので
ID を変えたいフォルダのみをドラック&ドロップすることで
ID を変えたいフォルダのみを名称変更できます。
このアプリケーションのソースコード
Visual Studio の Windows フォームで作成しています。
ソースコードは下記の内容です
namespace SephirothFolderIDNexter
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
if (!File.Exists(Application.ExecutablePath.Substring(0, Application.ExecutablePath.LastIndexOf("\\")) + "\\CheckText.txt"))
{
File.WriteAllText(Application.ExecutablePath.Substring(0, Application.ExecutablePath.LastIndexOf("\\")) + "\\CheckText.txt", "1");
}
checkBox1.Checked = File.ReadAllText(Application.ExecutablePath.Substring(0, Application.ExecutablePath.LastIndexOf("\\")) + "\\CheckText.txt").Equals("1");
checkBox2.Checked = File.ReadAllText(Application.ExecutablePath.Substring(0, Application.ExecutablePath.LastIndexOf("\\")) + "\\CheckText.txt").Equals("2");
checkBox3.Checked = File.ReadAllText(Application.ExecutablePath.Substring(0, Application.ExecutablePath.LastIndexOf("\\")) + "\\CheckText.txt").Equals("3");
checkBox4.Checked = File.ReadAllText(Application.ExecutablePath.Substring(0, Application.ExecutablePath.LastIndexOf("\\")) + "\\CheckText.txt").Equals("4");
if(checkBox1.Checked || checkBox2.Checked)
{
label1.Text = "ここに ID を上げたいフォルダを全てドラッグ&ドロップして下さい。";
}
else
{
label1.Text = "ここに ID を下げたいフォルダを全てドラッグ&ドロップして下さい。";
}
if (!File.Exists(Application.ExecutablePath.Substring(0, Application.ExecutablePath.LastIndexOf("\\")) + "\\NumText.txt"))
{
File.WriteAllText(Application.ExecutablePath.Substring(0, Application.ExecutablePath.LastIndexOf("\\")) + "\\NumText.txt", "1");
}
textBox1.Text = File.ReadAllText(Application.ExecutablePath.Substring(0, Application.ExecutablePath.LastIndexOf("\\")) + "\\NumText.txt");
}
private void panel1_DragDrop(object sender, DragEventArgs e)
{
if (e.Data == null)
{
return;
}
object? data = e.Data.GetData(DataFormats.FileDrop, false);
if (data == null)
{
return;
}
string[] files1 = (string[])data;
List<string> resultPathList1 = new List<string>();
List<string> resultPathList2 = new List<string>();
for (int i = 0; i < files1.Length; i++)
{
if (!System.IO.Directory.Exists(files1[i]))
{
MessageBox.Show(
"ドラッグ&ドロップしたものにファイルが紛れているため、安全性確保のため処理を実行せず終了します。(フォルダのみをドラッグ&ドロップして下さい)", "確認",
MessageBoxButtons.OK, MessageBoxIcon.Question
);
return;
}
string resultFolderPath = files1[i].Substring(files1[i].LastIndexOf("\\") + 1);
if (checkBox1.Checked)
{
int searchIndex = 1;
try
{
while (true)
{
int.Parse(resultFolderPath.Substring(0, searchIndex));
searchIndex++;
}
}
catch
{
if (1 < searchIndex)
{
string targetNum1 = resultFolderPath.Substring(0, searchIndex - 1);
string targetNum2 = "" + (int.Parse(targetNum1) + int.Parse(textBox1.Text));
if (int.Parse(targetNum2) < 0)
{
targetNum2 = targetNum1;
}
while (targetNum2.Length < targetNum1.Length)
{
targetNum2 = "0" + targetNum2;
}
resultFolderPath = targetNum2 + resultFolderPath.Substring(searchIndex - 1);
}
else
{
continue;
}
}
}
else if (checkBox2.Checked)
{
int searchIndex = 1;
try
{
while (true)
{
int.Parse(resultFolderPath.Substring(resultFolderPath.Length - searchIndex));
searchIndex++;
}
}
catch
{
if (1 < searchIndex)
{
string targetNum1 = resultFolderPath.Substring(resultFolderPath.Length - searchIndex + 1);
string targetNum2 = "" + (int.Parse(targetNum1) + int.Parse(textBox1.Text));
if (int.Parse(targetNum2) < 0)
{
targetNum2 = targetNum1;
}
while (targetNum2.Length < targetNum1.Length)
{
targetNum2 = "0" + targetNum2;
}
resultFolderPath = resultFolderPath.Substring(0, resultFolderPath.Length - searchIndex + 1) + targetNum2;
}
else
{
continue;
}
}
}
else if (checkBox3.Checked)
{
int searchIndex = 1;
try
{
while (true)
{
int.Parse(resultFolderPath.Substring(0, searchIndex));
searchIndex++;
}
}
catch
{
if (1 < searchIndex)
{
string targetNum1 = resultFolderPath.Substring(0, searchIndex - 1);
string targetNum2 = "" + (int.Parse(targetNum1) - int.Parse(textBox1.Text));
if (int.Parse(targetNum2) < 0)
{
targetNum2 = targetNum1;
}
while (targetNum2.Length < targetNum1.Length)
{
targetNum2 = "0" + targetNum2;
}
resultFolderPath = targetNum2 + resultFolderPath.Substring(searchIndex - 1);
}
else
{
continue;
}
}
}
else
{
int searchIndex = 1;
try
{
while (true)
{
int.Parse(resultFolderPath.Substring(resultFolderPath.Length - searchIndex));
searchIndex++;
}
}
catch
{
if (1 < searchIndex)
{
string targetNum1 = resultFolderPath.Substring(resultFolderPath.Length - searchIndex + 1);
string targetNum2 = "" + (int.Parse(targetNum1) - int.Parse(textBox1.Text));
if (int.Parse(targetNum2) < 0)
{
targetNum2 = targetNum1;
}
while (targetNum2.Length < targetNum1.Length)
{
targetNum2 = "0" + targetNum2;
}
resultFolderPath = resultFolderPath.Substring(0, resultFolderPath.Length - searchIndex + 1) + targetNum2;
}
else
{
continue;
}
}
}
resultFolderPath = files1[i].Substring(0, files1[i].LastIndexOf("\\") + 1) + resultFolderPath;
resultPathList1.Add(files1[i]);
resultPathList2.Add(resultFolderPath);
}
if (resultPathList1.Count != resultPathList2.Count)
{
MessageBox.Show(
"エラーが発生したため、安全のため処理を実行せず終了します。", "確認",
MessageBoxButtons.OK, MessageBoxIcon.Question
);
return;
}
for (int i = 0; i < resultPathList2.Count; i++)
{
if (System.IO.Directory.Exists(resultPathList2[i]))
{
MessageBox.Show(
"変更しようとしているフォルダ名が既に存在するため、安全性確保のため処理を実行せず終了します。" + resultPathList2[i], "確認",
MessageBoxButtons.OK, MessageBoxIcon.Question
);
return;
}
}
for (int i = 0; i < resultPathList2.Count; i++)
{
System.IO.Directory.Move(resultPathList1[i], resultPathList2[i]);
}
if (0 < resultPathList2.Count)
{
MessageBox.Show(
"完了しました。", "確認",
MessageBoxButtons.OK, MessageBoxIcon.Question
);
}
else if(checkBox1.Checked || checkBox3.Checked)
{
MessageBox.Show(
"フォルダ名の先頭に数値が見つかりませんでしたので、安全性確保のため処理を実行せず終了します。", "確認",
MessageBoxButtons.OK, MessageBoxIcon.Question
);
}
else
{
MessageBox.Show(
"フォルダ名の末尾に数値が見つかりませんでしたので、安全性確保のため処理を実行せず終了します。", "確認",
MessageBoxButtons.OK, MessageBoxIcon.Question
);
}
}
private void panel1_DragEnter(object sender, DragEventArgs e)
{
if (e.Data == null)
{
return;
}
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
e.Effect = DragDropEffects.All;
}
else
{
e.Effect = DragDropEffects.None;
}
}
private void checkBox1_Click(object sender, EventArgs e)
{
checkBox1.Checked = true;
checkBox2.Checked = false;
checkBox3.Checked = false;
checkBox4.Checked = false;
File.WriteAllText(Application.ExecutablePath.Substring(0, Application.ExecutablePath.LastIndexOf("\\")) + "\\CheckText.txt", "1");
label1.Text = "ここに ID を上げたいフォルダを全てドラッグ&ドロップして下さい。";
}
private void checkBox2_Click(object sender, EventArgs e)
{
checkBox1.Checked = false;
checkBox2.Checked = true;
checkBox3.Checked = false;
checkBox4.Checked = false;
File.WriteAllText(Application.ExecutablePath.Substring(0, Application.ExecutablePath.LastIndexOf("\\")) + "\\CheckText.txt", "2");
label1.Text = "ここに ID を上げたいフォルダを全てドラッグ&ドロップして下さい。";
}
private void checkBox3_Click(object sender, EventArgs e)
{
checkBox1.Checked = false;
checkBox2.Checked = false;
checkBox3.Checked = true;
checkBox4.Checked = false;
File.WriteAllText(Application.ExecutablePath.Substring(0, Application.ExecutablePath.LastIndexOf("\\")) + "\\CheckText.txt", "3");
label1.Text = "ここに ID を下げたいフォルダを全てドラッグ&ドロップして下さい。";
}
private void checkBox4_Click(object sender, EventArgs e)
{
checkBox1.Checked = false;
checkBox2.Checked = false;
checkBox3.Checked = false;
checkBox4.Checked = true;
File.WriteAllText(Application.ExecutablePath.Substring(0, Application.ExecutablePath.LastIndexOf("\\")) + "\\CheckText.txt", "4");
label1.Text = "ここに ID を下げたいフォルダを全てドラッグ&ドロップして下さい。";
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
try
{
File.WriteAllText(Application.ExecutablePath.Substring(0, Application.ExecutablePath.LastIndexOf("\\")) + "\\NumText.txt", "" + int.Parse(textBox1.Text));
}
catch
{
textBox1.Text = File.ReadAllText(Application.ExecutablePath.Substring(0, Application.ExecutablePath.LastIndexOf("\\")) + "\\NumText.txt");
}
}
}
}
以上 Windows アプリケーションを公開しました。
Visual Studio の Windows フォームは
フォルダ整理をしたい時等に
補助用のアプリケーションを
簡単に作成する事ができます。
皆さんもフォルダ整理の際は
Visual Studio を活用してみては
いかがでしょうか?
皆さんの開発の助けになれますように
閲覧ありがとうございました。