Upload files
Signed-off-by: Lev Rusanov <30170278+JDM170@users.noreply.github.com>
This commit is contained in:
103
Program.cs
Normal file
103
Program.cs
Normal file
@@ -0,0 +1,103 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
|
||||
namespace TextFileSplitter
|
||||
{
|
||||
class Program
|
||||
{
|
||||
static void SplitFile(string inputFile, int maxLines = 1023, string neededExt = null)
|
||||
{
|
||||
// Читаем все строки исходного файла
|
||||
string[] lines;
|
||||
try
|
||||
{
|
||||
lines = File.ReadAllLines(inputFile, Encoding.UTF8);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"Ошибка при чтении файла: {ex.Message}");
|
||||
return;
|
||||
}
|
||||
|
||||
// Если строк меньше или равно maxLines, ничего не делаем
|
||||
if (lines.Length <= maxLines)
|
||||
{
|
||||
Console.WriteLine($"Файл содержит {lines.Length} строк (не больше {maxLines}), разделение не требуется.");
|
||||
return;
|
||||
}
|
||||
|
||||
// Определяем базовое имя и расширение файла
|
||||
string baseName = Path.GetFileNameWithoutExtension(inputFile);
|
||||
string directory = Path.GetDirectoryName(inputFile);
|
||||
string ext = Path.GetExtension(inputFile);
|
||||
|
||||
if (!string.IsNullOrEmpty(neededExt))
|
||||
{
|
||||
ext = neededExt;
|
||||
}
|
||||
|
||||
// Если файл находится в корне диска, directory может быть null
|
||||
if (string.IsNullOrEmpty(directory))
|
||||
{
|
||||
directory = ".";
|
||||
}
|
||||
|
||||
// Разделяем файл на части
|
||||
int partNum = 1;
|
||||
for (int i = 0; i < lines.Length; i += maxLines)
|
||||
{
|
||||
// Формируем имя нового файла
|
||||
string outputFile = Path.Combine(directory, $"{baseName}_{partNum}{ext}");
|
||||
|
||||
// Определяем количество строк для текущей части
|
||||
int linesToTake = Math.Min(maxLines, lines.Length - i);
|
||||
string[] partLines = new string[linesToTake];
|
||||
Array.Copy(lines, i, partLines, 0, linesToTake);
|
||||
|
||||
// Записываем часть строк в новый файл
|
||||
try
|
||||
{
|
||||
File.WriteAllLines(outputFile, partLines, Encoding.UTF8);
|
||||
Console.WriteLine($"Создан файл {outputFile} с {partLines.Length} строками");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"Ошибка при записи файла {outputFile}: {ex.Message}");
|
||||
}
|
||||
|
||||
partNum++;
|
||||
}
|
||||
}
|
||||
|
||||
static void Main()
|
||||
{
|
||||
try
|
||||
{
|
||||
Console.Write("Введите путь к файлу для разделения: ");
|
||||
string inputFile = Console.ReadLine();
|
||||
|
||||
if (string.IsNullOrEmpty(inputFile))
|
||||
{
|
||||
Console.WriteLine("Путь к файлу не может быть пустым.");
|
||||
return;
|
||||
}
|
||||
|
||||
if (!File.Exists(inputFile))
|
||||
{
|
||||
Console.WriteLine("Файл не существует.");
|
||||
return;
|
||||
}
|
||||
|
||||
SplitFile(inputFile, neededExt: ".bat");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"Произошла ошибка: {ex.Message}");
|
||||
}
|
||||
|
||||
Console.WriteLine("Нажмите любую клавишу для выхода...");
|
||||
Console.ReadKey();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user