count.exe とは
- 複数のテキストファイルから任意の固定長文字列を探し出してカウントするプログラムです。
- ahk スクリプトを 実行バイナリ化しています
使い方
- count.exeと同一のフォルダにあるwords.txtに、任意の文字列を改行区切りで入力します
- words.txt は utf-8 で保存して下さい
- count.exeと同一のフォルダや下位フォルダに検索対象のテキストファイルを配置します
- count.exeを実行すると、words.txtの内容に従って検索し、処理結果をresult.csvに書き出します
- result.csvはcsv形式のファイルですから、エクセルですぐに編集できます
ソースコード
;; user option
words_filename := ".\words.txt"
result_filename := ".\result.csv"
;; join_string := " "
join_string := ", "
file_initialize()
;; add header
insert_file_arr(words_filename_without_folder_path, words_string, TRUE)
;; 下位フォルダのテキストを再帰的に処理する
Loop, *.txt, 0, 1
{
name := A_LoopFileName
path := A_LoopFileFullPath
if (name == words_filename_without_folder_path) {
continue
}
if (name == result_filename_without_folder_path) {
continue
}
insert_file_arr(name, file_read_without_newline(path))
}
output_result()
MsgBox, % "処理が終わりました`n処理結果は " result_filename " に書き出しました"
ExitApp
return
F12::
ExitApp
return
file_initialize() {
global
file_arr := []
file_index:=0
words_string := file_read_with_newline(words_filename)
words_arr := lines_to_array(words_string)
SplitPath, words_filename, OutFileName
words_filename_without_folder_path := OutFileName
SplitPath, result_filename, OutFileName
result_filename_without_folder_path := OutFileName
}
output_result() {
global result_filename, file_arr
file := FileOpen(result_filename, "w", "CP65001")
file.write(join_with_newline(file_arr))
file.close()
}
insert_file_arr(filename, string, header=FALSE) {
global words_arr, file_arr, file_index
file_arr[file_index, 0] := filename
count_match := 0
;; index は 0 から始まるので
;; 格納しているファイル名を上書きしないよう
;; index + 1 とする
if (header) {
;; header を出力するときには count_match を強制的に True にする
;; そうしなければ file_arr から header が取り除かれてしまう
count_match := True
for index, value in words_arr
{
file_arr[file_index, index + 1] := value
}
} else {
for index, value in words_arr
{
count := CountItem(string, value)
file_arr[file_index, index + 1] := count
count_match += count
}
}
;; word に一つもヒットしなければ
;; file_arr から今回の処理を取り除く
if (0 == count_match) {
file_arr.Remove(file_index)
} else {
file_index++
}
}
join_with_newline(array){
tmp := ""
for i, v in array
{
tmp .= join(v) "`r`n"
}
return tmp
}
join(array){
global join_string
tmp := ""
for index, value in array
{
tmp .= value
if (index != array.MaxIndex()) {
tmp .= join_string
}
}
return tmp
}
lines_to_array(string) {
arr := []
i := 0
Loop, Parse, string, `n, `r {
{
if ("" != Trim(A_LoopField)) {
arr[i] := A_LoopField
i++
}
}
return arr
}
file_read_without_newline(filename){
string := file_read_with_newline(filename)
tmp := ""
Loop, Parse, string, `n, `r
{
tmp .= A_LoopField
}
return tmp
}
file_read_with_newline(filename){
file := FileOpen(filename, "r", "CP65001")
tmp := file.read()
file.close()
return tmp
}
CountItem(string, item){
StringReplace, _, string, %item%, , UseErrorLevel
return errorlevel
}
Last modified : 2014/07/08 02:06:02 JST
blechmusik (blechmusik@gmail.com)