c# - 使用 txt 文件中的信息重命名一组文件
问题描述
我对如何根据 txt 文件中存在的信息来解决重命名一组 pdf 文件的问题有一些疑问。例如,假设在 txt 文件中,以下数据由制表符分隔:
"2222" "_" "Z1" "001" "E" "07"
"2222" "_" "C1" "002" "F" "08"
"2222" "_" "D1" "003" "F" "09"
给定文件夹中的 pdf 文件数始终对应于 txt 文件中的行数。如何使用 txt 文件中的此信息更改 * pdf 文件的名称,该文件位于 pdf 所在的同一文件夹中,不带引号?
解决方案
您可以使用以下代码作为起点,并在必要时使其更复杂。我有意见可以帮助跟进。请记住包括“使用 System.IO;”。
class Program
{
static void Main(string[] args)
{
string directory = @"E:\TempFiles\"; //Name of directory containing text files and PDFs
//Get text file with names for PDFs...
string filenames = File.ReadAllText(directory + "names.txt");
//Removed quotes, but can be done differently, and split by space, which may not work for all your cases, but gets going in the right direction...
string[] listFilenames = filenames.Replace("\"", "").Split('\t');
int i = 0; //Used to access list of filnames...
foreach (string file in Directory.GetFiles(directory))
{
//Skip text file...
if (!file.EndsWith(".txt"))
{
//Rename file...
File.Move(file, directory + listFilenames[i] + ".pdf");
i++;
}
}
}
}
推荐阅读
- django - Stripe - 为客户创建新卡
- python - 我是否应该记录一个函数的所有参数、异常和返回值,即使它们已经记录在其他函数中?(Python)
- python - (572.15 - 595.00),(3,692.05 - 3,757.00) 如果我想从字符串转换为浮点数,为什么会这样?
- c# - .Net Core 3.1 MVC 具有身份功能,通过 IIS 运行时产生 404 错误
- javascript - 每次我打开模式时都会多次触发 Ajax 调用
- python - 我必须仅使用 for 循环从列表中查找丢失的数字,一旦我认为您会理解,请查看代码
- python - 在熊猫中随条件变化
- python - Python 3 中的浮点减法是不准确的
- angular - 在模板中显式设置时指令输入属性“未定义”
- tensorflow - 将 TensorFlow 转换为 PyTorch