65.9K
CodeProject 正在变化。 阅读更多。
Home

统计你的 ASP/JS 代码!(迷你文章)

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.33/5 (5投票s)

2002年12月8日

1分钟阅读

viewsIcon

75832

这是一个快速的小工具,可以显示你在 Web 应用程序中编写了多少代码。它没有实际用途,只是让你了解你完成了多少工作。

引言

你是否曾经为一个项目工作了很长时间,以至于忘记了你实际投入了多少精力?我一直在开发一个通用的网站内容管理系统,它具有完整的无需编码设置系统,已经大约 8 个月了,包括白天、夜晚和周末。一段时间后,能够通过了解你付出的努力来给自己一点肯定,感觉很好。昨天,在失去“状态”之后,我编写了这个小工具来计算我实际编写了多少代码。我不会将源代码发布为可下载文件,因为它只是一个包含少量代码的单个文件。因此,我将直接在此处粘贴一份副本。要使其与你的项目相关,只需将其保存到 Web 服务器上的临时目录中的一个文件中,并将第 6 行 (iterate(server.mappath("/web_api"))) 更改为指向包含所有代码的文件夹。 瞧!你就能得到一个关于你完成的工作量的小总结!

目前,它只统计 ASP 和 JS 文件,因此如果你想统计其他内容,请相应地修改代码。统计代码时,不考虑空行。你将获得类似于我在撰写本文时的结果的格式化结果。

ASP:
  Total Lines Coded: 15540
  Total Bytes: 628957
  Total Individual Elements (words) Typed: 86189

JScript:
  Total Lines Coded: 4282
  Total Bytes: 155837

代码

<%
option explicit
dim fso
set fso = createobject("scripting.filesystemobject")
dim asplines, jslines, aspbytes, jsbytes, aspwords
iterate(server.mappath("/web_api"))
response.write "ASP:
Total Lines Coded: " & asplines & "
Total Bytes: " & aspbytes & "
Total Individual Elements (words) Typed: " & aspwords
response.write "

JScript:
Total Lines Coded: " & jslines & "
" & "Total Bytes: " & jsbytes

function iterate(path)
    dim folder, folders, files, file, ts, txt, arr, f
    set folder = fso.getfolder(path)
    set files = folder.files
    dim rx, c
    set rx = new regexp
    rx.ignorecase = true
    rx.global = true
    rx.pattern = "  +"
    for each file in files
        if right(file.name,4)=".asp" or right(file.name,3)=".js" then
            set ts = file.openastextstream
            if ts.atendofstream then txt = "" else txt = ts.readall
            ts.close
            txt = rx.replace(txt," ")
            txt = replace(txt,vbcrlf&vbcrlf,vbcrlf)
            arr = split(replace(txt,vbcrlf," ")," ")
            aspwords = aspwords + ubound(arr)
            arr = split(txt,vbcrlf)
            if right(file.name,4)=".asp" then
                asplines = asplines + ubound(arr)
                aspbytes = aspbytes + len(txt)
            else
                jslines = jslines + ubound(arr)
                jsbytes = jsbytes + len(txt)
            end if
        end if
    next
    set folders = folder.subfolders
    for each f in folders
        iterate f.path
    next
end function
%>
© . All rights reserved.