在文件中查找文本并检索行号 [英] Find Text in Files and retrieve the line number

查看:177
本文介绍了在文件中查找文本并检索行号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图(以编程方式)在大量VB6文件中找到对特定字符串的引用,即LOCK_ID。为了帮助人们直接导航到参考,我还想检索匹配的行号。即:
$ b $ ol

  • 搜索所有VB6文件以供参考

  • 如果找到引用,我想检索引用所在的行号。

  • 打开目录中的每个文件并迭代文件并保持计数我正在检查的搜索字词,是否有一个更快/更简单的方法来实现这一目标?

    解决方案

    这里是函数我用来实现所需的功能:

      private void FindReferences(List< string> output,string searchPath,string searchString)
    {
    if(Directory.Exists(searchPath))
    {
    string [] files = Directory.GetFiles(searchPath,*。*,SearchOption.AllDirectories);
    字符串行;

    //遍历指定目录中的所有文件&在所有子目录中
    foreach(文件中的字符串文件)
    {
    using(StreamReader reader = new StreamReader(file))
    {
    int lineNumber = 1; ($ line)
    while((line = reader.ReadLine())!= null)
    {
    if(line.Contains(searchString,StringComparison.OrdinalIgnoreCase))
    {
    output.Add(string.Format({0}:{1},file,lineNumber));
    }

    lineNumber ++;




    $ b code $


    助手类:

      ///< summary> 
    ///确定源字符串是否包含指定的值。
    ///< / summary>
    ///< param name =source>要搜索的字符串< / param>
    ///< param name =toCheck>搜索条件< / param>
    ///< param name =comparisonOptions>要使用的字符串比较选项< / param>
    ///<返回>
    ///< c> true< / c>如果源包含指定的值;否则,< c> false< / c> ;.
    ///< / returns>
    public static bool Contains(this string source,string value,StringComparison comparisonOptions)
    {
    return source.IndexOf(value,compareOptions)> = 0;
    }


    I am trying to (programatically) find references to a specific string i.e. 'LOCK_ID' within a large number of VB6 files. To help people navigate directly to the reference I would also like to retrieve the line number of the match. i.e:

    1. Search all VB6 files for reference
    2. If a reference was found I would like to retrieve the line number on which the reference was located.

    Short of opening every file in the directories and iterating through the file and keeping count of which line I am examining for the search term, is there a quicker/easier way of achieving this?

    解决方案

    Here are the functions I used to achieve the desired functionality:

    private void FindReferences( List<string> output, string searchPath, string searchString )
    {
        if ( Directory.Exists( searchPath ) )
        {                                                                
            string[] files = Directory.GetFiles( searchPath, "*.*", SearchOption.AllDirectories );
            string line;
    
            // Loop through all the files in the specified directory & in all sub-directories
            foreach ( string file in files )
            {
                using ( StreamReader reader = new StreamReader( file ) )
                {
                    int lineNumber = 1;
                    while ( ( line = reader.ReadLine() ) != null )
                    {
                        if ( line.Contains( searchString, StringComparison.OrdinalIgnoreCase ) )
                        {
                            output.Add( string.Format( "{0}:{1}", file, lineNumber ) );
                        }
    
                        lineNumber++;
                    }
                }                   
            }
        }
    }
    

    Helper class:

    /// <summary>
    /// Determines whether the source string contains the specified value.
    /// </summary>
    /// <param name="source">The String to search.</param>
    /// <param name="toCheck">The search criteria.</param>
    /// <param name="comparisonOptions">The string comparison options to use.</param>
    /// <returns>
    ///   <c>true</c> if the source contains the specified value; otherwise, <c>false</c>.
    /// </returns>
    public static bool Contains( this string source, string value, StringComparison comparisonOptions )
    {
        return source.IndexOf( value, comparisonOptions ) >= 0;
    }
    

    这篇关于在文件中查找文本并检索行号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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