Add dedup-filenames example

This commit is contained in:
Xavier Payn 2017-05-19 16:03:12 +02:00
parent 16627e0dbb
commit e48c85104a
3 changed files with 46 additions and 0 deletions

View file

@ -27,6 +27,7 @@ syslog = "3.2.0"
tar = "0.4.12"
tempdir = "0.3.5"
lazy_static = "0.2"
walkdir = "1.0.7"
[build-dependencies]
skeptic = "0.9"

View file

@ -10,6 +10,7 @@
| [Log to the Unix syslog][ex-log-syslog] | [![log-badge]][log] [![syslog-badge]][syslog] | [![cat-debugging-badge]][cat-debugging] |
| [Log messages to a custom location][ex-log-custom] | [![log-badge]][log] | [![cat-debugging-badge]][cat-debugging] |
| [Unzip a tarball to a temporary directory][ex-tar-temp] | [![flate2-badge]][flate2] [![tar-badge]][tar] [![tempdir-badge]][tempdir] | [![cat-filesystem-badge]][cat-filesystem] [![cat-compression-badge]][cat-compression] |
| [Recursively find duplicate file names][ex-dedup-filenames] | [![walkdir-badge]][walkdir] | [![cat-filesystem-badge]][cat-filesystem] |
[ex-clap-basic]: #ex-clap-basic
<a name="ex-clap-basic"></a>
@ -345,6 +346,44 @@ fn run() -> Result<()> {
quick_main!(run);
```
[ex-dedup-filenames]: #ex-dedup-filenames
<a name="ex-dedup-filenames"></a>
## Recursively find duplicate file names
Find recusively in the current directory duplicate filenames,
printing them only once.
[![walkdir-badge]][walkdir] [![cat-filesystem-badge]][cat-filesystem]
```rust,no_run
extern crate walkdir;
use std::collections::HashMap;
use walkdir::WalkDir;
fn main() {
// Counters indexed by filenames
let mut filenames = HashMap::new();
// List recusively all files in the current directory filtering out
// diretories and files not accessible (permission denied)
for entry in WalkDir::new(".").into_iter()
.filter_map(Result::ok)
.filter(|e| !e.file_type().is_dir()) {
// Get entry's filename
let f_name = String::from(entry.file_name().to_string_lossy());
// Get or initialize the counter
let counter = filenames.entry(f_name.clone()).or_insert(0);
// Update the counter
*counter += 1;
if *counter == 2 {
println!("{}", f_name);
}
}
}
```
<!-- Categories -->
@ -373,6 +412,8 @@ quick_main!(run);
[tar]: https://docs.rs/tar/
[tempdir-badge]: https://img.shields.io/crates/v/tempdir.svg?label=tempdir
[tempdir]: https://docs.rs/tempdir/
[walkdir-badge]: https://img.shields.io/crates/v/walkdir.svg?label=walkdir
[walkdir]: https://docs.rs/walkdir/
<!-- Reference -->

View file

@ -71,6 +71,7 @@ community. It needs and welcomes help. For details see
| [Log to the Unix syslog][ex-log-syslog] | [![log-badge]][log] [![syslog-badge]][syslog] | [![cat-debugging-badge]][cat-debugging] |
| [Log messages to a custom location][ex-log-custom] | [![log-badge]][log] | [![cat-debugging-badge]][cat-debugging] |
| [Unzip a tarball to a temporary directory][ex-tar-temp] | [![flate2-badge]][flate2] [![tar-badge]][tar] [![tempdir-badge]][tempdir] | [![cat-filesystem-badge]][cat-filesystem] [![cat-compression-badge]][cat-compression] |
| [Recursively find duplicate file names][ex-dedup-filenames] | [![walkdir-badge]][walkdir] | [![cat-filesystem-badge]][cat-filesystem] |
<!--
@ -149,11 +150,14 @@ Keep lines sorted.
[url]: https://docs.rs/url/
[regex]: https://docs.rs/regex/
[regex-badge]: https://img.shields.io/crates/v/regex.svg?label=regex
[walkdir-badge]: https://img.shields.io/crates/v/walkdir.svg?label=walkdir
[walkdir]: https://docs.rs/walkdir/
<!-- Examples -->
[ex-byteorder-le]: basics.html#ex-byteorder-le
[ex-clap-basic]: app.html#ex-clap-basic
[ex-dedup-filenames]: app.html#ex-dedup-filenames
[ex-global-mut-state]: basics.html#ex-global-mut-state
[ex-json-value]: encoding.html#ex-json-value
[ex-lazy-constant]: basics.html#ex-lazy-constant