11.8 遍历目录,汇总所有文件
遍历目录,递归调用脚本汇总文件。
遍历指定目录及其子目录下所有文本类型文件,将每个文件中的第 17 行汇总到一个文件中。

文本类型文件示例:
16 …
17 Middleware for report source data computing
18 …
汇总后 result.txt:
Middleware for report source data computing
SPL parsing and exporting Excel
SQL Headaches Therapies – For Loop Operations
The skill of updating database with esProc
…
参数:
| ID | Name | Value | Remarks |
|---|---|---|---|
| 1 | path | 文件目录 |
读取文件指定行并输出到文本文件,保存为 readfile.dfx。
脚本:
| A | B | |
|---|---|---|
| 1 | =directory@p(path) | |
| 2 | =A1.(file(~).cursor@s()) | |
| 3 | =A2.((~.skip(16),~.fetch@x(1))) | |
| 4 | =A3.union() | |
| 5 | >file(“result.txt”).export@a(A4) | |
| 6 | =directory@dp(path) | |
| 7 | if A6.len()==0 | return |
| 8 | else | =A6.(call(“readfile.dfx”,~)) |
A1 列出当前目录文件列表
A2 循环打开文件游标
A3 每个文件游标跳过 16 行,取第 17 行
A4 合并取出来的记录
A5 将结果追加到 result.txt 文件
A6 列出当前目录的子目录列表
B7 如果没有子目录,程序返回
B8 如果有子目录,递归执行脚本
使用 call() 函数执行编辑好的 readfile.dfx。
脚本:
| A | |
|---|---|
| 1 | =call(“readfile.dfx”,“D:/Documents”) |
A1 执行网格程序,参数指定目录
通过递归遍历目录和游标式读取文件,几乎不占用内存,适合大量文件和大数据场景。
