------iOS training, Java training, Android training, iOS learning technology blog, looking forward to communicating with you------
Calculates the total number of rows for all files under the current path
/*
calculates the total number of rows for all files under the current path
involving nsarray,nsstring,nsfilemanager
*/
calculates the number of rows for all files under the current full path (file \ folder)
receive parameter path: Full path to file ( file, folder )
Nsuinteger codelinescount (nsstring *path)
{
//1. get the File Manager Singleton mode: During the whole program, the object of theNsfilemanager class is the only one
nsfilemanager *fmg = [nsfilemanager defaultmanager];
//2. determine if path is a folder or a file path
BOOL isdir = NO; // mark is a folder
// path exists (valid)
BOOL isexist = [FMG fileexistsatpath:p ath isdirectory:&isdir];
//3. returns the 0 Exit Function directly if the path does not exist
if (!isexist)
{
NSLog(@ " file path is invalid ");
return 0;
}
// path exists
if (isdir)
{
// is a folder
//NSLog (@ " is a folder ");
// Returns a list of all (paths)--- files, folder paths ( arrays ) in the current path (the folder) directory
// array element not full path
nsarray *filepathslist = [FMG contentsofdirectoryatpath:p ath error:nil];
//NSLog (@ "%@", filepathslist);
Nsuinteger count = 0; // number of rows
// iterate through all paths in the array (file name \ folder name)
for (nsstring *filepath in filepathslist)
{
// Stitching full path of the current path folder (sub -Files \ Subfolders ) of the full path
nsstring *fullpath = [nsstring stringwithformat:@ "%@/%@", Path, FilePath];
//NSLog (@ "%@", FullPath);
/*
recursion here
the call itself returns the number of rows per final child file
Codelinescount (FullPath);
*/
// Add the total number of rows per sub-path to each final subpath file
Count + = codelinescount(fullPath);
}
return count;
}
Else
{ // is a file
//NSLog (@ " is a file ");
//1. loads the contents of a file into a string object
nsstring *content = [nsstring stringwithcontentsoffile:p ath encoding: Nsutf8stringencoding error:nil];
//2. The contents of the file (string content) are sliced with ' \ n ' each line , and the contents of the Shard are put back into the array .
nsarray *array = [content componentsseparatedbystring:@ "\ n"];
// Each array element is a nsstring object, which is the contents of a row in the file
//int i = 0;
//For (NSString *str in array)
// {
//i++;
//NSLog (@ "%d---%@", I, str);
// }
return array. Count; // array element number is the number of rows
}
}
Dark Horse Programmer---tool class---OC custom function---Calculate the total number of rows for all files under the current path