将多个文本文件附加到单个文件中并重命名 [英] Append multiple text files into single file and rename it

查看:81
本文介绍了将多个文本文件附加到单个文件中并重命名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好我在一个地方有多个文本文件,它们就像下面这个名字:



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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