LoginSignup
0
0

【フォルダ整理】フォルダ内のファイルを拡張子ごとに分別する Windows アプリケーションを無料公開してみる

Last updated at Posted at 2024-06-14

ドラッグ&ドロップしたフォルダの
中にあるファイルを拡張子ごとに分類する
Windows アプリケーションを作成したため
皆さんの役に立てればと思い
ソースと本体を公開したいと思います。

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


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

ドラッグ&ドロップしたフォルダの
中にあるファイルを拡張子ごとに分類します。

image1.png
image2.png
image3.png
image4.png

「このフォルダの中のエクセルのデータを
日付順にソートして一覧表示したい」といった場合に
このアプリケーションを使うことで
エクセルのデータだけを
1 個のフォルダに収める事が出来ます。

エクスプローラーの検索で検索すると
少し見づらいと私個人的には感じたため
拡張子で検索したい場合のみ
このアプリケーションを活用しています。

拡張子ごとに分類するため
ドラッグ&ドロップしたフォルダに
どんな拡張子が含まれているかも
一覧で分かります。

また、

ドラッグ&ドロップしたフォルダ - フォルダA - ファイルB

のようにフォルダの中にフォルダがある場合も
その中身まで分類の対象になります。
つまり孫要素も分類の対象になります。

他の人から貰ったフォルダが
乱雑に配置されていて見にくい場合は
是非ご活用頂けると幸いです。


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

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

使用したコード
using System.IO.Compression;

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

            if (File.Exists(Application.ExecutablePath.Substring(0, Application.ExecutablePath.LastIndexOf("\\")) + "\\CheckText.txt"))
            {
                checkBox1.Checked = File.ReadAllText(Application.ExecutablePath.Substring(0, Application.ExecutablePath.LastIndexOf("\\")) + "\\CheckText.txt").Equals("True");
            }
        }

        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;

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

                string createFolderPath = fileName1 + "_Organization";
                System.IO.Directory.CreateDirectory(createFolderPath);

                string[] files2 = Directory.GetFiles(fileName1, "*", System.IO.SearchOption.AllDirectories);

                List<string> extensionList = new List<string>();

                for (int j = 0; j < files2.Length; j++)
                {
                    string files2Extension = System.IO.Path.GetExtension(files2[j]);

                    if (files2Extension == "")
                    {
                        files2Extension = "null";
                    }

                    bool isContain = false;
                    foreach (string oneExtension in extensionList)
                    {
                        if (files2Extension.Equals(oneExtension))
                        {
                            isContain = true;
                            break;
                        }
                    }
                    if (!isContain)
                    {
                        extensionList.Add(files2Extension);
                        System.IO.Directory.CreateDirectory(createFolderPath + "\\" + files2Extension);
                    }
                }

                for (int j = 0; j < files2.Length; j++)
                {
                    if (checkBox1.Checked)
                    {
                        string files2Extension = System.IO.Path.GetExtension(files2[j]);

                        if (files2Extension == "")
                        {
                            files2Extension = "null";
                        }

                        string rootFolderName = files2[j].Substring(0, files2[j].LastIndexOf("\\"));
                        rootFolderName = rootFolderName.Substring(rootFolderName.LastIndexOf("\\") + 1);

                        if (!System.IO.Directory.Exists(createFolderPath + "\\" + files2Extension + "\\" + rootFolderName))
                        {
                            System.IO.Directory.CreateDirectory(createFolderPath + "\\" + files2Extension + "\\" + rootFolderName);
                        }

                        string distPath = createFolderPath + "\\" + files2Extension + "\\" + rootFolderName + "\\" + System.IO.Path.GetFileName(files2[j]);

                        for (int searchIndex = 0; true; searchIndex++)
                        {
                            if (0 < searchIndex)
                            {
                                distPath = createFolderPath + "\\" + "\\" + files2Extension + "\\" + rootFolderName + "\\" + System.IO.Path.GetFileNameWithoutExtension(files2[j]) + searchIndex + files2Extension;
                            }
                            else
                            {
                                distPath = createFolderPath + "\\" + files2Extension + "\\" + rootFolderName + "\\" + System.IO.Path.GetFileName(files2[j]);
                            }
                            if (!System.IO.File.Exists(distPath))
                            {
                                break;
                            }
                        }

                        System.IO.File.Copy(files2[j], distPath);
                    }
                    else
                    {
                        string files2Extension = System.IO.Path.GetExtension(files2[j]);

                        if (files2Extension == "")
                        {
                            files2Extension = "null";
                        }

                        string distPath = createFolderPath + "\\" + files2Extension + "\\" + System.IO.Path.GetFileName(files2[j]);

                        for (int searchIndex = 1; true; searchIndex++)
                        {
                            if (0 < searchIndex)
                            {
                                distPath = createFolderPath + "\\" + "\\" + files2Extension + "\\" + System.IO.Path.GetFileNameWithoutExtension(files2[j]) + searchIndex + files2Extension;
                            }
                            else
                            {
                                distPath = createFolderPath + "\\" + files2Extension + "\\" + System.IO.Path.GetFileName(files2[j]);
                            }
                            if (!System.IO.File.Exists(distPath))
                            {
                                break;
                            }
                        }

                        System.IO.File.Copy(files2[j], distPath);
                    }
                }
            }

            MessageBox.Show(
                    "仕分けが完了しました。", "確認",
                    MessageBoxButtons.YesNo, 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_CheckedChanged(object sender, EventArgs e)
        {
            File.WriteAllText(Application.ExecutablePath.Substring(0, Application.ExecutablePath.LastIndexOf("\\")) + "\\CheckText.txt", "" + checkBox1.Checked);
        }
    }
}

以上 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