吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 118|回复: 2
收起左侧

[经验求助] 定向悬赏@Broadm

[复制链接]
dreamlife1979 发表于 2024-11-5 16:50
80吾爱币
感谢Broadm兄,重新开贴

最佳答案

查看完整内容

C#写的, 源码如下: [mw_shl_code=csharp,true]using System; using System.Collections.Generic; using System.IO; using System.Linq; namespace Tools { class BookTableItem { public string id { get; set; } public string type { get; set; } public string name { get; set; } public string bookNum { get; set; } public string isbn { get; set; } ...

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

Broadm 发表于 2024-11-5 16:50
Broadm 发表于 2024-11-5 17:10
没事啊, 能帮到就行

C#写的, 源码如下:

[C#] 纯文本查看 复制代码
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

namespace Tools
{
    class BookTableItem
    {
        public string id { get; set; }
        public string type { get; set; }
        public string name { get; set; }
        public string bookNum { get; set; }
        public string isbn { get; set; }
        public string imageUrl { get; set; }
        public string modifyTime { get; set; }
        public string createTime { get; set; }
        public string authorNames { get; set; }

        public override string ToString()
        {
            return $"{id}, {type}, {name}, {bookNum}, {isbn}, {modifyTime}, {createTime}, {authorNames}";
        }
    }

    class Program
    {
        static void Main()
        {
            var exeDir = AppDomain.CurrentDomain.BaseDirectory;
            var bookTablePath = Path.Combine(exeDir, "书表.txt");
            var invalidPathChars = Path.GetInvalidPathChars();

            try
            {
                Console.WriteLine("开始读取书表...");
                var bookTableItems = ReadBookTable(bookTablePath);
                Console.WriteLine("成功读取书表...");

                var filenames = Directory.GetFiles(exeDir);
                foreach (var filename in filenames)
                {
                    if (filename.IndexOf("书表", StringComparison.OrdinalIgnoreCase) != -1)
                    {
                        continue;
                    }
                    if (filename.IndexOf("Tools.exe", StringComparison.OrdinalIgnoreCase) != -1)
                    {
                        continue;
                    }

                    var nameWithExt = Path.GetFileName(filename);
                    var pureName = Path.GetFileNameWithoutExtension(nameWithExt);
                    var ext = nameWithExt.Replace(pureName, "");

                    if (int.TryParse(pureName, out var _))
                    {
                        var item = bookTableItems.FirstOrDefault(a => a.bookNum == pureName);
                        if (item != null)
                        {
                            var destPureName =
                                $"{item.createTime.Substring(0, 4)}_{item.name}_{item.isbn}";
                            var destFileName = destPureName + ext;
                            try
                            {
                                File.Move(filename, Path.Combine(exeDir, destFileName));
                                Console.WriteLine($"处理成功: {nameWithExt} => {destFileName}");
                            }
                            catch (Exception ex)
                            {
                                Console.ForegroundColor = ConsoleColor.Red;
                                Console.WriteLine(ex.Message);
                                Console.WriteLine($"处理失败: {nameWithExt} => {destFileName}");
                                Console.ResetColor();
                            }
                        }
                    }
                }

                Console.WriteLine("处理完毕...");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

            Console.ReadKey();
        }

        private static List<BookTableItem> ReadBookTable(string bookTablePath)
        {
            if (!File.Exists(bookTablePath))
            {
                throw new Exception("未找到 书表.txt");
            }

            string[] lines = File.ReadAllLines(bookTablePath);
            if (lines == null || lines.Length == 0)
            {
                throw new Exception("书表没有内容");
            }

            var BookTableItems = new List<BookTableItem>(lines.Length);

            int lineNumber = 0;

            //id,type,name,bookNum,isbn,imageUrl,modifyTime,createTime,authorNames

            foreach (string line in lines)
            {
                lineNumber++;
                if (lineNumber == 1 || line.Contains(",,,") || line.Length < 9)
                {
                    continue;
                }

                //id
                var prevIndex = 0;
                var curIndex = line.IndexOf(',', prevIndex);
                string id = line.Substring(prevIndex, curIndex - prevIndex);

                //type
                prevIndex = curIndex + 1;
                curIndex = line.IndexOf(',', prevIndex);
                string type = line.Substring(prevIndex, curIndex - prevIndex);

                //name
                string name = "";
                {
                    prevIndex = curIndex + 1;
                    curIndex = line.IndexOf(',', prevIndex);
                    //查找bookNum前面的逗号
                    while (curIndex + 1 < line.Length && char.IsDigit(line[curIndex + 1]) == false)
                    {
                        curIndex = line.IndexOf(',', curIndex + 1);
                    }
                    name = line.Substring(prevIndex, curIndex - prevIndex);
                }

                //bookNum
                prevIndex = curIndex + 1;
                curIndex = line.IndexOf(',', prevIndex);
                string bookNum = line.Substring(prevIndex, curIndex - prevIndex);

                //isbn
                prevIndex = curIndex + 1;
                curIndex = line.IndexOf(',', prevIndex);
                string isbn = line.Substring(prevIndex, curIndex - prevIndex);

                //imageUrl
                prevIndex = curIndex + 1;
                curIndex = line.IndexOf(',', prevIndex);
                string imageUrl = line.Substring(prevIndex, curIndex - prevIndex);

                //modifyTime
                prevIndex = curIndex + 1;
                curIndex = line.IndexOf(',', prevIndex);
                string modifyTime = line.Substring(prevIndex, curIndex - prevIndex);

                //createTime
                prevIndex = curIndex + 1;
                curIndex = line.IndexOf(',', prevIndex);
                string createTime = line.Substring(prevIndex, curIndex - prevIndex);

                //authorNames
                string authorNames = "";
                prevIndex = curIndex + 1;
                curIndex = line.IndexOf(',', prevIndex);
                if (curIndex != -1)
                {
                    authorNames = line.Substring(prevIndex, curIndex - prevIndex);
                }

                if (name.IndexOf(":") != -1)
                {
                    name = name.Replace(":", ":");
                }
                if (name.IndexOf("/") != -1)
                {
                    name = name.Replace("/", "");
                }
                if (name.IndexOf("\"") != -1)
                {
                    name = name.Replace("\"", "");
                }

                var item = new BookTableItem()
                {
                    id = id,
                    type = type,
                    name = name,
                    bookNum = bookNum,
                    isbn = isbn,
                    imageUrl = imageUrl,
                    modifyTime = modifyTime,
                    createTime = createTime,
                    authorNames = authorNames,
                };

                BookTableItems.Add(item);
            }

            return BookTableItems;
        }
    }
}
Broadm 发表于 2024-11-5 17:10
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-11-24 23:54

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表