Recursively scan directory functions

Post Reply
User avatar
gabrielp
Advanced member
Posts: 577
Joined: Fri Aug 08, 2014 4:31 pm
Location: Boston
Contact:

Recursively scan directory functions

Post by gabrielp »

I had to recursively scan directories within Scripter for switch-inject-lite. Perhaps these functions may be useful to others in the future.

Code: Select all

		// Function for recursively counting bytes within a directory
			get_recursive_byte_count = function(target_dir, entry_list){
				byte_count = 0;
				forEach(entry_list, function(file_name, index){
					type = getTargetType(target_dir + file_name);
					if(type === "dir"){
						sub_target_dir = target_dir + file_name + getDirectorySeperator(s);
						sub_dir = new Dir(sub_target_dir);
						sub_file_list = sub_dir.entryList("*", Dir.Files|Dir.Dirs|Dir.NoDotAndDotDot, Dir.Name);
						result_count = get_recursive_byte_count(sub_target_dir, sub_file_list);
					} else if(type === "file"){
						fs = new FileStatistics(target_dir + file_name);
						byte_count += fs.getByteCount();
					}
				});
				return byte_count;
			}

Code: Select all

		// Function for recursively counting files within a directory
			get_recursive_file_count = function(target_dir, entry_list){
				file_count = 0;
				forEach(entry_list, function(file_name, index){
					type = getTargetType(target_dir + file_name);
					if(type === "dir"){
						sub_target_dir = target_dir + file_name + getDirectorySeperator(s);
						sub_dir = new Dir(sub_target_dir);
						sub_file_list = sub_dir.entryList("*", Dir.Files|Dir.Dirs|Dir.NoDotAndDotDot, Dir.Name);
						result_count = get_recursive_file_count(sub_target_dir, sub_file_list);
					} else if(type === "file"){
						file_count++;
					}
				});
				return file_count;
			}
Used like this:

Code: Select all

		dir = new Dir( 'your_path_to_directory' );
			file_list = dir.entryList("*", Dir.Files|Dir.Dirs|Dir.NoDotAndDotDot, Dir.Name);
			count = get_recursive_file_count(prop.target_url, file_list);
Chat: open-automation @ gitter
Code: open-automation & dominickp @ GitHub
Tools: Switch, Pitstop, EPMS, Veracore, PageDNA, SmartStream, Metrix
User avatar
gabrielp
Advanced member
Posts: 577
Joined: Fri Aug 08, 2014 4:31 pm
Location: Boston
Contact:

Re: Recursively scan directory functions

Post by gabrielp »

Improved to fix some bugs and controlling search depth:

Code: Select all

		// Function for recursively counting files within a directory
			get_recursive_file_count = function(target_dir, entry_list, search_depth, file_count, iteration){
				if(typeof(file_count) == "undefined"){
					file_count = 0;
				}
				if(typeof(iteration) == "undefined"){
					iteration = 0;
				}
				if(iteration == search_depth){
					return file_count;
				}
				iteration++;

				forEach(entry_list, function(file_name, index){
					type = getTargetType(target_dir + file_name);
					if(type === "dir"){
						sub_target_dir = target_dir + file_name + getDirectorySeperator(s);
						sub_dir = new Dir(sub_target_dir);
						sub_file_list = sub_dir.entryList("*", Dir.Files|Dir.Dirs|Dir.NoDotAndDotDot, Dir.Name);
						result_count = get_recursive_file_count(sub_target_dir, sub_file_list, search_depth, file_count, iteration);						
						file_count += result_count;
					} else if(type === "file"){
						file_count++;
					}
				});
				return file_count;
			}
			
		count = get_recursive_file_count(prop.target_url, file_list, prop.search_depth);
Chat: open-automation @ gitter
Code: open-automation & dominickp @ GitHub
Tools: Switch, Pitstop, EPMS, Veracore, PageDNA, SmartStream, Metrix
User avatar
gabrielp
Advanced member
Posts: 577
Joined: Fri Aug 08, 2014 4:31 pm
Location: Boston
Contact:

Re: Recursively scan directory functions

Post by gabrielp »

And here is the fixed byte count:

Code: Select all

		// Function for recursively counting bytes within a directory
			get_recursive_byte_count = function(target_dir, entry_list, search_depth, byte_count, iteration){
				if(typeof(byte_count) == "undefined"){
					byte_count = 0;
				}
				if(typeof(iteration) == "undefined"){
					iteration = 0;
				}
				if(iteration == search_depth){
					return byte_count;
				}
				iteration++;

				forEach(entry_list, function(file_name, index){
					type = getTargetType(target_dir + file_name);
					if(type === "dir"){
						sub_target_dir = target_dir + file_name + getDirectorySeperator(s);
						sub_dir = new Dir(sub_target_dir);
						sub_file_list = sub_dir.entryList("*", Dir.Files|Dir.Dirs|Dir.NoDotAndDotDot, Dir.Name);
						result_size = get_recursive_byte_count(sub_target_dir, sub_file_list, search_depth, byte_count, iteration);
						byte_count += result_size;
					} else if(type === "file"){
						fs = new FileStatistics(target_dir + file_name);
						byte_count += fs.getByteCount();
					}
				});
				return byte_count;
			}
Chat: open-automation @ gitter
Code: open-automation & dominickp @ GitHub
Tools: Switch, Pitstop, EPMS, Veracore, PageDNA, SmartStream, Metrix
Post Reply