Old 09-04-2015, 11:43 PM   #1
mpl
Human being with feelings
 
mpl's Avatar
 
Join Date: Oct 2013
Location: Moscow, Russia
Posts: 3,984
Default Get directory filelist with Lua io.popen - how?

ok, previously i posted this to a wrong developer forum (or not?) so I post this here again.


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()
Can I do something with os.execute?
mpl is offline   Reply With Quote
Old 09-05-2015, 12:26 AM   #2
spk77
Human being with feelings
 
Join Date: Aug 2012
Location: Finland
Posts: 2,668
Default

Maybe this helps (needs some editing to make it work correctly):

Get file names from current script path.lua

Code:
function msg(m)
  return reaper.ShowConsoleMsg(tostring(m) .. "\n")
end

function get_script_path()
  local info = debug.getinfo(1,'S');
  local script_path = info.source:match[[^@?(.*[\/])[^\/]-$]]
  --msg(script_path)
  return script_path
end

-- get "script path"
script_path = get_script_path()

-- Lua implementation of PHP scandir function
function scandir(directory)
    local i, t, popen = 0, {}, io.popen
    for filename in popen('dir "'..directory..'" /b'):lines() do
        msg(filename)
        i = i + 1
        t[i] = filename
    end
    return t
end

file_table = scandir(script_path)

--msg(package.path)
spk77 is offline   Reply With Quote
Old 09-05-2015, 12:43 AM   #3
mpl
Human being with feelings
 
mpl's Avatar
 
Join Date: Oct 2013
Location: Moscow, Russia
Posts: 3,984
Default

Same thing when appeal to io.popen -
Quote:
attempt to index a nil value
It needs value, ok. If I do something like
Code:
io.popen(directory)
I get
Quote:
attempt to call a nil value (local 'popen')
script_path is OK, to be clear. So, as i understood, reaper reads io.popen not as a LUA command, but as value in table.

This is I found also: http://www.promixis.com/forums/showt...-not-supported, it is about Lua 5.2, but now we have 5.3, so coult it be missing because of Lua limitations somehow?

Last edited by mpl; 09-05-2015 at 12:57 AM.
mpl is offline   Reply With Quote
Old 09-05-2015, 01:14 AM   #4
spk77
Human being with feelings
 
Join Date: Aug 2012
Location: Finland
Posts: 2,668
Default

What happens if you remove the last "\\" ?
path = reaper.GetExePath().."\\Grooves" .."\\"




Just noticed that this works:
grooves_path = [[C:\Users\Pavillion 11\AppData\Roaming\REAPER\Grooves]]
(this is the path to SWS groove files on my computer)




Code:
function msg(m)
  return reaper.ShowConsoleMsg(tostring(m) .. "\n")
end

grooves_path = [[C:\Users\Pavillion 11\AppData\Roaming\REAPER\Grooves]]

-- Lua implementation of PHP scandir function
function scandir(directory)
    local i, t, popen = 0, {}, io.popen
    for filename in popen('dir "'..directory..'" /b'):lines() do
        msg(filename)
        i = i + 1
        t[i] = filename
    end
    return t
end

file_table = scandir(grooves_path)
spk77 is offline   Reply With Quote
Old 09-05-2015, 01:34 AM   #5
mpl
Human being with feelings
 
mpl's Avatar
 
Join Date: Oct 2013
Location: Moscow, Russia
Posts: 3,984
Default

That is really strange. I just replace my folder into script and got still the same error. And there is no any cyrillic sybols in path, backslash at the end or something.
Quote:
attempt to index a nil value
on line
Code:
    for filename in popen('dir "'..directory..'" /b'):lines() do
Hope it is not because i`m on win7

Last edited by mpl; 09-05-2015 at 01:40 AM.
mpl is offline   Reply With Quote
Old 09-05-2015, 01:47 AM   #6
spk77
Human being with feelings
 
Join Date: Aug 2012
Location: Finland
Posts: 2,668
Default

I'm on Win8.1 64bit.

Made another test:
I made a folder (Grooves) and two files into it (Test file1.txt and Test file2.txt). "Grooves" folder is in the script path.


It's working for me :


Code:
function msg(m)
  return reaper.ShowConsoleMsg(tostring(m) .. "\n")
end

--p = reaper.GetExePath()
function get_script_path()
  local info = debug.getinfo(1,'S');
  local script_path = info.source:match[[^@?(.*[\/])[^\/]-$]]
  --msg(script_path)
  return script_path
end

-- get "script path"
script_path = get_script_path()
grooves_path = script_path .. "\\Grooves"

-- Lua implementation of PHP scandir function
function scandir(directory)
    local i, t, popen = 0, {}, io.popen
    for filename in popen('dir "'..directory..'" /b'):lines() do
        msg(filename)
        i = i + 1
        t[i] = filename
    end
    return t
end

--file_table = scandir(script_path)
file_table = scandir(grooves_path)
spk77 is offline   Reply With Quote
Old 09-05-2015, 01:57 AM   #7
mpl
Human being with feelings
 
mpl's Avatar
 
Join Date: Oct 2013
Location: Moscow, Russia
Posts: 3,984
Default

Variables is ok for me.

I also tried to run REAPER as administrator - same.

UPD. We find a bug.
Reaper 502pre3 x86 - doesn`t work
Reaper 502pre3 x64 - works.

