(ファイル名はバラバラですが、1フォルダ内に収まっています)
テキストファイル内の
2カンマ目のデータ内容が「2」だったら
ファイル名とその行の内容を
書き出していくという処理を行いたいです
(書き出した結果はcsvファイルにしたい)
どのようなVBAを組めば良いか教えてください
<例>
a.txt ファイル内
20121001,1,,start
20121001,0,1234567,OK
20121001,0,2345678,OK
20121001,2,,error0391
20121001,1,,start
20121001,0,1234567,OK
20121001,0,2345678,OK
20121001,2,,error0111
b.txt ファイル内
20121001,1,,start
20121001,0,1234567,OK
20121001,0,2345678,OK
20121001,2,,error123
結果.csv
a.txt 20121001,2,,error0391
a.txt 20121001,2,,error0111
b.txt 20121001,2,,error0123
こんな感じでどうでしょう。
読み込むファイルや保存先のファイルは、サブルーチン hogeConv の引数を変更して下さい。
Option Explicit '--- メインプログラム Sub main() '第1引数=対象フォルダ '第2引数=拡張子 '第3引数=保存先パス名+ファイル名 '第4引数=合致を検知するカラム番号 '第5引数=合致する文字列 Call hogeConv("C:/test/", "txt", "C:/test/hozon.csv", 2, "2") End Sub '----ファイル探索+ファイル処理 Sub hogeConv(path As String, ext As String, outfname As String, num As Integer, str As String) Dim fcol As Object, re As Object Dim flist As Variant, remat As Variant Dim pat As String, fname As String Dim flag As Boolean Kill outfname '処理対象ファイル探索+処理実行 Set fcol = CreateObject("Scripting.FileSystemObject").GetFolder(path).Files Set re = CreateObject("VBScript.RegExp") pat = "\." & ext & "$" With re .Pattern = pat .IgnoreCase = True .Global = True For Each flist In fcol Set remat = .Execute(flist.Name) If remat.Count > 0 Then flag = hogeFile(path, flist.Name, outfname, num, str) End If Next flist End With Set re = Nothing Set fcol = Nothing End Sub '---1ファイル処理 Function hogeFile(path As String, infname As String, outfname As String, num As Integer, str As String) As Boolean Dim buf As String, cols() As String Open path & infname For Input As #1 Open outfname For Append As #2 Do Until EOF(1) Line Input #1, buf cols = Split(buf, ",") If (cols(num - 1) = str) Then Print #2, infname & " " & buf End If Loop Close #1 Close #2 hogeFile = True End Function
テキスト処理であれば EXCEL を使用する必要もないと思いますので、VBS での
実装例ですが、VBA への移植もWSCript とある部分を変更する程度で可能だと思います。
下記をメモ帳等に貼り付け適当な名前(PhooSan.vbs)として保存し、このファイルに処理したいフォルダをドロップすればフォルダ内のファイルを処理します。
Option Explicit '//-------------------------------------------------------------------- '// スクリプトに データの入ったフォルダをドロップして実行 '//-------------------------------------------------------------------- If WScript.Arguments.Count <> 1 Then WScript.Echo "フォルダを指定してください。" WScript.Quit End If Dim fso Set fso = CreateObject("Scripting.FileSystemObject") Dim resultFilePath '// 出力ファイル名 resultFilePath = fso.GetFile( WScript.ScriptFullName ).ParentFolder.Path _ & "\result" & Replace( Replace( Replace( FormatDateTime(Now()), "/", ""), ":", "" ), " ", "_" ) & ".txt" Dim resultFile Set resultFile = fso.CreateTextFile( resultFilePath ) Dim txtFile Dim Lines Dim Line Dim Data For Each txtFile In fso.GetFolder( WScript.Arguments.Item(0) ).Files If UCase( fso.GetExtensionName( txtFile.Path ) ) = "TXT" Then Lines = Split( fso.OpenTextFile( txtFile ).ReadAll(), vbNewLine ) For Each Line In Lines Data = Split( Line, "," ) If UBound( Data ) >= 1 Then If Trim( Data(1) ) = "2" Then resultFile.WriteLine txtFile.Name & " " & Line End If End If Next End If Next '// 処理結果をメモ帳で表示 CreateObject("WScript.Shell").Run "notepad.exe """ & resultFilePath & """"
うまく処理できませんでした・・・(というより動きません)w
2012/12/11 23:32:07