Help - Search - Members - Calendar
Full Version: Example: Count files in directory
Intellipool Network Monitor - Forum > Intellipool Network Monitor > Lua scripts
RA
Download a free trial of Intellipool Network Monitor today!
More information about Intellipool Network Monitor !



CODE
----------------------------------------------------------------------------------------------------------
-- Name: Filecounter.lua
-- Author: Intellipool AB
-- Version: 1.0
-- Date: 2006-03-03
-- Description: Counts number of files in a directory
-- Arguments:
-- Directory path
-- Max number of files
----------------------------------------------------------------------------------------------------------

-- User must supply path and max number of files in directory
iArgumentCount = GetArgumentCount()
if iArgumentCount < 2 then
  SetExitStatus("To few arguments",false)
  return
end

-- Get path and max number of files
sFilePath = GetArgument(0)
iMaxNumberOfFiles = tonumber(GetArgument(1))

file = TLuaFile:new()
sFileList = file:GetFileList(sFilePath ,"*.*")

-- "count" files,string returned will contain all files in the directory, separated by CR
iLen = string.len(sFileList);
iFileCount =0
for count = 0, iLen do
    cCurrentChar = string.byte(sFileList,count);
    if cCurrentChar == 10 then
 iFileCount = iFileCount +1;
    end
end
file:Close();

-- Do "test"
print(iFileCount);
if iFileCount < iMaxNumberOfFiles then
    SetExitStatus("Test ok",true);
else
    SetExitStatus("More files in directory then allowed",false);
end
Guest_Eric_*
I have modified this script to show realtime file count and create a graph of it:

CODE
----------------------------------------------------------------------------------------------------------
-- Name: Filecounter.lua
-- Authors: Eric Strauss
-- Version: 1.1
-- Date: 2006-03-06
-- Description: Counts number of files in a directory
-- Arguments:
-- Directory path
-- Max number of files
-- Separate args with spaces
-- Unix filesystem "/" not "\"
----------------------------------------------------------------------------------------------------------

-- User must supply path and max number of files in directory
iArgumentCount = GetArgumentCount()
if iArgumentCount < 2 then
   SetExitStatus("To few arguments",false)
   return
end

-- Get path and max number of files
sFilePath = GetArgument(0)
iMaxNumberOfFiles = tonumber(GetArgument(1))

file = TLuaFile:new()
sFileList = file:GetFileList(sFilePath ,"*.*")

-- "count" files,string returned will contain all files in the directory, separated by CR
iLen = string.len(sFileList);
iFileCount =0
for count = 0, iLen do
   cCurrentChar = string.byte(sFileList,count);
   if cCurrentChar == 10 then
      iFileCount = iFileCount +1;
   end
end
file:Close();

-- Write stats
StoreStatisticalData(0,iFileCount,iMaxNumberOfFiles,"Files");

-- Do "test"
print(iFileCount);
if iFileCount < iMaxNumberOfFiles then    
   SetExitStatus("Test OK, Files: ".. tostring(iFileCount).." of "..tostring(iMaxNumberOfFiles),true);  
else
   SetExitStatus("Too Many Files: ".. tostring(iFileCount).." of "..tostring(iMaxNumberOfFiles),false);
end
David Spindler
Hi,

we use this lua scripts for 4 monitors. 3 Monitors works correct and without any problem but from one monitor I get this message

"Syntax error in LUA script count files in dir.lua, [string ""]:40: error in function 'StoreStatisticalData'. argument #3 is 'nil'; 'number' expected."

I'm wondering because If there is a syntax error in the script, I should get this message from all monitors or not?

Any ideas to solve the problem?

Kind regards,

David
RA
have you specifed the second argument as a number ?

The nil values comes from the variable iMaxNumberOfFiles that is created by GetArgument.

Sorry about the very late reply.
David Spindler
No problem! It's not a hugh problem for us.

I have specified this:

C:\Program Files\Achab\Archive Server for MDaemon\engine\queue 200

Is it possible that the spaces are the problem?
RA
Try to enclose the path in quotation marks.
David Spindler
Sorry for my late response...

unfortunally quotation marks doesn't help...

any other ideas?
eric
Have you tried "/" instead of "\" in your path?
Just an idea...
afaure
Is it possible to use UNC to specify folder name ?
RA
No, the path is relative to the object that executes the script.
danielh
For me, it seems like this script broke when upgrading to the latest version of INM.
Getting the following error message on both my monitors using this script.

Syntax error in LUA script Filecounter.lua, [string ""]:37: error in function 'StoreStatisticalData'.
argument #1 is 'nil'; 'number' expected.
RA
Seems like a small problem with the export. Will fix in next patch.

Until then substitute the following constant with the corrsponding numbers:

LUA_RECORDSET_1 = 0

LUA_RECORDSET_2 = 1

LUA_RECORDSET_3 = 2

LUA_RECORDSET_4 = 3
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2010 Invision Power Services, Inc.