-
gabrielp
- Advanced member
- Posts: 645
- Joined: Fri Aug 08, 2014 4:31 pm
- Location: Boston
-
Contact:
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);
-
gabrielp
- Advanced member
- Posts: 645
- Joined: Fri Aug 08, 2014 4:31 pm
- Location: Boston
-
Contact:
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);
-
gabrielp
- Advanced member
- Posts: 645
- Joined: Fri Aug 08, 2014 4:31 pm
- Location: Boston
-
Contact:
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;
}