Hi,The API Document shows impl Iterator for ReadDir , but i cant figure out how to use those methods expect the next()
. To sort the files from some directory, the only way I can find is that , read the files into Vec ,then invoke the sort_by
method of Vec. I want to know how to use the collect
method if the Iterator trait have been implemented. Can someone help me? Thanks
use std::fs;
use std::io;
use std::fs::DirEntry;
use std::path::Path;
fn main() {
test_group_files();
}
fn test_group_files() -> io::Result<()> {
let dir = ".";
let read_dir = try!(fs::read_dir(dir));
// Sort the files
// =>
let mut entries: Vec<DirEntry> = vec![];
// loop through the files
for entry in try!(fs::read_dir(dir)) {
match entry {
Ok(v) => {
entries.push(v);
}
Err(_) => {}
};
}
entries.sort_by(|a, b| a.path().cmp(&b.path()));
// <=
// 30 directories per group
let mut count = 0u16;
let mut dir_count = 1u16;
for variable in entries {
count += 1;
if count ^ 30 == 0 {
count = 0;
dir_count += 1;
}
let _target = format!("{}/{:0>3}", dir, dir_count);
let mut target_dir = Path::new(&_target);
if !target_dir.exists() {
try!(fs::create_dir(target_dir));
}
let original = variable.path();
let destination = original.file_name().unwrap();
let destination_buffer=target_dir.join(destination);
fs::rename(&original,destination_buffer);
}
Ok(())
}