Support and General Use > Plugins/Viewers
Directory operations in Lua script?
Crend King:
Hello. I would like to write a little Lua script to do some file and directory operations in Rockbox 3.3. Although the plugin.h contains functions like mkdir(), rmdir() and dir_exists(), I do not see them in the Lua rb table. rb.remove() on a directory returns -1. The only function I found successful on directory is rename(). So I guess the directory operations are not supported yet, right?
Or is there up-to-date Lua reference documentation for Rockbox? I found one at http://mcuelenaere.alwaysdata.net/rockbox_api/, but there is no file/directory functions listed. Although they are listed when I switch to show undocumented functions, I cannot tell which ones are supported in the current Rockbox. It seems this site is not up-to-date.
Thanks!
Grahack:
dir_exists() is in the rb table. I don't know why rmdir and mkdir don't appear here.
From my personal investigations, rb.remove(path) deletes files and *empty* dirs.
To know what is and what is not available, I wrote a small script available here.
Crend King:
Thanks for reply. Unfortunately in my Rockbox 3.3 copy, rb.remove() does not remove empty directories, but only files. Also, dir_exists() does not exist in my copy. Which version do you have?
I know how to iterate and display the members in rb table. I'm more interested in the script from your link that dump the members to a rb.txt. But when I run it in the UiSimulator, it reports error: /test.lua:20: attempt to index global 'io' (a nil value). Instead, I slightly modified the code to
--- Code: ---rb_obj = {}
for k, v in pairs(rb) do
local the_type = type(v)..'s'
if not rb_obj[the_type] then
rb_obj[the_type] = {}
end
table.insert(rb_obj[the_type], k)
end
local list = {}
for rb_type, rb_table in pairs(rb_obj) do
table.insert(list, string.format('* %s *', rb_type))
table.sort(rb_table)
for _, v in ipairs(rb_table) do
table.insert(list, v)
end
-- jump a line
table.insert(list, '')
end
local file = rb.open('/rb.txt', rb.TRUNC or rb.O_WRONLY)
rb.write(file, table.concat(list, '\n'))
rb.close(file)
--- End code ---
and successfully dumped out the members. The attachment is the rb.txt. I wonder besides rb and string, is there any other global objects available in Rockbox Lua?
Frankly what I want is to remove the directories generated by the Apple iPod OS, which is loaded when I connect my iPod to the computer and forget to unlock to switch. It is easy to easy to write Windows batch file to accomplish this, but it requires another connection with computer. It would be much more convenient to run a program or script natively in Rockbox to do this, and Lua comes to my screen as a simpler solution. Thus, I actually do not care about dir_exists(), but more interested in dir_remove() or rmdir().
Grahack:
For all this kind of work: test, fix, test, fix... the only version that is worth having is the current build: http://build.rockbox.org/
The io table was written after the 3.3 release.
Now if you want to delete a non empty directory, maybe you need a recursive function that you need to write from scratch. Or maybe there is already something in the C Rockbox code for this that I'm not aware of.
safetydan:
Not that I'd want discourage use of the Lua plugin, because it would be great to see some real world usage, but there's already a plugin in Rockbox that will do what you want. You can configure the Disk Tidy plugin to delete additional files when it runs.
Navigation
[0] Message Index
[#] Next page
Go to full version