Old 09-01-2015, 07:06 AM   #1
mpl
Human being with feelings
 
mpl's Avatar
 
Join Date: Oct 2013
Location: Moscow, Russia
Posts: 4,177
Default io.popen - is it working with Lua?

Hi.
I need to store table of all files in folder, for example /Reaper/Grooves.
Cant get this works. Can anyone help?

Code:
files_t = {}
path = reaper.GetExePath().."\\Grooves" .."\\"
for file in io.popen(path):lines() do     
  table.insert(files_t, file)
end
Error: "attempt to index a nil value"

I also tried this (don`t understand synthax, here could be something wrong), same error:
Code:
io.popen([[dir "]]..path..[[" /b]]):lines()
ps: is this forum с++ only, or developer forum also means scripters place ( ReaScript: API: Wish List thread, first questions of The "how do I?" REAPER plug-in extension thread)?

Last edited by mpl; 09-01-2015 at 08:56 AM.
mpl is offline   Reply With Quote
Old 09-16-2015, 01:56 AM   #2
mpl
Human being with feelings
 
mpl's Avatar
 
Join Date: Oct 2013
Location: Moscow, Russia
Posts: 4,177
Default

Upd: doesn't work with Reaper x86
mpl is offline   Reply With Quote
Old 09-16-2015, 02:44 AM   #3
Xenakios
Human being with feelings
 
Xenakios's Avatar
 
Join Date: Feb 2007
Location: Oulu, Finland
Posts: 8,062
Default

Doesn't work here but I don't even get an error message.
__________________
I am no longer part of the REAPER community. Please don't contact me with any REAPER-related issues.
Xenakios is offline   Reply With Quote
Old 09-16-2015, 04:31 AM   #4
schwa
Administrator
 
schwa's Avatar
 
Join Date: Mar 2007
Location: NY
Posts: 16,501
Default

I don't think you want io.popen, you just want io.open. What your code is doing is trying to execute the list of files and read the output of that nonexistent program, instead of just reading the list of files.

http://www.lua.org/manual/5.3/manual.html#6.8

io.open and file:lines seem to be working properly.
schwa is offline   Reply With Quote
Old 09-16-2015, 07:59 AM   #5
mpl
Human being with feelings
 
mpl's Avatar
 
Join Date: Oct 2013
Location: Moscow, Russia
Posts: 4,177
Default

Popen works for me only with Reaper x 64.
Schwa, can you explain how to get list of files in directory, using io.open?
What I need is just store filenames in directory to table, no need to read every file content, just names.
mpl is offline   Reply With Quote
Old 09-16-2015, 08:59 AM   #6
schwa
Administrator
 
schwa's Avatar
 
Join Date: Mar 2007
Location: NY
Posts: 16,501
Default

Sorry, I thought you were opening a file that already contained a list of filenames. If you need to list the files within a directory, you will need popen, which is really just way to call a shell command and capture the output.

This should work on Windows. It's equivalent to opening a shell and executing "dir path /b". Other platforms will need different shell commands.

Code:
path=reaper.GetResourcePath() .. "\\Grooves"
filelist=io.popen('dir "' .. path .. '" /b')
for filename in filelist:lines() do
  reaper.ShowConsoleMsg(filename ..'\n')
end
schwa is offline   Reply With Quote
Old 09-16-2015, 09:41 AM   #7
Xenakios
Human being with feelings
 
Xenakios's Avatar
 
Join Date: Feb 2007
Location: Oulu, Finland
Posts: 8,062
Default

Would be good if Reaper provided some direct APIs for these pain points...Frankly it's ridiculous one has to execute the operating system directory listing command as an external process to get the file names out of a directory!
__________________
I am no longer part of the REAPER community. Please don't contact me with any REAPER-related issues.
Xenakios is offline   Reply With Quote
Old 09-16-2015, 10:55 AM   #8
mpl
Human being with feelings
 
mpl's Avatar
 
Join Date: Oct 2013
Location: Moscow, Russia
Posts: 4,177
Default

Schwa, that is what I`m talking about

Quote:
attempt to index a nil value (global 'filelist')
But it is working ok with any Reaper x64.

(I`m on Win7x64)
mpl is offline   Reply With Quote
Old 09-16-2015, 11:03 AM   #9
schwa
Administrator
 
schwa's Avatar
 
Join Date: Mar 2007
Location: NY
Posts: 16,501
Default

My code example works here on Windows 7 x86 and x64. But shell commands by definition won't be reliably portable. Like Xenakios says, the only way for this to be fully portable would be for us to add file management functions to the API.

Last edited by schwa; 09-16-2015 at 11:15 AM.
schwa is offline   Reply With Quote
Old 09-16-2015, 11:20 AM   #10
mpl
Human being with feelings
 
mpl's Avatar
 
Join Date: Oct 2013
Location: Moscow, Russia
Posts: 4,177
Default

Agree. Yes, native implementation of these things would be good.

Hmm, very strange, can`t get it works. Well, anyway, thanks.

Just to say, I need this for my Quantize tool. To let user choose SWS groove from GUI list. If possible to get file from browser "Open file" window, please let me know. Thanks again!
mpl is offline   Reply With Quote
Old 09-16-2015, 11:40 AM   #11
schwa
Administrator
 
schwa's Avatar
 
Join Date: Mar 2007
Location: NY
Posts: 16,501
Default

http://www.reaper.fm/sdk/reascript/r...ileNameForRead
schwa is offline   Reply With Quote
Old 09-16-2015, 12:06 PM   #12
mpl
Human being with feelings
 
mpl's Avatar
 
Join Date: Oct 2013
Location: Moscow, Russia
Posts: 4,177
Default

Wow, how could I missed that?! Thank you very much.
mpl is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -7. The time now is 12:33 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.