这是一个用C#代码开发的控制台程序,
功能:用于将输入目录下所有的txt文件进行遍历,然后用每个txt文档非空的首行字符重命名txt文件。(*输入的不能带引号,可以直接从地址栏复制)
其他几点说明:
-
遍历txt时自动遍历非空的首行
-
使用正则表达式,将非法的文件名字符进行替换
-
采用UTF-8或ASCII的的编码选择
-
文件会自动存放到新生成的目录
-
文件自动加了原文件名的前缀,可以根据需要进行使用/取消
-
经过测试,从iPhone创建的UTF-8的Unix的txt文档也支持
-
采用ASCII编码时要注意只识别ASCII字符
-
以下是我堆的代码,欢迎大家相互讨论学习
void Main() { Console.WriteLine("输入目录,自动用txt不为空的首行重命名txt文件"); string txtPath = ""; Console.WriteLine("请输入txt文件所在目录:"); while (!Directory.Exists((txtPath = Console.ReadLine()))) { Console.WriteLine($codeholder_0amp;quot;目录:[{txtPath}]不存在,重新输入:"); } System.Text.Encoding CodeType = System.Text.Encoding.UTF8; string _codeType = ""; Console.WriteLine("请输入编码方式: 0 :ASCII, 1:UTF8"); while ((_codeType = Console.ReadLine()) != "0" && _codeType != "1" && _codeType != "2") { Console.WriteLine("请输入正确的编码方式: 0 :ASCII, 1 :UTF8"); } switch (_codeType) { case "0": CodeType = System.Text.Encoding.ASCII; break; case "1": CodeType = System.Text.Encoding.UTF8; break; //case "2": CodeType = System.Text.Encoding.Unicode; break; } string txtPathNew = txtPath + "\\txtRename\\";//重命名的文件存放到新目录 List<string> lsTxt = new List<string>(); string[] txtArry; if (!Directory.Exists(txtPathNew)) { Directory.CreateDirectory(txtPathNew); } txtArry = Directory.GetFiles(txtPath, "*.txt"); if (txtArry.Length > 0) { for (int i = 0; i < txtArry.Length; i++) { lsTxt.Add(txtArry[i]); } foreach (var item in lsTxt) { Console.WriteLine($codeholder_0amp;quot;原文件:\n{item.ToString()}"); // 判断文件是否存在 if (File.Exists(item)) { string firstLine = ""; int i = 0; // 打开文件并读取非空的首行文本(编码模式有UTF-8和ASCII可选) using (StreamReader reader = new StreamReader(item, CodeType)) { while ((firstLine = reader.ReadLine()) != null && string.IsNullOrWhiteSpace(firstLine)) { ++i; Console.WriteLine($codeholder_0amp;quot;第{i}行文本为空-{firstLine}"); } if (string.IsNullOrEmpty(firstLine)) { firstLine="txt文档为空-"; } //Console.WriteLine($codeholder_0amp;quot;文本行:{i}-{firstLine}"); //使用正则表达式验证并替换9个非法的文件名字符 string illegalChars = new string(Path.GetInvalidFileNameChars()) + new string(Path.GetInvalidPathChars()); Regex regex = new Regex(string.Format("[{0}]", Regex.Escape(illegalChars))); string safeFileName = regex.Replace(firstLine, ""); //判断首行字符的长度是否超过248,超过则截取 if (safeFileName.Length >= 248) { safeFileName = safeFileName.Substring(0, 246); } //如果重命名后的文件已存在,则加如日期时分秒毫秒后缀 if (File.Exists(txtPathNew + safeFileName + ".txt")) { safeFileName = safeFileName + DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss-fff"); } //重命名文件并存到生成的备份目录中 File.Copy(item, $codeholder_0amp;quot;{txtPathNew}{Path.GetFileNameWithoutExtension(item).ToString()}--{ safeFileName + ".txt"}"); Console.WriteLine($codeholder_0amp;quot;重命名后的文件:\n{txtPathNew} +{Path.GetFileNameWithoutExtension(item).ToString()} {safeFileName + ".txt"}"); } } else { Console.WriteLine("文件不存在!"); } } Console.WriteLine($codeholder_0amp;quot;即将打开目录:\n【{txtPathNew}】"); if (Directory.Exists(txtPathNew)) { Process.Start("explorer.exe", txtPathNew); } //Console.ReadLine(); } else { Console.WriteLine($codeholder_0amp;quot;{txtPath}中不存在txt文件"); } }