Wie man Liste der Verzeichnisse in Lua erhält

Ich brauche eine Liste von Verzeichnis in LUA

Angenommen ich habe ein Verzeichnis-Pfad "C:\Program Dateien"

Brauche ich eine Liste aller Ordner in einem bestimmten Pfad und wie nach jedem bestimmten Ordner in diese Liste.

Beispiel

Benötigen eine Liste aller Ordner im Pfad "C:\Program Dateien"

Unten sind Ordner-Namen, die in den oben genannten Pfad

  1. test123
  2. test4567
  3. Ordner 123
  4. Ordner 456
  5. Ordner 456 789

    Brauchen die oben in einer Liste und dann auf die Suche nach einer bestimmten Zeichenfolge, wie Ordner 456 im Ordner 456 789 nur.

Haben Versucht folgenden code. Etwas, was ich bin fehlt unten:-

local function Loc_Lines( str )
--
local ret= {}   -- 0 lines

while str do
    local _,_,line,tail= string.find( str, "(.-)\n(.+)" )
    table.insert( ret, line or str )
    str= tail
  Print (str)
end

return ret
end


local function Loc_ShellCommand( cmd )
--
local str= nil

    --
    local f= io.popen( cmd )    -- no command still returns a handle :(
     if f then

        str= f:read'*a'
    Print(str)
        f:close()
    end

    if str=="" then   -- take no output as a failure (we can't tell..)
    Print("hi")
        str= nil
    end

-- Remove terminating linefeed, if any (eases up one-line analysis)
--
if str then
    if string.sub( str, -1 ) == '\n' then
        str= string.sub( str, 1, -2 )
    end
end

return str
 end


 local function Loc_DirCmd( cmd )

 Print(cmd)

  local str= Loc_ShellCommand( cmd )



 return Loc_Lines(str)
 end


local function Loc_DirList( dirname )

 local ret= {}

    local lookup= {}

   local tbl= Loc_DirCmd( "dir /AD /B "..dirname )   -- only dirs

    -- Add slash to every dir line
    --
    for i,v in ipairs(tbl) do
        table.insert( ret, v..'\\' )
        lookup[v]= true
    end       


    -- Return with forward slashes
    --
    if true then
        for i=1,table.getn(ret) do
            ret[i]= string.gsub( ret[i], '\\', '/' )
     Print (ret[i])
        end
    end


   return ret
 end


 Loc_DirList("C:\\Program Files\\")

InformationsquelleAutor der Frage che | 2011-03-14

Schreibe einen Kommentar