Last edited by mpl; 09-05-2015 at 02:10 AM.
mpl is offline   Reply With Quote
Old 09-16-2015, 03:28 PM   #8
semiquaver
Human being with feelings
 
Join Date: Jun 2008
Posts: 4,923
Default

I get "'popen' not supported" on mac 64 5.01b3
semiquaver is offline   Reply With Quote
Old 09-17-2015, 05:09 PM   #9
semiquaver
Human being with feelings
 
Join Date: Jun 2008
Posts: 4,923
Default @ devs: is io.popen supported on mac??

I get a message "'popen' not supported.

Trying to use external processes for text io

thx!

Mac Reaper64

Last edited by semiquaver; 09-17-2015 at 05:18 PM.
semiquaver is offline   Reply With Quote
Old 09-17-2015, 08:42 PM   #10
X-Raym
Human being with feelings
 
X-Raym's Avatar
 
Join Date: Apr 2013
Location: France
Posts: 9,900
Default

It seems that the question was answered few posts ago ! :P
http://forum.cockos.com/showthread.php?t=166043

Sorry, wrong link, here is it is :
http://forum.cockos.com/showthread.php?t=165856
X-Raym is offline   Reply With Quote
Old 09-17-2015, 09:43 PM   #11
semiquaver
Human being with feelings
 
Join Date: Jun 2008
Posts: 4,923
Default

in that thread Schwa suggests that io.popen works fine and gives some windows code as an example...

... but here on mac I'm having no luck - so wondering if io.open is supported on windows only??

(the solution to mlp's problem doesn't solve mine sadly )
semiquaver is offline   Reply With Quote
Old 09-18-2015, 04:39 AM   #12
schwa
Administrator
 
schwa's Avatar
 
Join Date: Mar 2007
Location: NY
Posts: 15,815
Default

Merged the threads. There may be a compile issue with enabling Lua's popen on OS X x64, we'll look at it.

For directory listing, this will be moot soon, as we have added file listing functions to the REAPER API for the next build.

For user input, there are a few basic functions in the API already.
schwa is offline   Reply With Quote
Old 09-18-2015, 04:46 AM   #13
planetnine
Human being with feelings
 
planetnine's Avatar
 
Join Date: Oct 2007
Location: Lincoln, UK
Posts: 7,942
Default

Quote:
Originally Posted by schwa View Post
...

For directory listing, this will be moot soon, as we have added file listing functions to the REAPER API for the next build.

...

Like the sound of that



>
__________________
Nathan, Lincoln, UK. | Item Marker Tool. (happily retired) | Source Time Position Tool. | CD Track Marker Tool. | Timer Recording Tool. | dB marks on MCP faders FR.
planetnine is offline   Reply With Quote
Old 09-18-2015, 05:02 AM   #14
schwa
Administrator
 
schwa's Avatar
 
Join Date: Mar 2007
Location: NY
Posts: 15,815
Default

... fixed popen on OSX for the next build, too.
schwa is offline   Reply With Quote
Old 09-18-2015, 05:14 AM   #15
X-Raym
Human being with feelings
 
X-Raym's Avatar
 
Join Date: Apr 2013
Location: France
Posts: 9,900
Default

Oh yes that sounds very great, it open a lot of possibility !
Thanks
X-Raym is offline   Reply With Quote
Old 09-18-2015, 06:31 AM   #16
semiquaver
Human being with feelings
 
Join Date: Jun 2008
Posts: 4,923
Default

thanks Schwa !
semiquaver is offline   Reply With Quote
Old 07-15-2017, 05:15 PM   #17
deeb
Human being with feelings
 
deeb's Avatar
 
Join Date: Feb 2017
Posts: 4,820
Default

Quote:
Originally Posted by schwa View Post
For directory listing, this will be moot soon, as we have added file listing functions to the REAPER API for the next build.
hello schwa! where can i read about it?


i am trying to get spk77 example working: but scandir does not msg() anything

tried:
for filename in popen('dir "'..directory..'" /b'):lines() do

also:
for filename in popen('"'..directory..'" /b'):lines() do

i don't find way to get inside the for cycle

Thank you

Last edited by deeb; 07-15-2017 at 08:24 PM.
deeb is offline   Reply With Quote
Old 07-15-2017, 06:35 PM   #18
karbomusic
Human being with feelings
 
karbomusic's Avatar
 
Join Date: May 2009
Posts: 29,269
Default

Quote:
Originally Posted by deeb View Post
hello schwa! where can i read about it?
Have you ran this action yet?

"[developer]Write C++ API functions header"

That tends to have the definitions of all the API functions but since I haven't programmed with Reaper lately, not sure how much it helps. Looks like what you are doing up ^there may be lua syntax related but there is stuff in the API about file listings and lua.

Code:
// EnumerateFiles
// List the files in the "path" directory. Returns NULL (or empty string, in Lua) when all files have been listed. See EnumerateSubdirectories
__________________
Music is what feelings sound like.
karbomusic is offline   Reply With Quote
Old 07-15-2017, 08:37 PM   #19
deeb
Human being with feelings
 
deeb's Avatar
 
Join Date: Feb 2017
Posts: 4,820
Default

wow man! thanks that's it! very neat
deeb 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 04:37 PM.


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