當前位置:編程學習大全網 - 源碼下載 - 求Lua插件枚舉安卓系統指定目錄下文件名實例

求Lua插件枚舉安卓系統指定目錄下文件名實例

用lua file system 這個庫,在lua項目主頁可以找到,或者去谷歌。

具體用法在它的說明文檔裏有。

妳可以看這個庫的例子,裏面有教妳查看文件的創建日期與修改日期。

下面是例子:

------------------------------------------------------

local tmp = "/tmp"

local sep = "/"

local upper = ".."

require"lfs"

print (lfs._VERSION)

function attrdir (path)

--這個就是妳要的目錄叠代器

for file in lfs.dir(path) do

if file ~= "." and file ~= ".." then

local f = path..sep..file

print ("\t=> "..f.." <=")

--這壹行打印出所有文件,妳可以改成妳想要的

local attr = lfs.attributes (f)

assert (type(attr) == "table")

if attr.mode == "directory" then

attrdir (f)

else

for name, value in pairs(attr) do

print (name, value)

end

end

end

end

end

---下面是其他的例子,如獲取文件屬性

-- Checking changing directories

local current = assert (lfs.currentdir())

local reldir = string.gsub (current, "^.*%"..sep.."([^"..sep.."])$", "%1")

assert (lfs.chdir (upper), "could not change to upper directory")

assert (lfs.chdir (reldir), "could not change back to current directory")

assert (lfs.currentdir() == current, "error trying to change directories")

assert (lfs.chdir ("this couldn*t be an actual directory") == nil, "could change to a non-existent directory")

-- Changing creating and removing directories

local tmpdir = current..sep.."lfs_tmp_dir"

local tmpfile = tmpdir..sep.."tmp_file"

-- Test for existence of a previous lfs_tmp_dir

-- that may have resulted from an interrupted test execution and remove it

if lfs.chdir (tmpdir) then

assert (lfs.chdir (upper), "could not change to upper directory")

assert (os.remove (tmpfile), "could not remove file from previous test")

assert (lfs.rmdir (tmpdir), "could not remove directory from previous test")

end

-- tries to create a directory

assert (lfs.mkdir (tmpdir), "could not make a new directory")

local attrib, errmsg = lfs.attributes (tmpdir)

if not attrib then

error ("could not get attributes of file `"..tmpdir.."*:\n"..errmsg)

end

local f = io.open(tmpfile, "w")

f:close()

-- Change access time

local testdate = os.time({ year = 2007, day = 10, month = 2, hour=0})

assert (lfs.touch (tmpfile, testdate))

local new_att = assert (lfs.attributes (tmpfile))

assert (new_att.access == testdate, "could not set access time")

assert (new_att.modification == testdate, "could not set modification time")

-- Change access and modification time

local testdate1 = os.time({ year = 2007, day = 10, month = 2, hour=0})

local testdate2 = os.time({ year = 2007, day = 11, month = 2, hour=0})

assert (lfs.touch (tmpfile, testdate2, testdate1))

local new_att = assert (lfs.attributes (tmpfile))

assert (new_att.access == testdate2, "could not set access time")

assert (new_att.modification == testdate1, "could not set modification time")

local res, err = lfs.symlinkattributes(tmpfile)

if err ~= "symlinkattributes not supported on this platform" then

-- Checking symbolic link information (does not work in Windows)

assert (os.execute ("ln -s "..tmpfile.." _a_link_for_test_"))

assert (lfs.attributes"_a_link_for_test_".mode == "file")

assert (lfs.symlinkattributes"_a_link_for_test_".mode == "link")

assert (os.remove"_a_link_for_test_")

end

if lfs.setmode then

-- Checking text/binary modes (works only in Windows)

local f = io.open(tmpfile, "w")

local result, mode = lfs.setmode(f, "binary")

assert((result and mode == "text") or (not result and mode == "setmode not supported on this platform"))

result, mode = lfs.setmode(f, "text")

assert((result and mode == "binary") or (not result and mode == "setmode not supported on this platform"))

f:close()

end

-- Restore access time to current value

assert (lfs.touch (tmpfile, attrib.access, attrib.modification))

new_att = assert (lfs.attributes (tmpfile))

assert (new_att.access == attrib.access)

assert (new_att.modification == attrib.modification)

-- Remove new file and directory

assert (os.remove (tmpfile), "could not remove new file")

assert (lfs.rmdir (tmpdir), "could not remove new directory")

assert (lfs.mkdir (tmpdir..sep.."lfs_tmp_dir") == nil, "could create a directory inside a non-existent one")

-- Trying to get attributes of a non-existent file

assert (lfs.attributes ("this couldn*t be an actual file") == nil, "could get attributes of a non-existent file")

assert (type(lfs.attributes (upper)) == "table", "couldn*t get attributes of upper directory")

-- Stressing directory iterator

count = 0

for i = 1, 4000 do

for file in lfs.dir (tmp) do

count = count + 1

end

end

print"Ok!"

  • 上一篇:如何監控計算機文件有沒有被復制到U盤或者上傳到網絡中
  • 下一篇:急求 MD5的加密解密算法,用C++實現的源代碼 高分答謝
  • copyright 2024編程學習大全網