c2535103 发表于 2015-2-13 00:33:46

批次读取txt档里的特定字串数值 再输出csv档

大致流程如下,有多个以.log结尾的txt档
想先读取log1里面A後面的变数跟B後面的变数,存进array
接下来再读取log2,进行一样的动作
假设读完100做txt档後,再一次输出成一个csv档,并照固定的栏位排列
log1.log
==
A:1
B:2

log2.log
==
A:3
B:4

log.csv
==
      A   B
log11   2
log23   4
==

要实现这个内容应该会用到这两个函式吧?
_FileReadToArray
_FileWriteFromArray
另外要批次读取txt档是用哪做函式,可否指教一下?
是否有简单处理两个txt档的范例可参考?
感激不尽!

veket_linux 发表于 2015-2-13 18:26:42


;生成测试log文件
Dim $i
Dim $logfilename
Dim $fhandle
Dim $buf
Dim $sc_path
$sc_path = @ScriptDir
If StringRight($sc_path, 1) <> "\" Then $sc_path &="\"
For $i = 1 To 100
        $logfilename = "log" & $i & ".log"
        $fhandle = FileOpen($sc_path & $logfilename, 10)
        $buf = "A:" & Random(1, 10, 1) & @CRLF
        $buf &= "B:" & Random(1, 10, 1) & @CRLF
        FileWrite($fhandle, $buf)
        FileClose($fhandle)
Next
MsgBox(0, "测试用log文件已生成", "")



;提取log文件输出csv
Dim $i
Dim $logfilename
Dim $fhandle
Dim $buf
Dim $sc_path
Dim $var_a
Dim $var_b
Dim $csv
$sc_path = @ScriptDir
If StringRight($sc_path, 1) <> "\" Then $sc_path &="\"
$csv = ",A,B" & @CRLF

For $i = 1 To 100
        $logfilename = "log" & $i & ".log"
        $fhandle = FileOpen($sc_path & $logfilename, 0)
        $buf = FileRead($fhandle)
        FileClose($fhandle)
        $n = StringRegExp($logfilename, ".+?(?=\.)", 3)
        $csv &=$n & ","
        $n = StringRegExp($buf, "(?<=:).*?(?=\s)", 3)
        $csv &=$n & "," & $n & @CRLF
Next
$fhandle = FileOpen($sc_path & "log.csv", 10)
FileWrite($fhandle, $csv)
FileClose($fhandle)
MsgBox(0, "提取文件完成", "")

veket_linux 发表于 2015-2-13 18:34:12

页: [1]
查看完整版本: 批次读取txt档里的特定字串数值 再输出csv档