It’s a relatively trivial task, really easy to do with the command prompt and GNU wc:
1 |
for /f "delims=" %P in ('dir /s /b *.cs') do wc -l "%P" >> D:\codewordcounts.txt |
I executed this within the desired directory (my BASeBlock source folder, if you must know) and the result was a file filled with numbers and files; I wrote a quick python script to parse that and add up the numbers that were at the start of each line, but then I figured, why not just write the whole think in python and forget about the rest of it, so I did.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
#!/usr/bin/python import sys import fnmatch import os def searchfolder(folder,filemask,excludemask): filelist=[] for root,subFolders,files in os.walk(folder): for file in files: buildpath=os.path.join(root,file) if os.path.isfile(buildpath): if fnmatch.fnmatch(buildpath,filemask): if not fnmatch.fnmatch(buildpath,excludemask): filelist.append(buildpath) return filelist def Countlines(filename): #counts the lines in filename. countit = open(filename) runner=0 for line in countit: runner=runner+1 return runner searchfor = sys.argv[1] searchin = sys.argv[2] excludethese = sys.argv[3] print "examining files matching " + searchfor + " in directory " + searchin + " excluding " + excludethese totalcount=0 for countthese in searchfolder(searchin,searchfor,excludethese): #print Countlines(countthese) totalcount = totalcount + Countlines(countthese) print "total line count:" + totalcount |
It’s a rather basic script, and I don’t even comment it as much as I ought to. I just wanted a quick tool to be able to count the lines of code in a given directory for a given source file type. Ideally, I’d allow for multiple types, but I didn’t want to complicate the argument parsing code too much. The counting method is pretty barren, it just loops over every line and increments a counter. It seems to work relatively fast. It quickly gave me the result I wanted, which was that BASeBlock’s .cs files comprise about 53K lines of code, excluding the .designer.cs files (thus the third argument). And now I have a nice reusable script to figure this out in a jiffy without too much thinking about shell syntax or what I need to pipe to wc and what arguments I need to pass wc and whatnot. plonked in a location on my windows machine with pathext set to allow execution of .py files directly using the ActivePython interpreter and putting it on my Linux machine and adding a symlink in /usr/bin to it makes it available to me on both machines.
Update: Jul 12 2020
I went to use this and realized I had written it for Python 2. I’ve adjusted it to function with Python 3:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
import sys import fnmatch import os def searchfolder(folder,filemask,excludemask): filelist=[] for root,subFolders,files in os.walk(folder): for file in files: buildpath=os.path.join(root,file) if os.path.isfile(buildpath): if fnmatch.fnmatch(buildpath,filemask): if not fnmatch.fnmatch(buildpath,excludemask): filelist.append(buildpath) return filelist def Countlines(filename): #counts the lines in filename. countit = open(filename) runner=0 for line in countit: runner=runner+1 return runner searchfor = sys.argv[1] searchin = sys.argv[2] excludethese = sys.argv[3] print("examining files matching " + searchfor + " in directory " + searchin + " excluding " + excludethese) totalcount=0 for countthese in searchfolder(searchin,searchfor,excludethese): #print Countlines(countthese) totalcount = totalcount + Countlines(countthese) print("total line count:" + str(totalcount)); |
Have something to say about this post? Comment!