File Exchange

image thumbnail

slocdir( directoryPath )

version 1.1.0.0 (977 Bytes) by Diffy D
Go recursively into folders to count the .m lines of code

0 Downloads

Updated 31 Mar 2016

View License

A simple recursive function which uses the sloc function to count the number of lines of .m code.
The sloc function can be found here : http://www.mathworks.com/matlabcentral/fileexchange/loadFile.do?objectId=3900&objectType=file

Cite As

Diffy D (2019). slocdir( directoryPath ) (https://www.mathworks.com/matlabcentral/fileexchange/24000-slocdir-directorypath), MATLAB Central File Exchange. Retrieved .

Comments and Ratings (5)

Sven

Thanks very much for this neat little helper.

Little suggestion: changing the few lines of the try statement to:

newlines = slocdir(fullfile(directoryPath,list(i).name));
count = count + newlines;
fprintf('Folder %30s contains %6d lines.\n',fullfile(directoryPath,list(i).name),newlines)

allows to show lines for all subfolders. Not nicely just a fast patch.

It doesn't recurse through the folders correctly:
Instead of:
count = count + sloc(list(i).name);
Need:
count = count + sloc(fullfile(directoryPath, list(i).name));

To handle file systems more generally instead of:
count = count + slocdir(strcat(directoryPath,'\\',list(i).name));

Need:
count = count + slocdir(fullfile(directoryPath,list(i).name));

Hi,

You should change your line :

count = count + slocdir(strcat(directoryPath,'//',list(i).name));

by :

count = count + slocdir(fullfile(directoryPath,list(i).name));

in order your script being portable over non-windows machines...

Thanks for the file.

François.

Diffy D

BugWarning :

at line :
warning( strcmp( list(i).name,'file or folder skipped') );

replace by :
warning( strcat( list(i).name,'file or folder skipped') );

it STRCAT not STRCMP

Updates

1.1.0.0

Updated accordingly with the comments given by users

1.1.0.0

Fix a bug, change strcmp to strcat in the warning

MATLAB Release Compatibility
Created with R2007b
Compatible with any release
Platform Compatibility
Windows macOS Linux
Acknowledgements

Inspired by: sloc