指定したフォルダにあるファイル内の指定した文字列を含む行を抽出します。
◆動作環境
OS |
Windows 7 Professional Service Pack 1 64bit |
画面説明
メイン画面
No | 説明 |
@ | 検索対象のフォルダを指定します。 |
A | 「フォルダーの参照」ダイアログを開きます。 |
B | 検索する文字列を指定します。 |
C | 検索を開始します。 |
D | 検索結果を表示します。 |
E | 検索結果に表示されている内容をファイルに出力します。 |
フォルダーの参照画面
実行例
開発情報
◆環境
OS |
Windows 7 Professional Service Pack 1 64bit |
C# |
Microsoft Visual C# 2010 01018-587-4111284-70817 |
[ファイル]-[新規作成]-[プロジェクト]を選択。
[Windows フォーム アプリケーション]を選択し、名前に「MyFindAndGrep」と入力し「OK」。
文字列に指定した文字が含まれるかどうかは String.IndexOf で調べることができます。
Form1.cs
|
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:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
165:
166:
167:
168:
169:
170:
171:
172:
173:
174:
175:
176:
177:
178:
179:
180:
181:
182:
183:
184:
185:
186:
187:
188:
189:
190:
191:
192:
193:
194:
195:
196:
197:
198:
199:
200:
201:
202:
203:
204:
205:
206:
207:
208:
209:
210:
211:
212:
213:
214:
215:
216:
217:
218:
219:
220:
221:
|
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace MyFindAndGrep
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click( object sender, EventArgs e )
{
int ret = 0;
if ( ( textBox_Folder.Text.Count() <= 0 )
|| ( textBox_Letter.Text.Count() <= 0 )
)
{
MessageBox.Show(
"対象フォルダと検索文字列を"
+ "入力してください。"
, "Question"
, MessageBoxButtons.OK
, MessageBoxIcon.Question
);
return;
}
// リストボックスの消去
listBox_Result.Items.Clear();
// find+grepの実行
ret = DoFindAndGrep( textBox_Folder.Text
, textBox_Letter.Text
, listBox_Result
);
// 結果表示
MessageBox.Show(
"検索が終わりました。\n"
+ "終了コード:" + ret
, "Information"
, MessageBoxButtons.OK
, MessageBoxIcon.Information
);
}
private int DoFindAndGrep( string folder
, string letter
, ListBox list_result
)
{
int ret = 0;
// 指定されたディレクトリ配下のファイルを取得
string[] files
= Directory.GetFiles( folder );
foreach( string file_path in files )
{
ret = DoFileRead( file_path
, letter
, list_result
);
if ( ret != 0 )
{
break;
}
}
return ret;
}
private int DoFileRead( string file_path
, string letter
, ListBox list_result
)
{
int ret = 0;
// ファイルを開く
StreamReader sr
= new StreamReader(
file_path
, Encoding.GetEncoding( "Shift_JIS" )
);
// ファイルを1行ずつ読み込む
string line = "";
for ( int i = 0; ; i++ )
{
line = sr.ReadLine();
if ( line == null )
{
break;
}
ret = DoGrep( line, letter );
if ( ret == 0 )
{
list_result.Items.Add( line );
}
}
// ファイルを閉じる
sr.Close();
return 0;
}
private int DoGrep( string line
, string letter
)
{
// lineにletterが含まれるか調べる
int idx = line.IndexOf( letter );
if ( idx >= 0 )
{
// 含まれる
return 0;
}
// 含まれない
return -1;
}
// 検索フォルダを選択するボタンの処理
private void button2_Click( object sender, EventArgs e )
{
FolderBrowserDialog fbd = new FolderBrowserDialog();
// 説明テキスト
fbd.Description = "フォルダを指定してください";
// デフォルトフォルダー
fbd.RootFolder = Environment.SpecialFolder.Desktop;
// ダイアログを表示
DialogResult rslt = fbd.ShowDialog( this );
if ( rslt == DialogResult.OK )
{
textBox_Folder.Text = fbd.SelectedPath;
textBox_Letter.Focus();
}
}
// 出力結果(リストの内容)をファイルに書き出す
// ボタンの処理
private void button3_Click( object sender, EventArgs e )
{
// 出力リストの内容を取得
string[] list
= listBox_Result.Items.Cast<string>().ToArray();
if ( list.Count() <= 0 )
{
// 結果表示
MessageBox.Show(
"出力する内容がありません。"
, "Information"
, MessageBoxButtons.OK
, MessageBoxIcon.Exclamation
);
return;
}
SaveFileDialog sfd = new SaveFileDialog();
// 説明テキスト
sfd.Title = "保存先のファイルを指定してください";
// 保存するファイルの名前
sfd.FileName = textBox_Letter.Text + ".txt";
// ダイアログを表示
DialogResult rslt = sfd.ShowDialog( this );
if ( rslt != DialogResult.OK )
{
return;
}
// ファイルを開く
StreamWriter sw
= new StreamWriter(
sfd.FileName
, true
, Encoding.GetEncoding( "Shift_JIS" )
);
// 取得した出力リストの内容を1行ずつファイルに
// 書き込む
foreach( string s in list )
{
sw.WriteLine( s );
}
// ファイルを閉じる
sw.Close();
// 結果表示
MessageBox.Show(
"リストの内容をファイルに出力しました。\n"
+ "ファイル名:" + sfd.FileName
, "Information"
, MessageBoxButtons.OK
, MessageBoxIcon.Information
);
}
}
}
|
|
▲ PageTop ■ Home
Copyright (C) 2015 ymlib.com