0

我试图弄清楚如何浏览内容格式如下的文本文件:

Date: 7/30/2013 12:00:00 AM 
Source Path: C:\FileMoveResults 
Destination Path: C:\Users\Documents\.NET Development\testing\ 
Last Folder Updated: 11.0.25.1

我可以根据“上次更新的文件夹”在哪里搜索它,并将源路径和目标路径存储在单独的变量中。

怎么做呢?

谢谢!

编辑:

这是我目前拥有的代码,它不起作用,因为我收到“foreach 不支持 bool”的错误

using (var reader = new StreamReader(GlobalVars.strLogPath))
{
foreach (var items in File.ReadLines(GlobalVars.strLogPath))
{
foreach(var item in items.StartsWith("Destination Path: "))
{
//nothing here yet
4

4 回答 4

4
  • 一次读 4 行
  • 解析最后一行(最后一个文件夹更新)
  • 检查是否要存储条目
  • 如果是 -> 存储第 2 行和第 3 行
  • 重复直到没有更多可用的行

编辑:反映您关于代码的新问题:

删除最里面的 foreach 循环并将“items”重命名为“oneSingleLine” - 这应该更清楚您的错误所在的位置。也许创建一个新问题,因为这不是一个讨论板。

于 2013-07-30T19:53:24.690 回答
0

这是一个关于如何使用 LINQ 执行此操作的示例启动器。我使用LinqPad来玩代码。

    var input = "Date: 7/30/2013 12:00:00 AM\nSource Path: C:\\FileMoveResults\nDestination Path: C:\\Users\\Documents\\.NET Development\\testing\\ \nLast Folder Updated: 11.0.25.1 \nDate: 7/30/2013 12:00:00 AM\nSource Path: C:\\FileMoveResults\nDestination Path: C:\\Users\\Documents\\.NET Development\\testing\\ \nLast Folder Updated: 11.0.25.1 \nDate: 7/30/2013 12:00:00 AM\nSource Path: C:\\FileMoveResults\nDestination Path: C:\\Users\\Documents\\.NET Development\\testing\\ \nLast Folder Updated: 11.0.25.1 \nDate: 7/30/2013 12:00:00 AM\nSource Path: C:\\FileMoveResults\nDestination Path: C:\\Users\\Documents\\.NET Development\\testing\\ \nLast Folder Updated: 11.0.25.1 \nDate: 7/30/2013 12:00:00 AM\nSource Path: C:\\FileMoveResults\nDestination Path: C:\\Users\\Documents\\.NET Development\\testing\\ \nLast Folder Updated: 11.0.25.1 \nDate: 7/30/2013 12:00:00 AM\nSource Path: C:\\FileMoveResults\nDestination Path: C:\\Users\\Documents\\.NET Development\\testing\\ \nLast Folder Updated: 11.0.25.1 \nDate: 7/30/2013 12:00:00 AM\nSource Path: C:\\FileMoveResults\nDestination Path: C:\\Users\\Documents\\.NET Development\\testing\\ \nLast Folder Updated: 11.0.25.1 \nDate: 7/30/2013 12:00:00 AM\nSource Path: C:\\FileMoveResults\nDestination Path: C:\\Users\\Documents\\.NET Development\\testing\\ \nLast Folder Updated: 11.0.25.1 \nDate: 7/30/2013 12:00:00 AM\nSource Path: C:\\FileMoveResults\nDestination Path: C:\\Users\\Documents\\.NET Development\\testing\\ \nLast Folder Updated: 11.0.25.1";

string[] lines = null;
using(var reader = new StreamReader(new MemoryStream(Encoding.ASCII.GetBytes(input))))
{
    lines = reader.ReadToEnd().Split('\n');
}

IList<Tuple<string, string>> paths = new List<Tuple<string, string>>();
if(lines != null)
{
    const int TAKE = 4;

    var position = 0;

    while(position < lines.Length)
    {
        var currentLines = lines.Skip(position).Take(TAKE);
        paths.Add(Tuple.Create(currentLines.ElementAt(1).Replace("Source Path: ", string.Empty), currentLines.ElementAt(2).Replace("Destination Path: ", string.Empty)));
        position = (position + TAKE);
    }
}

paths.Dump("Result");

我所做的是收集所有源和目标路径。您可以将其修改为仅根据搜索条件存储所需的内容。

结果看起来像这样,当然我只是一遍又一遍地复制您的示例,因此路径名称没有变化:

在此处输入图像描述

Tuple<DictionaryInfo, DictionaryInfo>如果您想在对象中内置移动和复制能力,我可能还会制作类型的元组。

于 2013-07-31T04:50:44.180 回答
0

这是一个如何完成的示例。在处理格式错误的行等方面,这里有很大的改进空间。

    static void ReadFile(string path)
    {
        var lines = System.IO.File.ReadAllLines(path).ToList();
        var infos = new List<TextFileInfo>();

        var info = new TextFileInfo();
        var currentLine = 0;
        while (lines.Count > 0)
        {
            TryReadLine(lines[0], info);

            if (currentLine++ >= 3)
            {
                currentLine = 0;
                infos.Add(info);
                info = new TextFileInfo();
            }

            lines.RemoveAt(0);
        }

        //Do something with infos
        // return infos;
   }

    public static void TryReadLine(string line, TextFileInfo info)
    {
        if (line.StartsWith(DateTag))
        {
            var value = line.Replace(DateTag, string.Empty).Trim();
            info.Date = DateTime.Parse(value);

            return;
        }

        if (line.StartsWith(SourcePathTag))
        {
            var value = line.Replace(SourcePathTag, string.Empty).Trim();
            info.SourcePath = value;

            return;
        }

        if (line.StartsWith(DestinationPathTag))
        {
            var value = line.Replace(SourcePathTag, string.Empty).Trim();
            info.DestinationPath = value;

            return;
        }

        if (line.StartsWith(LastFolderUpdatedTag))
        {
            var value = line.Replace(SourcePathTag, string.Empty).Trim();
            info.LastFolderUpdated = value;

            return;
        }

        throw new Exception("Invalid line encountered");
    }

    public class TextFileInfo
    {
        public DateTime Date { get; set; }
        public string SourcePath { get; set; }
        public string DestinationPath { get; set; }
        public string LastFolderUpdated { get; set; }
    }
于 2013-07-30T20:40:48.447 回答
0

我在想这样的事情。它有点复杂,但有助于确保您的文件格式发生变化时您仍然应该能够循环并找到每组值。

    public void ReadFile(string filepath)
    {
        Dictionary<string, string> mypaths = new Dictionary<string, string>();
        string line;
        string line1 = null;
        string line2 = null;
        bool store = false;
        using(var rdr = new StreamReader(filepath))
        {
            while((line = rdr.ReadLine()) != null)
            {
                if(store = false && line.ToLower().Contains("source path:"))
                {
                    line1 = line;
                    store = true;
                }
                else if (store = true && line.ToLower().Contains("destination path:"))
                {
                    line2 = line;
                }
                else if (line.ToLower().Contains("last folder updated:"))
                {
                    var myvaluetoinspect = line;
                    if(1==1) // my condition
                    {
                        mypaths.Add(line1, line2);
                    }
                    // Clear and start over
                    store = false;
                    line1 = null;
                    line2 = null;
                }
                else
                {
                    store = false;
                    line1 = null;
                    line2 = null;
                }
            }
        }
    }
于 2013-07-30T20:34:57.910 回答