Showing posts with label LinqPAD. Show all posts
Showing posts with label LinqPAD. Show all posts

December 4, 2022

Linqpad Notepad++ Hyperlink Extension

When searching through some files using LinqPad it can be useful to print out the results as a hyperlink that will open the file in Notepad++ at a particular line number.
public static class NotepadppExtension
{
    // Usage
    //string filePath = "X:/some/path/file.txt";
    //filePath.CreateNotePadppHyperLink(lineNumber);

    private const string NotePadppPath = @"C:\Program Files\Notepad++\notepad++.exe";
    private static bool onceOnly = false;

    public static Hyperlinq CreateNotepadppHyperLink(this string filePath, int lineNumber)
    {
        if (!onceOnly)
        {
            onceOnly = true;
            Debug.Assert(File.Exists(NotePadppPath), $"Notepad++.exe Path: \"{NotePadppPath}\" is wrong");
        }
        ProcessStartInfo psi = new ProcessStartInfo()
        {
            FileName = NotePadppPath,
            WorkingDirectory = Path.GetDirectoryName(NotePadppPath),
            //Arguments = " " + filePath + " -n" + lineNumber.ToString() + " ",
        };
        psi.ArgumentList.Add(filePath);
        psi.ArgumentList.Add("-n" + lineNumber.ToString());
        var filelink = new Hyperlinq(() => Process.Start(psi), filePath);

        return filelink;
    }
}
Usage
string filePath = .... ;
filePath.CreateNotePadppHyperLink(lineNumber);
A search files example in Linq
void Main()
{
	Directory.EnumerateFiles(
	@"X:\Backup\Documents\Journals\", 
	"*.log", SearchOption.AllDirectories)
		.SelectMany(file => TryFileReadLines(file).Select((text,n)=> 
                 new {Text=text,LineNumber=n+1, Link=file.CreateNotepadppHyperLink(n+1)}))
		.Where(line => 
		    //Regex.IsMatch(line.text, @"CallSearcherBase")  && 
		    line.Text.Contains("\"Search for this text\"", StringComparison.OrdinalIgnoreCase) )	
		.Dump("Matches found");
}

March 4, 2021

Search File Contents with LinqPad

Searching the contents of some file or files in LinqPad (a line at a time) in C# or even just finding a file:
Directory.EnumerateFiles(
  @"C:\Path\To\Search\Directory\", // Target of search
  "*.txt", // Specify file or files to search
  SearchOption.AllDirectories).  
  // SearchOption.AllDirectories - recusively search subdirectories
  // SearchOption.TopDirectoryOnly - search specified directory only
  // Next line records a line of text, the line number, and the file
  SelectMany(file => File.ReadAllLines(file).Select((text,n) => new {text,lineNumber=n+1,file})).
  // Here we search the text
  // Use Regex for the search OR use string methods Contains(), StartsWith, EndsWith ...
  Where(line => Regex.IsMatch(line.text, @"Searching for this text", RegexOptions.IgnoreCase)
                                            && line.text.Contains("ERROR") ).
  Dump("Matches found")
When debugging you can always use Skip() and Take() to reduce the number of lines searched.
A similar one but this uses a LinqPad HyperLinq object to use Notepad++ to open the file at the given line number.
// https://npp-user-manual.org/docs/command-prompt/
var notepadppPath = @"C:\Program Files (x86)\Tools\Notepad++\notepad++.exe";
Directory.EnumerateFiles(
   @"C:\Repos\",  // Directory
   "*.csproj", SearchOption.AllDirectories) // File names
   .SelectMany(file => File.ReadAllLines(file).Select((text, n) =>
		new
		{   
			file = new Hyperlinq(() => Process.Start(notepadppPath, 
				file+" -n"+(n+1).ToString()), file),
			lineNumber = n + 1,
			text
		}))
   .Where(x => // WHat you are looking for in the file
  // Regex.IsMatch(line.text, @"^.*tlpNotifySystemShutdown.*$"))
     x.text.Contains("DownloadProcess.Contracts") // 1.0.1
  || x.text.Contains("Foobar.Client")  // 13.15.0
  || x.text.Contains("DownloadProcess.Client") // 1.2.0
  || x.text.Contains("XxxZipCreator.")  // 5.1.0
  || x.text.Contains("Sloopy.Crial.Messages") // 1.6.3
   )
   .Dump("Matches found");
Here is another example:
Directory.EnumerateFiles(
  @"C:\Path\To\Search\Directory\",
  "*.cs", SearchOption.AllDirectories)
  .SelectMany(file => File.ReadAllLines(file).Select((text, n) => new { text, lineNumber = n + 1, file }))
  .Where(line => line.text.Contains("[CallerMemberName"))
  .Dump("Matches found")

August 28, 2013

LINQPad Command-Line and Scripting

LINQPad Command-Line and Scripting
Sample usage:
CALL C:\...\LinqPad\lprun.exe "C:\...\Queries\AttachDatabases.linq" DEV
This runs the given linq query. The sample is a "C# Program" and has a "Main" method taking "string[] args" as a paremeter. In this way the "DEV" string at the end of the line is passed as a parameter to the linq program.
Here is the sample linq script:
<Query Kind="Program" />

void Main(string[] args)
{
  string attachFolder = @"C:\Databases\";
  if ((args != null) && (args.Length == 1))
  {
  attachFolder = Path.Combine(attachFolder, args[0]);
  }
  Console.WriteLine("Attaching to databases in \'" + attachFolder + "\'"); 
  Console.WriteLine("");
  var server = @".\";
  var databaseNames = new[] { "XXX", "YYY", "ZZZ", "AAA" };

    using(var connection = new SqlConnection(
   string.Format("Server={0};Database=master;Trusted_Connection=True;", server)))
    {
    connection.Open();
  
    // attach the databases
    foreach(var database in databaseNames)
    {
      var dataFile = Path.Combine(attachFolder, database + "_Data.MDF");
      if (File.Exists(dataFile))
      {
        Console.WriteLine("Attaching {0}", database);
        var attachCommand = connection.CreateCommand();
        attachCommand.CommandText = "sp_attach_db  @dbName, @dataFileName, @logFileName";
        attachCommand.Parameters.AddWithValue("dbName", database);
        attachCommand.Parameters.AddWithValue("dataFileName", dataFile);
        attachCommand.Parameters.AddWithValue("logFileName", 
            Path.Combine(attachFolder, database + "_Log.LDF"));    
        attachCommand.ExecuteNonQuery();
      }
      else
      {
        Console.WriteLine("No data file for {0}", database);
      }
    }
      }
  
    Console.WriteLine("");
    Console.WriteLine("Press any key to continue ...");
    Console.ReadKey(false);
}
If you want the script to hang around a bit so that you can read the error messages then you can add the the following lines to the end
Console.WriteLine("");
Console.WriteLine("Press any key to continue ...");
Console.ReadKey(false);
This keeps the script alive until a key is pressed.