• 拓展文件 BY SA NC CC
  • 【指令脚本/抛砖引玉】getFileList 获取文件夹列表—— os.execute/os.popen 的简单应用

获取文件夹列表

使用《署名—非商业性使用—相同方式共享 4.0 协议国际版》(CC BY-NC-SA 4.0)进行授权。
https://creativecommons.org/licenses/by-nc-sa/4.0/legalcode.zh-Hans

[ toc ]

Systemstatement
Windowssupported
Linuxwaiting for validate
IOSunsupported

1.基本信息

  • 作者: 简律纯
  • 联系方式:qq:a2c29k9
  • 版本:v1.0
  • 更新日期:2022/08/03
  • 关键词:
    Windows:
    --getDiceList=
    --getRootList=
    Linux:
    --getFileList=
  • 许可协议:CC BY-NC-SA 4.0
  • 原文

2.原理

利用os库中的os. execute执行cmd指令,达到获取文件夹列表的目的。

须知,该脚本只适配win平台,linux版本将会在后续更新(或者你也可以修改脚本,将dir改为ls即可,注意linux斜杠和反斜杠与win的区别),MA暂无解决方法。

3.用法

--getDiceList=nil--getRootList=nil分别用于获取dice根目录列表以及框架根目录列表,后接二级目录或更多(你需要用两个反斜杠\将他们连接)子目录来获取其列表。每次执行命令,脚本将会在dice根目录生成对应的DiceList.txt与RootList.txt,若文件太多发不出来也可以去那边查看。

4.举例

获取根目录列表

获取根目录二级或三级文件夹列表
获取Dice文件夹二级或更多级文件夹列表同理。

获取Dice文件夹列表

5.下载

os.execute
Windows Version:

写法上与linux差异过大

getfilelist.zip
797B

os.popen
Linux Version还未测试

欢迎使用linux的master测试一波然后回复我能否正常使用

getfilelist-for-linux.zip
514B
14 天 后
简律纯 将标题更改为 「【指令脚本】getFileList 获取文件夹列表—— os.execute 的简单应用」。

更新

Update the Linux version.

io.popen()

故事不紧接上文,今天通宵了,主要是在整原神tts说实话快猝死了,如图:

然后就又根据FileLib.lua写了个获取文件列表:

提取重要信息:

-作用:获取文件夹下的一级文件及文件夹table
--参数: path——>遍历文件的路径
getFileList=function(path)
	
	local a = io.popen("dir "..path.."/");
	local fileTable = {};
	
	if a==nil then
		
	else
		for l in a:lines() do
			table.insert(fileTable,l)
		end
	end
	return fileTable;
end;

再看看本帖的脚本:

提取重要信息:

getDiceList = function(msg)
    local path = string.sub(msg.fromMsg,#'--getDiceList='+1)
    local Dir = path
    
    if path == 'nil' then Dir = nil end
    
    if Dir then 
        cmd = 'dir '.. getDiceDir()..'\\'..path..' /b >'..getDiceDir()..'//DiceList.txt'
        os.execute(cmd)
    else
        cmd = 'dir '.. getDiceDir()..' /b >'..getDiceDir()..'//DiceList.txt'
        os.execute(cmd)
    end
    
    return read_file(getDiceDir()..'//DiceList.txt','r','*a')..os.date('\n%x')..'\n文件夹与文件共 '..getLineCount(getDiceDir()..'//DiceList.txt')..' 个'

end 

二者对比,

我快被蠢到了

我快被蠢到了

我快被蠢到了

我快被蠢到了

为了计算文件个数我还真搞了个计算文件行数:

getLineCount = function(path)
local BUFSIZE = 2^13
local f = io.input(path)
local lc = 0

while true do
    local lines,rest = f:read(BUFSIZE,'*line')
    
    if not lines then break end
        if rest then lines = lines .. rest .. '\n' end
        local _,t = string.gsub(lines,"%S+","")
        _,t = string.gsub(lines,"\n","\n")
        lc = lc + t
    end

return lc
end

但是,重点是,用今天熬夜的代码,只需要一步就可以了,也就是 #table

我真的会谢,望大佬不要喷我这个帖子的脚本写的太烂orz

6 天 后
简律纯 将标题更改为 「【指令脚本/抛砖引玉】getFileList 获取文件夹列表—— os.execute/os.popen 的简单应用」。
说点什么吧...