ここではDropboxの特定のフォルダ以下にあるテキストファイルから★のある行を探している。
VSを使わずに直接CSC.exeでコンパイルしている。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
using System; using System.Linq; using System.Text; using System.IO; namespace Application { class Program { static void Main(string[] args) { int k = 1; string[] ary = new string[256]; var e = Encoding.GetEncoding("shift_jis"); foreach(var f in Directory.GetFiles(@"D:\Dropbox\My Cloud Work\Text\Project", "*.txt", SearchOption.AllDirectories)) { var grep = File.ReadAllLines(@f, e) .Select((s, i) => new { Index = i, Value = s }) .Where(s => s.Value.Contains("★")); foreach (var g in grep) { ary[k] = f; Console.WriteLine(k + " " + Path.GetFileNameWithoutExtension(f) + " {0}:{1}\n", g.Index, g.Value); k++; } } } } } |
PowerShellでやってみると。
1 2 |
Get-ChildItem -include *.txt -Path 'D:\Dropbox\My Cloud Work\Text\Project' -Recurse | Select-String -Pattern "★.*" -encoding default | % { $_ | select-object matches ; $_ | select-object path | split-path -Leaf ; "`n" } read-host |