对于如何处理根据txt文件中存在的信息重命名一组pdf文件的问题,我有些疑问。例如,假设在txt文件中,以下数据由制表符分隔:
"2222" "_" "Z1" "001" "E" "07"
"2222" "_" "C1" "002" "F" "08"
"2222" "_" "D1" "003" "F" "09"
给定文件夹中pdf文件的数量始终与txt文件中的行数相对应。 如何使用pdf文件所在的txt文件(不带引号)中的此信息来更改* pdf文件的名称?
答案 0 :(得分:0)
您可以使用以下代码作为起点,并在必要时使其更复杂。我有意见以帮助跟进。记住要包括“ using 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++;
}
}
}
}