LoginSignup
0
0

【フォルダ整理】フォルダ名の先頭の数値を取り除く Windows アプリケーションを公開してみる

Last updated at Posted at 2024-06-17

フォルダ名の先頭の数値を取り除きたいフォルダを
全てドラッグ&ドロップすることで取り除く事が出来る
Windows アプリケーションを作成したため
皆さんの役に立てればと思い
ソースと本体を公開したいと思います。

本体はこちら(BOOTH)
本体はこちら(Google Drive)


このアプリケーションの説明

フォルダ名の先頭の数値を取り除きたいフォルダを
全てドラッグ&ドロップすることで
取り除くことが出来ます。

image1.png
image2.png
image3.png

「サブディレクトリも対象にする」にチェックを入れた場合は
子要素孫要素も対象になります。

フォルダ名の先頭に
数値があるのが嫌いな人で
他の人から貰ったフォルダがそうなっている場合は
是非使用をご検討下さい。


このアプリケーションのソースコード

Visual Studio の Windows フォームで作成しています。
ソースコードは下記の内容です。

使用したコード
using System.Globalization;
using System.IO;
using System.IO.Compression;
using System.Security.Policy;
using System.Text;

namespace SephirothDeletePreNumberAtFolderName
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void panel1_Paint(object sender, PaintEventArgs e)
        {

        }

        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;
            }

            List<string> resultList1 = new List<string>();
            List<string> resultList2 = new List<string>();

            foreach (string one in (string[])data)
            {
                List<string> files1 = new List<string>();
                
                if(checkBox1.Checked)
                {
                    files1 = new List<string>(System.IO.Directory.GetDirectories(one, "*", System.IO.SearchOption.AllDirectories));
                }
                
                files1.Add(one);

                for (int i = 0; i < files1.Count; i++)
                {
                    string folderName1 = files1[i];

                    if (System.IO.Directory.Exists(folderName1))
                    {
                        string name = folderName1.Substring(folderName1.LastIndexOf("\\") + 1);

                        int target = 1;

                        try
                        {
                            while (true)
                            {
                                int.Parse(name.Substring(0, target));
                                target++;
                            }
                        }
                        catch
                        {
                        }

                        name = name.Substring(target - 1);

                        if (name.StartsWith("_"))
                        {
                            name = name.Substring(1);
                        }

                        string resultPath = folderName1.Substring(0, folderName1.LastIndexOf("\\") + 1) + name;

                        if (!folderName1.Equals(resultPath))
                        {
                            resultList1.Add(folderName1);
                            resultList2.Add(resultPath);
                        }
                    }
                }
            }

            

            if(resultList1.Count != resultList2.Count)
            {
                MessageBox.Show(
                        "エラーが発生したため、安全のため処理を何もしないで終了しました。", "確認",
                        MessageBoxButtons.OK, MessageBoxIcon.Question
                        );
                return;
            }

            foreach(string one1 in resultList1)
            {
                foreach(string one2 in resultList2)
                {
                    if(one1.Equals(one2))
                    {
                        MessageBox.Show(
                        "名称変更前と名称変更後のファイル名が同じものが存在するため、安全のため処理を何もしないで終了しました。", "確認",
                        MessageBoxButtons.OK, MessageBoxIcon.Question
                        );
                        return;
                    }
                }
            }

            for(int i = 0; i < resultList2.Count; i++)
            {
                for(int j = i + 1; j < resultList2.Count; j++)
                {
                    if (resultList2[i].Equals(resultList2[j]))
                    {
                        MessageBox.Show(
                        "数値を取り除いた結果の名称が重複する箇所が存在したため、安全のため処理を何もしないで終了しました。", "確認",
                        MessageBoxButtons.OK, MessageBoxIcon.Question
                        );
                        return;
                    }
                }
            }

            for (int i = 0; i < resultList2.Count; i++)
            {
                System.IO.Directory.Move(resultList1[i], resultList2[i]);
            }

            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;
            }
        }
    }
}

以上 Windows アプリケーションを公開しました。

Visual Studio の Windows フォームは
フォルダ整理をしたい時等に
補助用のアプリケーションを
簡単に作成する事ができます。

皆さんもフォルダ整理の際は
Visual Studio を活用してみては
いかがでしょうか?

皆さんの開発の助けになれますように
閲覧ありがとうございました。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0