Old 09-01-2015, 07:06 AM   #1
mpl
Human being with feelings
 
mpl's Avatar
 
Join Date: Oct 2013
Location: Moscow, Russia
Posts: 1,221
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)?
__________________
SoundCloud | MPL Scripts / ReaPack | Donate

Last edited by mpl; 09-01-2015 at 08:56 AM.
mpl is online now   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: 1,221
Default

Upd: doesn't work with Reaper x86
__________________
SoundCloud | MPL Scripts / ReaPack | Donate
mpl is online now   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: 6,377
Default

Doesn't work here but I don't even get an error message.
__________________
For info on SWS Reaper extension plugin (including Xenakios' previous extension/actions) :
http://www.sws-extension.org/
https://github.com/Jeff0S/sws
--
Xenakios blog (about HourGlass and λ) :
http://xenakios.wordpress.com/
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: 9,047
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: 1,221
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.
__________________
SoundCloud | MPL Scripts / ReaPack | Donate
mpl is online now   Reply With Quote
Old 09-16-2015, 08:59 AM   #6
schwa
Administrator
 
schwa's Avatar
 
Join Date: Mar 2007
Location: NY
Posts: 9,047
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: 6,377
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!
__________________
For info on SWS Reaper extension plugin (including Xenakios' previous extension/actions) :
http://www.sws-extension.org/
https://github.com/Jeff0S/sws
--
Xenakios blog (about HourGlass and λ) :
http://xenakios.wordpress.com/
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: 1,221
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)
__________________
SoundCloud | MPL Scripts / ReaPack | Donate
mpl is online now   Reply With Quote
Old 09-16-2015, 11:03 AM   #9
schwa
Administrator
 
schwa's Avatar
 
Join Date: Mar 2007
Location: NY
Posts: 9,047
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: 1,221
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!
__________________
SoundCloud | MPL Scripts / ReaPack | Donate
mpl is online now   Reply With Quote
Old 09-16-2015, 11:40 AM   #11
schwa
Administrator
 
schwa's Avatar
 
Join Date: Mar 2007
Location: NY
Posts: 9,047
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: 1,221
Default

Wow, how could I missed that?! Thank you very much.
__________________
SoundCloud | MPL Scripts / ReaPack | Donate
mpl is online now   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 01:43 AM.


Powered by vBulletin® Version 3.8.9
Copyright ©2000 - 2017, vBulletin Solutions, Inc.