PtokaX forum

Archive => Archived 4.0 boards => Request for Lua 4 scripts => Topic started by: ??????Hawk?????? on 20 April, 2004, 22:03:46

Title: !! File Count Function Needed !!
Post by: ??????Hawk?????? on 20 April, 2004, 22:03:46
Heya Peeps.  Hope someone can help me out ..

im wanting a function to return all file names in a specified directory..

something like this:-


Function ReadFiles()
directory = "/files"
filename = "/r/n"
number = count files          // count the number of files in ' directory '
Loop                                  // loop 1 to number
get next filename          
filename = fileName.."/r/n"
end                                   // end loop
return FileName , number
end                             // end function




Thanks in Advance  ??????Hawk??????
Title:
Post by: NotRabidWombat on 20 April, 2004, 23:03:21
function CountFiles(sDirectory)
   local sFileName = tmpname ();

   system("dir /b /a-d \""..sDirectory.."\" | find /c /v \"\" > \""..sFileName.."\"");
   local hFile = openfile (sFileName, "r");

   local iCount = read(hFile, "*n");

   closefile(sFileName);

   remove(sFileName);
end
Nothing to test on here. Hope it works, or you understand what I was going for.

Hrm, actually looks like you want a listing of files?

-NotRabidWombat
Title:
Post by: ??????Hawk?????? on 20 April, 2004, 23:14:55
yes m8  .  it is a listing of  files  that im after..

thx  anyway
Title:
Post by: NotRabidWombat on 20 April, 2004, 23:21:46
Alrighty, in that case.
function ReadFiles(sDirectory)
   local sFileName = tmpname ();
   local sLine = nil;

   system("dir /b /a-d \""..sDirectory.."\" > \""..sFileName.."\"");
   local hFile = openfile (sFileName, "r");

   sLine = read(hFile, "*l");
   while ( sLine )
      -- sLine will be the file name in sDirectory
      sLine = read(hFile, "*l");
   end

   closefile(sFileName);

   remove(sFileName);
end

-NotRabidWombat
Title:
Post by: ??????Hawk?????? on 20 April, 2004, 23:48:11
not having any joy with that function m8  :-(
Title:
Post by: NotRabidWombat on 21 April, 2004, 00:09:53
Sorry. Right now I'm using the Wombot interpreter to check syntax ;-)

I'll be back home in an hour at which point I can debug.

-NotRabidWombat
Title:
Post by: ??????Hawk?????? on 21 April, 2004, 00:17:01
hi m8 thx

 i appreciate your help  ..  theres  no rush as ive closed my script boxes down for the evening..

but i would appreciate it if you could come up with something that followed this pattern returning the info required..


Function ReadFiles()
directory = "/files"
filename = "/r/n"
number = count files          // count the number of files in ' directory '
Loop                                  // loop 1 to number
get next filename          
filename = fileName.."/r/n"
end                                   // end loop
return FileName , number
end                             // end function

cheers  ??????Hawk??????
Title:
Post by: plop on 21 April, 2004, 03:04:31
i bet system is c++.
changed it 2 execute and added a do.
function ReadFiles(sDirectory)
   local sFileName = tmpname ();
   local sLine = nil;

   execute("dir /b /a-d \""..sDirectory.."\" > \""..sFileName.."\"");
   local hFile = openfile (sFileName, "r");

   sLine = read(hFile, "*l");
   while ( sLine ) do
      -- sLine will be the file name in sDirectory
      sLine = read(hFile, "*l");
   end

   closefile(sFileName);

   remove(sFileName);
end
plop
Title:
Post by: NotRabidWombat on 21 April, 2004, 04:07:55
Heh! :D It has been a while since I've used lua.

Yes system is c/c++.

-NotRabidWombat
Title:
Post by: ??????Hawk?????? on 22 April, 2004, 01:41:10
hi guys ..   thx for your help .  

the function works but  i get this error:-

Syntax error: bad argument #1 to `closefile' (invalid file handle)
stack traceback:

and the tmp.$$$   file is left in the scripts directory.
it makes a new tmp2.$$$  /  tmp3.$$$  and so on every time its executed.