C# 使用Directory.Enumerate文件显示文件名
我正在做一个文件阅读器,需要输出一个具有特定格式的新文件。 现在,我正在尝试获取构建输出文件所需的所有信息,但我缺少两条信息:文件日期和文件名。对于这两个字段,我计划使用文件名,因此对于日期,a将修剪名称以仅获取日期,因为日期在名称中 我需要从目录中的每个文件中获取“fct=”之后的值,并计算该值的累计时间,我可以使用正则表达式来实现这一点,但我无法提取我所在文件的名称,因此我可以知道“fct=”值是在哪个文件中获取的。C# 使用Directory.Enumerate文件显示文件名,c#,regex,linq,file,C#,Regex,Linq,File,我正在做一个文件阅读器,需要输出一个具有特定格式的新文件。 现在,我正在尝试获取构建输出文件所需的所有信息,但我缺少两条信息:文件日期和文件名。对于这两个字段,我计划使用文件名,因此对于日期,a将修剪名称以仅获取日期,因为日期在名称中 我需要从目录中的每个文件中获取“fct=”之后的值,并计算该值的累计时间,我可以使用正则表达式来实现这一点,但我无法提取我所在文件的名称,因此我可以知道“fct=”值是在哪个文件中获取的。 以前我可以通过Directory.Getfilename使用它,但现在我无
以前我可以通过
Directory.Getfilename
使用它,但现在我无法使用它,因为我使用的是枚举文件
以下是输入=文件的示例:
**2020-03-02_TRS.txt**
<dat>FCT=10019,XN=KEY,CN=ROHWEPJQSKAUMDUCFCT=777</dat></logurl>
<dat>XN=KEY,CN=RTU FCT=4515</dat>LBZ=test.sqi</logurl>
<dat>XN=KEY,CN=RT</dat>FCT=10019</logurl>
<dat>XN=KEY,CN=RT</dat>fct=717</logurl>
**2020-03-02_SKU.txt**
<dat>FCT=666,XN=KEY,CN=ROHWEPJQSKAUMDUCFCT=777</dat></logurl>
<dat>XN=KEY,CN=RTU FCT=123</dat>LBZ=test.sqi</logurl>
The output i'm looking for is:
2x 10019 TRS
1x 4515 TRS
1x 717 TRS
1x 666 SKU
1x 123 SKU
**2020-03-02\u TRS.txt**
FCT=10019,XN=KEY,CN=ROHWEPJQSKAUMDUCFCT=777
XN=键,CN=RTU FCT=4515LBZ=test.sqi
XN=键,CN=RTFCT=10019
XN=键,CN=RTfct=717
**2020-03-02_SKU.txt**
FCT=666,XN=KEY,CN=ROHWEPJQSKAUMDUCFCT=777
XN=键,CN=RTU FCT=123LBZ=test.sqi
我想要的输出是:
2x 10019 TRS
1x 4515 TRS
1x 717 TRS
1x 666 SKU
1x 123 SKU
TRS和SKU只是名称的一部分,因此如果我能找到一种方法来查找文件名,然后我将能够对其进行解析,以仅保留TRS或SKU
这是我的密码:
class Program
{
static void Main(string[] args)
{
int counter = 0;
StreamWriter sw = new StreamWriter("C:/LogParser/LogParserV1/test.sql", true);
char[] delimiters = { '<', ',', '&', ':', ' ', '\\', '\'' };
{
Regex regex = new Regex("(?<=FCT=)[0-9]*", RegexOptions.IgnoreCase);
var fctlist = Directory
.EnumerateFiles(@"C:/LogParser/LogParserV1", "*.txt", SearchOption.AllDirectories)
.SelectMany(file => File.ReadLines(file))
.SelectMany(line => regex
.Matches(line)
.Cast<Match>()
.Select(match => match.Value))
.GroupBy(number => number)
.Select(group => $"'{group.Count()}','{group.Key}','FCT',");
foreach (string fctnumber in fctlist)
Console.WriteLine(fctnumber);
}
counter++;
// Suspend the screen.
System.Console.ReadLine();
sw.Close();
}
}
}
类程序
{
静态void Main(字符串[]参数)
{
int计数器=0;
StreamWriter sw=newstreamwriter(“C:/LogParser/LogParserV1/test.sql”,true);
char[]分隔符={'File.ReadLines(文件))
.SelectMany(line=>regex
.匹配项(行)
.Cast()
.Select(匹配=>match.Value))
.GroupBy(数字=>number)
.Select(group=>$“{group.Count()}',{group.Key}','FCT',”;
foreach(fctlist中的字符串fctnumber)
控制台写入线(fctnumber);
}
计数器++;
//暂停屏幕。
System.Console.ReadLine();
sw.Close();
}
}
}
首先,正则表达式似乎还捕获了ROHWEPJQSKAUMDUCFCT=777
值,根据输出示例,它确实不应该捕获这些值。也许,您需要将其更改为类似于(?以下是另一种解决方案:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Text.RegularExpressions;
namespace ConsoleApplication159
{
class Program
{
const string FOLDER = @"c:\temp\test\";
static void Main(string[] args)
{
string patternData = "<dat>(?'data1'[^<]+)</dat>(?'data2'[^<]+)?</logurl>";
string patternAttribute = @"(?'key'\w+)=(?'value'\w+)";
string[] filenames = Directory.GetFiles(FOLDER, "*.txt");
foreach (string filename in filenames)
{
Dictionary<string, int> dict = new Dictionary<string, int>();
StreamReader reader = new StreamReader(filename);
string line = "";
while ((line = reader.ReadLine()) != null)
{
Match matchData = Regex.Match(line, patternData);
if (matchData.Success)
{
string data = string.Join(",", new string [] {matchData.Groups["data1"].Value, matchData.Groups["data2"].Value}).ToUpper();
Dictionary<string,string> dictAttributes = Regex.Matches(data, patternAttribute)
.Cast<Match>().GroupBy(x => x.Groups["key"].Value, y => y.Groups["value"].Value)
.ToDictionary(x => x.Key, y => y.FirstOrDefault());
if (dictAttributes.ContainsKey("FCT"))
{
string value = dictAttributes["FCT"];
if (dict.ContainsKey(value))
{
dict[value]++;
}
else
{
dict.Add(value, 1);
}
}
}
}
foreach (KeyValuePair<string, int> key in dict)
{
string file = filename.Substring(0,filename.LastIndexOf("\\"));
file = file.Substring(file.LastIndexOf(".") + 1);
Console.WriteLine("{0}x {1}\t\t{2}", key.Value, key.Key, file);
}
}
Console.ReadLine();
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.IO;
使用System.Text.RegularExpressions;
命名空间控制台应用程序159
{
班级计划
{
常量字符串文件夹=@“c:\temp\test\”;
静态void Main(字符串[]参数)
{
string patternData=“(?'data1'[^它是2020-03-02_-EXE.txt
还是2020-03-02_-SKU.txt
?它都有2个文件。我看到了TRS
和EXE
文件,我没有看到SKU
文件。SKU
从哪里来的?我的错误我纠正了,谢谢注意
.Select(match => new
{
Filedate = //some parsing here
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Text.RegularExpressions;
namespace ConsoleApplication159
{
class Program
{
const string FOLDER = @"c:\temp\test\";
static void Main(string[] args)
{
string patternData = "<dat>(?'data1'[^<]+)</dat>(?'data2'[^<]+)?</logurl>";
string patternAttribute = @"(?'key'\w+)=(?'value'\w+)";
string[] filenames = Directory.GetFiles(FOLDER, "*.txt");
foreach (string filename in filenames)
{
Dictionary<string, int> dict = new Dictionary<string, int>();
StreamReader reader = new StreamReader(filename);
string line = "";
while ((line = reader.ReadLine()) != null)
{
Match matchData = Regex.Match(line, patternData);
if (matchData.Success)
{
string data = string.Join(",", new string [] {matchData.Groups["data1"].Value, matchData.Groups["data2"].Value}).ToUpper();
Dictionary<string,string> dictAttributes = Regex.Matches(data, patternAttribute)
.Cast<Match>().GroupBy(x => x.Groups["key"].Value, y => y.Groups["value"].Value)
.ToDictionary(x => x.Key, y => y.FirstOrDefault());
if (dictAttributes.ContainsKey("FCT"))
{
string value = dictAttributes["FCT"];
if (dict.ContainsKey(value))
{
dict[value]++;
}
else
{
dict.Add(value, 1);
}
}
}
}
foreach (KeyValuePair<string, int> key in dict)
{
string file = filename.Substring(0,filename.LastIndexOf("\\"));
file = file.Substring(file.LastIndexOf(".") + 1);
Console.WriteLine("{0}x {1}\t\t{2}", key.Value, key.Key, file);
}
}
Console.ReadLine();
}
}
}