将多个文本文件附加到单个文件中并重命名 [英] Append multiple text files into single file and rename it
问题描述
你好我在一个地方有多个文本文件,它们就像下面这个名字:
A_L_PRTD_021345.txt,A_L_PRTD_432124,就像这样..
我想将它们合并为单个文件,并希望将单个文件重命名为L_PRTD_Currentdate> .txt
怎么能有可能吗?请与我分享一些做这个的鳕鱼
使用System.IO.StreamReader / StreamWriter读取/写入文本文件。
例如...
使用 System.IO;
DirectoryInfo di = new DirectoryInfo( <包含目录的路径>);
FileInfo [] files = di.GetFiles();
string data = 跨度>;
foreach (FileInfo fi in files)
{
if (fi.Name.ToUpper()。StartsWith( A_L_PRTD))
{
StreamReader rd = new StreamReader(fi.FullName);
data + = rd.ReadToEnd();
rd.Close();
rd.Dispose();
// 可选...
File.Delete(fi.FullName) ;
}
}
StreamWriter wr = new StreamWriter( string .Format( L_PRTD_ {0} .txt,DateTime.Now.ToString(< span class =code-string> ddMMyyyy)));
wr.Write(data);
wr.Close();
wr.Dispose();
试试这样..
string path = d://文件//跨度>;
// string [] fileNames = {A_L_PRTD_432124.txt,A_L_PRTD_021345.txt};
string fileNameStartsWIth = A_L_PRTD_ 跨度>;
string [] fileNames = Directory
.GetFiles(path, * .txt,SearchOption.AllDirectories)
.Select(f = > Path.GetFileName( f))。其中(k = > k.StartsWith(fileNameStartsWIth))。ToArray();
string finalFile = L_PRTD _ + DateTime.Now.ToString( ddMMMyyyy)+ 。txt;
string contentSeperator = Environment.NewLine + -------------------------------------------------- --- + Environment.NewLine;
foreach (字符串文件 in fileNames)
{
string content = File.ReadAllText(path + file)+ contentSeperator;
File.AppendAllText(path + finalFile,content);
}
参考:
文件.ReadAllText [ ^ ]
File.AppendText [< a href =http://msdn.microsoft.com/en-us/library/system.io.file.appendtext(v=vs.110).aspx\"target =_ blanktitle =New Window> ^ ]
试试这个
File.AppendAllText(路径,内容);
Hello I have multiple text files in one location and they are like the name below :
A_L_PRTD_021345.txt , A_L_PRTD_432124 , like this ..
I want to merge them into single file and want to Rename the single file as L_PRTD_Currentdate>.txt
How can It be possible ? Kindly share me some cod of doing this
Use System.IO.StreamReader/StreamWriter to read/write the text files.
For example...
using System.IO; DirectoryInfo di = new DirectoryInfo("<path to containing directory>"); FileInfo[] files = di.GetFiles(); string data = ""; foreach (FileInfo fi in files) { if (fi.Name.ToUpper().StartsWith("A_L_PRTD")) { StreamReader rd = new StreamReader(fi.FullName); data += rd.ReadToEnd(); rd.Close(); rd.Dispose(); //Optionally... File.Delete(fi.FullName); } } StreamWriter wr = new StreamWriter(string.Format("L_PRTD_{0}.txt", DateTime.Now.ToString("ddMMyyyy"))); wr.Write(data); wr.Close(); wr.Dispose();
try like this..
string path = "D://Files//"; //string[] fileNames = { "A_L_PRTD_432124.txt", "A_L_PRTD_021345.txt" }; string fileNameStartsWIth = "A_L_PRTD_"; string[] fileNames = Directory .GetFiles(path, "*.txt", SearchOption.AllDirectories) .Select(f => Path.GetFileName(f)).Where(k => k.StartsWith(fileNameStartsWIth)).ToArray(); string finalFile = "L_PRTD_" + DateTime.Now.ToString("ddMMMyyyy") + ".txt"; string contentSeperator = Environment.NewLine + "-----------------------------------------------------" + Environment.NewLine; foreach (string file in fileNames) { string content = File.ReadAllText(path + file) + contentSeperator; File.AppendAllText(path + finalFile, content); }
reference:
File.ReadAllText [^]
File.AppendText[^]
try this
File.AppendAllText(Path, content);
这篇关于将多个文本文件附加到单个文件中并重命名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!