Dark Horse Programmer---tool class---OC custom function---Calculate the total number of rows for all files under the current path

Source: Internet
Author: User

------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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.