LoginSignup
0
0

ログを分割する Windows アプリを無料公開してみる

Last updated at Posted at 2024-06-11

今回はログを分割する
Windows アプリを作成したため
無料公開してみようと思います。

ログファイルの容量が大きくて
閲覧が面倒な場合は
お試し頂けると幸いです。

ダウンロードはこちら


分割するおよその文字数を設定して
テキストファイルをドラッグ&ドロップして操作します。
image1.png

するとその文字数を超える最初の改行で
テキストファイルが分割されます。

例えば 100 万文字に設定した場合は
100 万文字目の次の改行で分割されます。

1GB を超えるログファイルを分割する際は
時間がかかると思われます。
その場合は気長にお待ちください。
その場合に短時間で分割する手段として
最初に 1000 万文字程度で分割して、
分割したファイルを更に 50 万文字程度で
分割する手段もあります。

使用したコードも合わせて載せておきます。
気になる方はご覧下さい。

使用したコード
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

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

            if(File.Exists(Application.ExecutablePath.Substring(0, Application.ExecutablePath.LastIndexOf("\\")) + "\\SaveSplitNum.txt")) {
                textBox1.Text = File.ReadAllText(Application.ExecutablePath.Substring(0, Application.ExecutablePath.LastIndexOf("\\")) + "\\SaveSplitNum.txt");
            }
            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_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 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 loadText = "";

                Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
                string utf8 = File.ReadAllText(files1[i]);
                string shift = File.ReadAllText(files1[i], System.Text.Encoding.GetEncoding(932));

                string utf8Replace = utf8.Replace("�", "");
                string shiftReplace = shift.Replace("�", "");

                int utf8Length = System.Text.Encoding.UTF8.GetByteCount(utf8);
                utf8Length += 12 * (utf8.Length - utf8Replace.Length); //shiftLength と同じ内容のペナルティ

                int shiftLength = System.Text.Encoding.UTF8.GetByteCount(shift);
                shiftLength += 12 * (shift.Length - shiftReplace.Length); //代用文字に対し 12 byte のペナルティ

                if (utf8Length < shiftLength)
                {
                    loadText = utf8;
                }
                else
                {
                    loadText = shift;
                }

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

                bool endFlg = false;
                while(!endFlg)
                {
                    endFlg = true;
                    for (int targetIndex = int.Parse(textBox1.Text); targetIndex < loadText.Length; targetIndex++)
                    {
                        if (("" + loadText[targetIndex]).Equals("\n"))
                        {
                            resultList.Add(loadText.Substring(0, targetIndex));
                            loadText = loadText.Substring(targetIndex, loadText.Length - targetIndex);
                            endFlg = false;
                            break;
                        }
                    }
                }
                resultList.Add(loadText);

                if(checkBox1.Checked)
                {
                    int last = fileName1.LastIndexOf(".");
                    File.WriteAllText(fileName1.Substring(0, last) + (resultList.Count) + fileName1.Substring(last, fileName1.Length - last), resultList[resultList.Count - 1]);
                }
                else
                {
                    for (int indexNumber = 0; indexNumber < resultList.Count; indexNumber++)
                    {
                        int last = fileName1.LastIndexOf(".");
                        File.WriteAllText(fileName1.Substring(0, last) + (indexNumber + 1) + fileName1.Substring(last, fileName1.Length - last), resultList[indexNumber]);
                    }
                }
            }

            MessageBox.Show(
                    "終了しました。", "確認",
                    MessageBoxButtons.OK, MessageBoxIcon.Question
                    );
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            try
            {
                File.WriteAllText(Application.ExecutablePath.Substring(0, Application.ExecutablePath.LastIndexOf("\\")) + "\\SaveSplitNum.txt", "" + int.Parse(textBox1.Text));
            }
            catch
            {

            }
        }

        private void label3_Click(object sender, EventArgs e)
        {

        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            File.WriteAllText(Application.ExecutablePath.Substring(0, Application.ExecutablePath.LastIndexOf("\\")) + "\\CheckText.txt", "" + checkBox1.Checked);
        }
    }
}

以上テキストファイルを分割する
Windows アプリケーションを公開してみました。

PowerSell で分割するのも面倒で
ネット上で個人的に良いのが見つからなかったため
ささっと作成してみました。

気に入って頂けると幸いです。
閲覧ありがとうございました。

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