Help - Search - Members - Calendar
Full Version: Script - Subfolder Size
Intellipool Network Monitor - Forum > Intellipool Network Monitor > Lua scripts
Jazz
Hello Again,

Got Another. This One is relatively new so there is room so there is plenty of room for refinement. The Script Recursively Monitors File-size of a Sub folder on Windows Operating System. Can be used to Monitor User Directories, Application Folders etc. Although its been tested on folders over half a Terabyte it does take some time to complete when dealing with Folders of that size. ** Untested on Windows Vista/Server 2008 **.

Arguments : Windows Folder Path to Be monitored < 'C:\intellipool' >

Thanks,

/jAzz

-- Comments and Suggestions Welcome


CODE
--[[
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
--]]

------------------------------------------------------------------------------------------------

Name = "SubFolderSize"
Author = " Jazz Alyxzander Turner-Baggs "
VersionMaj = 2
VersionMin = 1
Date = "December 16th, 2008"
Description = "Recursively Enumates Diskspace used by a Sub directory"

----------------------------------------------------------------------------------------------------------

function OnConfigure()

    Config = LuaScriptConfigurator()
    Config:SetAuthor(Author)
    Config:SetDescription(Description)
    Config:SetMinBuildVersion(0)
    Config:SetScriptVersion(VersionMaj, VersionMin )

    Config:AddArgument("Path","Stanard Path of files and subfolders to be iterated  Ex<c:\intellipool>",LuaScriptConfigurator.CHECK_NOT_EMPTY)
    Config:SetEntryPoint("main")
    return Config
end

----------------------------------------------
-- Global Vars
---------------------------------------------

-- Main Recursive Function

--[[
1) Get Subfolders
2) Recurse on all subfolders,
3) Sum size of files
4) return.
--]]

function FileRecurse(path, k)                              -- Path = subdirectory , k = folderCount

    local FILE = TLuaFile()
    local folderList = FILE:GetDirectoryList(path)
    local fileList = FILE:GetFileList(path, '*.*')
    local fileSize = 0                                        -- Init Local Size to zero
    local folder = ""
    
        
-- Recurse Folders
      if folderList ~= "" then
        
          while folderList ~= ""  do
           folderList, folder = stringIterator(folderList, "\n")   -- Pop Head from String-Queue
           tmpsize, k = FileRecurse(folder,k)
           fileSize = fileSize + tmpsize                                -- Add Sub folder Size To Running Folder Sum
                  
        end
    end
-- Sum Files
    while fileList ~= "" do
        fileList, file = stringIterator(fileList, "\n")
        if file ~= "" then
           fileSize = fileSize + FILE:GetFileSize(file)              -- Add File Size to running Folder Size
        end
    end

    return fileSize, k+1
    
end

-- Pops First Token from string , by given deliminator.
function stringIterator(s1, delim)
    if s1 == nil then
        return "",""
    end
    local iCurr = string.find(s1 , "\n")
    if iCurr == nil then
      return "",""
    end
    local filePath = string.sub(s1,0,iCurr-1)
    s1 = string.sub(s1,iCurr+1)
    return s1,filePath
end
----------------------------------------------

function main()

  Total = -1                                      -- Total Size init -1
  iConversionFactor =  2 ^ 20            -- Btyes to Output Conversion
  iSECONDS_CONVERSION = 1000     -- Seconds to Output Conversion
  path = GetArgument(0)                  

    Timer = TLuaTimer()
    Timer:Start()
    FileSize, FolderCount = FileRecurse(path,0)
    FileSize = FileSize / iConversionFactor
    time = Timer:Stop() / iSECONDS_CONVERSION
    StoreStatisticalData(1,FileSize,0,'MB')
        StoreStatisticalData(2,time,0,'Seconds')
    SetExitStatus("Last Test(" .. FolderCount .. "): " .. FileSize .. " MB's - " .. time .. " seconds",true)

