Skip to content

Commit

Permalink
rename: Make sure to not overwrite any existing files
Browse files Browse the repository at this point in the history
  • Loading branch information
ad-si committed Dec 8, 2024
1 parent 6407650 commit 7f53f65
Showing 1 changed file with 28 additions and 13 deletions.
41 changes: 28 additions & 13 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -557,19 +557,12 @@ async fn exec_with_args(args: Args, stdin: &str) {
} else {
chrono::Local::now().format("%Y-%m-%dT%H%M").to_string()
};
let description =
analysis.description.trim().to_lowercase().replace(' ', "_");
let path = std::path::Path::new(&file);
let extension =
path.extension().and_then(|ext| ext.to_str()).unwrap_or("");
let new_name =
format!("{}_{}.{}", timestamp, description, extension);

if let Err(err) = std::fs::rename(&file, &new_name) {
eprintln!("Error renaming file: {}", err);
std::process::exit(1);
}
println!("Renamed {} to {}", file, new_name);
let description = analysis //
.description
.trim()
.to_lowercase()
.replace(' ', "_");
rename_file(file, timestamp, description);
}
Err(err) => {
eprintln!("Error analyzing file: {}", err);
Expand Down Expand Up @@ -753,6 +746,28 @@ async fn exec_with_args(args: Args, stdin: &str) {
};
}

fn rename_file(file: String, timestamp: String, description: String) {
let path = std::path::Path::new(&file);
let ext = path.extension().and_then(|ext| ext.to_str()).unwrap_or("");
let mut new_name = format!("{}_{}.{}", timestamp, description, ext);

let mut counter = 0;
loop {
if std::path::Path::new(&new_name).exists() {
counter += 1;
new_name = format!("{}_{}_{}.{}", timestamp, description, counter, ext)
} else {
break;
}
}

if let Err(err) = std::fs::rename(&file, &new_name) {
eprintln!("Error renaming file: {}", err);
std::process::exit(1);
}
println!("Renamed {} to {}", file, new_name);
}

#[tokio::main]
async fn main() {
let stdin = stdin();
Expand Down

0 comments on commit 7f53f65

Please sign in to comment.