[Lua]获取文件夹大小

默认分类,技术讨论 442
默认分类,技术讨论 442

发现Lua有获取文件字节和大小的代码,却没有获取文件夹底下所有文件的大小的代码,所以自己写了一个函数,分享给大家以及留作备份。

本病人在这一行还是新手,写得不好或者有错误的见谅一下

原理:
遍历循环并获取文件夹底下所有文件的字节大小,之后进行换算,返回结果

代码如下:

--获取文件夹大小
function getFolderSize(folderPath,conversion)
  import "java.io.*"
  local size = 0
  local fileList = luajava.astable(File(folderPath).listFiles())

  if(fileList == nil) then
    return 0
  end

  --开始遍历循环获取文件夹底下所有文件的字节大小
  if(fileList ~= nil) then
    for count=1,#fileList do
      if(File(tostring(fileList[count])).isDirectory()) then
        size = size + getFolderSize(tostring(fileList[count]))
      else
        local singleFileSize = File(tostring(fileList[count])).length()
        size = size + singleFileSize
      end
    end
  end
  
  --字节换算
  if(conversion == true) then
    local GB = 1024 * 1024 * 1024;--定义GB的计算常量
    local MB = 1024 * 1024;--定义MB的计算常量
    local KB = 1024;--定义KB的计算常量
    local countResult = ""

    if(size / GB >= 1) then
      --如果当前Byte的值大于等于1GB
      countResult = string.format("%.2f",size / GB).."GB"
      return countResult
    elseif (size / MB >= 1) then
      --如果当前Byte的值大于等于1MB
      countResult = string.format("%.2f",size / MB).."MB"
      return countResult
    elseif (size / KB >= 1) then
      --如果当前Byte的值大于等于1KB
      countResult = string.format("%.2f",size / KB).."KB"
      return countResult
    else
      countResult = size.."B"
      return countResult
    end

  elseif(conversion == nil or conversion == false) then
    return size
  end
end

使用方法:

getFolderSize(文件夹路径,是否进行转换true或false)

转换后将以G,M,KB的形式返回结果,否则直接返回总字节大小

代码来自:303病人

拿走麻烦留下言一下好嘛?

  • copyright
    著作权归作者所有
copyright info_outline
  1. Amoxicillin For Pink Eye Cialis By Mail Nexium Coupons For Seniors Buy Generic Tadalis Sx Elavil Generic 50 Mg. No Script Kamagra Sublinguale India Buy Proventil Online Ovral No Prescription achat viagra en ligne Viagra A 20 Anni Yahoo Viagra En Linea Amoxicillin Antibiotics Weil kamagra 100 mgs Keflex For Broncitis

  2. Viagra 50mg Pills Amazon Viagra Cialis Efectos viagra Levitra Purchase Usa Clomid Boule Amoxicillin And Clavulanate Potassium Infant Dosage levitra 20 mg efectos secundarios Legally Fluoxetine Ups Viagra Kaufen Bremen 2ahr0cdovlzixni4xnz Medicament Cialis Performance Shop Cheap Propecia generic viagra Buy Fluconazole Without A Prescription Herbal Remedy Propecia viagra Pharma Online Achat De Cialis Doctissimo Amoxicillin Threat Meningitis Which Is Better Ampicillin Or Amoxicillin Comprar Levitra Generico En Espana Cialis Originale Miglior Prezzo cialis 20mg for sale Amoxicillin For Impetigo Order Alli On Line

  3. Cialis Laboratorio Lilly is it safe to buy levitra on line Buy Cheap Atomoxetine Online Cialis Im Urlaub Kaufen Viagra Online Bestellen Legal pharmacie viagra Does Alcohol Counteract Amoxicillin Zithromax Doseage Uso Del Viagra En Jovenes Pacific Care Pharmacy Cialis Et Ordonnance cheap cialis online Cialis Ventajas Viagra Giugno 2013 Cialis And Viagra Packages cheap cialis Proscar Pelo Propecia Cialis Seguro Buy Viagara On Line In The Usa

  4. Cialis Generique Effets Secondaires buy canadian finasteride 1 mg Cheap Cialis Azithromycin Buy Online Generique Cialis En France Buy Cheao Store For Sale Comprar Cialis Mallorca cialis from canada Amoxil 400 5 Water Cialisis Medicamentos Propecia Produce Impotencia Cialis Kaufen Wien viagra Forum Viagra 25

选择表情

*

*