end
tboggs13
So I took this script and ran with it, tweaking a few things up to suit my needs. Primarily, I needed to add a file count along with the folder count and size of the sub-folder. I also dropped storage of the time to perform the test, but kept it on the status line for reference. My level of programming would not have allowed me to create this from scratch, without taking a great deal of time, so I to give Jazz much thanks for this.

CODE
--[[
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
--]]

------------------------------------------------------------------------------------------------

Name = "SubFolderSizeCount"
Author = " Jazz Alyxzander Turner-Baggs "
VersionMaj = 3
VersionMin = 1
Date = "March 2nd, 2009"
Description = "Recursively Enumates Diskspace and File/Folder count in a directory tree"

----------------------------------------------------------------------------------------------------------

function OnConfigure()

    Config = LuaScriptConfigurator()
    Config:SetAuthor(Author)
    Config:SetDescription(Description)
    Config:SetMinBuildVersion(0)
    Config:SetScriptVersion(VersionMaj, VersionMin )

    Config:AddArgument("Path","Stanard Path of files and subfolders to be iterated  Ex<c:\intellipool>",LuaScriptConfigurator.CHECK_NOT_EMPTY)
    Config:SetEntryPoint("main")
    return Config
end

----------------------------------------------
-- Global Vars
---------------------------------------------

-- Main Recursive Function

--[[
1) Get Subfolders
2) Recurse on all subfolders and count folders
3) Sum size of files and count number of files
4) return.
--]]

function FileRecurse(path, k, l)                              -- Path = subdirectory , k = folderCount, l = fileCount

    local FILE = TLuaFile()
    local folderList = FILE:GetDirectoryList(path)
    local fileList = FILE:GetFileList(path, '*.*')
    local fileSize = 0                                        -- Init Local Size to zero
    local folder = ""
    
        
-- Recurse Folders
    if folderList ~= "" then
       while folderList ~= ""  do
           folderList, folder = stringIterator(folderList, "\n")   -- Pop Head from String-Queue
           tmpsize, k, l = FileRecurse(folder,k,l)
           k = k + 1
           fileSize = fileSize + tmpsize                                -- Add Sub folder Size To Running Folder Sum
       end
    end
-- Sum Files
    while fileList ~= "" do
       fileList, file = stringIterator(fileList, "\n")
       l = l + 1
       if file ~= "" then
          fileSize = fileSize + FILE:GetFileSize(file)              -- Add File Size to running Folder Size
       end
    end

    return fileSize, k, l
    
end

-- Pops First Token from string , by given deliminator.
function stringIterator(s1, delim)
    if s1 == nil then
        return "",""
    end
    local iCurr = string.find(s1 , "\n")
    if iCurr == nil then
      return "",""
    end
    local filePath = string.sub(s1,0,iCurr-1)
    s1 = string.sub(s1,iCurr+1)
    return s1,filePath
end
----------------------------------------------

function main()
    Total = -1                                      -- Total Size init -1
    iConversionFactor =  2 ^ 20            -- Btyes to Output Conversion
    sizeFormat = 10 ^ 1
    iSECONDS_CONVERSION = 1000     -- Seconds to Output Conversion
    path = GetArgument(0)                  
    Timer = TLuaTimer()
    Timer:Start()
    FileSize, FolderCount, FileCount = FileRecurse(path,0,0)
    FileSize = FileSize / iConversionFactor
    FileSizeMB = math.floor( FileSize*sizeFormat + 0.5) / sizeFormat
    time = Timer:Stop() / iSECONDS_CONVERSION
    StoreStatisticalData(0,FileSize,0,'MB')
    StoreStatisticalData(1,FileCount,0,'Files')
    StoreStatisticalData(2,FolderCount,0,'Folders')
    SetExitStatus("Found " .. FolderCount .. " Folders, " .. FileCount .. " Files, " .. FileSizeMB .. " MB's in " .. time .. " seconds",true)
end


I will agree it is slow, taking 5-7 second to count 3300 files, but it does get the job done. I do wonder what kind of load it puts on the system for those seven seconds.
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.