Nested Loop From A Folder to Get Files In Servers

Nested loop to do whatever you need to do with the files and folders in the server remotely using ManagementScope. This function will loop the first layer or loop nested until the end.

public static IEnumerable queryByFolder(ManagementScope scope, string path, bool withSecurity, bool recursive) {
        if (String.isNullOrEmpty(path)) throw new Exception("Please specified folder.");

          if (recursive) {
            foreach (var subdir in dir.GetSubDirectories(scope, path, true)) {
              foreach (var f in queryByFolder(scope, subdir, withSecurity)) yield return f;
          foreach (var f in queryByFolder(scope, path, withSecurity)) yield return f;

        yield break;

Below example is to generate a html nested list:

function sdRoleFeatures(records,map, header) {
		var html = _.swTh(map, {style:_.sdTdStyle()} ,1) + '<tr><td><div><ul>';

		if(_.isNullOrEmpty(records)) return '';
		for(var r in records){
			html += _.fmt('<li>{0}</li>',records[r].Name);

			if (! _.isNullOrEmpty(records[r].Roles)){
				html += sdSubRoleFeature(records[r].Roles);

		html += '</ul></div></td></tr>';
		var str = sdSectionTemplate(html, header);
		return str;
function sdSubRoleFeature(roles){
		var html = '';
		if(_.isNullOrEmpty(roles)) return '';

		for(var i = 0; i &lt; roles.length; i++) {
			html += &#039;<ul>';
			if( _.isNullOrEmpty(roles[i].Roles)){
				html +=  _.swLi(roles[i].Name);
				html += _.swLi(roles[i].Name);
				html += sdSubRoleFeature(roles[i].Roles);
			html += '</ul>';

		return html;

One thought on “Nested Loop From A Folder to Get Files In Servers

  1. Pingback: List All Files and Directories in a Directory – Web / Application Development Blog

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.