D
D
Dmitry2018-12-05 06:14:50
C++ / C#
Dmitry, 2018-12-05 06:14:50

How to rename files in a folder alphabetically?

There is a folder with files:
(1).pdf
(2).pdf
(10).pdf
(20).pdf There is a .txt file
with content:
1.003.1924.003.4
1.003.1924.004.4
1.003.1924.005.4
.4
It is necessary to rename the files in the folder in accordance with the .txt
file. I have implemented the renaming of files, I will give the code below. But there's a problem. On Windows 10, renames files in "alphabetical order", i.e. (1).pdf->(2).pdf->(10).pdf->(20).pdf And on Windows 7 "alphabetical order" is considered different: (1).pdf->(10).pdf- >(2).pdf->(20).pdf
To fix this, you can add leading zeros, i.e. we will get files:
(001).pdf
(002).pdf
(010).pdf
(020).pdf
Then he perceives the "alphabetical order" correctly. I guess we need to add a method to check the file name through regular expressions and change it when it only writes files to the array.
Please tell me how to do it right. Or if there is a more rational way out of this situation, I will listen with pleasure. Thanks in advance.

Program code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Text.RegularExpressions;

namespace Переименование_файлов
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        public string [] files; //пути всех файлов
        public string file; //путь файла .txt

        private void КнопкаОбзор_Click(object sender, EventArgs e) //вызов окна для выбора папки
        {
            DialogResult result = ОкноОбзорПапка.ShowDialog();
            if (result == DialogResult.OK)
            {
                ПутьПапка.Text = ОкноОбзорПапка.SelectedPath;
                files = Directory.GetFiles(ОкноОбзорПапка.SelectedPath); //получаем файлы
                files.Select(fn => new FileInfo(fn)).OrderBy(f => f.Name); //сортируем в "алфавитном порядке"
            }
        }

        private void КнопкаОбзорФайл_Click(object sender, EventArgs e) //вызов окна для выбора файла
        {
            DialogResult result = ОкноОбзорФайл.ShowDialog();
            if (result == DialogResult.OK)
            {
                ПутьФайл.Text = ОкноОбзорФайл.FileName;
                file = ПутьФайл.Text;
            }
        }

        private void КнопкаПереименовать_Click(object sender, EventArgs e) //метод переименования
        {
            string[] arStr = File.ReadAllLines(file); //читаем файл построчно

            int ind = 0;
            for (int i = 0; i < files.Length; i++)
            {
                ind = files[i].LastIndexOf('\\');
                File.Move(files[i], files[i].Remove(ind + 1) + arStr[i] + ".pdf"); //переименовываем файлы
            }
            MessageBox.Show("Готово!");
        }
    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
F
freeExec, 2018-12-05
@DmitryKyd

You actually blew the result of this sorting
And you work with the order in which they are created in the catalog (i.e. randomly).

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question