定向悬赏@Broadm
感谢Broadm兄,重新开贴 Broadm 发表于 2024-11-5 17:10没事啊, 能帮到就行
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) == 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;
}
}
}
没事啊, 能帮到就行:lol
页:
[1]