init
This commit is contained in:
68
src/main.rs
Normal file
68
src/main.rs
Normal file
@@ -0,0 +1,68 @@
|
||||
use std::fs;
|
||||
use std::io;
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
use encoding_rs::WINDOWS_1250;
|
||||
#[cfg(unix)]
|
||||
use std::os::unix::ffi::OsStrExt;
|
||||
|
||||
fn main() -> io::Result<()> {
|
||||
// Get input directory from command line arguments
|
||||
let args: Vec<String> = std::env::args().collect();
|
||||
if args.len() < 3 {
|
||||
eprintln!("Usage: {} <input_directory> <output_directory>", args[0]);
|
||||
std::process::exit(1);
|
||||
}
|
||||
let input_dir = PathBuf::from(&args[1]);
|
||||
let output_dir = PathBuf::from(&args[2]);
|
||||
let input_dir = Path::new(&input_dir);
|
||||
let output_dir = Path::new(&output_dir);
|
||||
|
||||
fs::create_dir_all(output_dir)?;
|
||||
|
||||
for entry in fs::read_dir(input_dir)? {
|
||||
let entry = entry?;
|
||||
let file_type = entry.file_type()?;
|
||||
if !file_type.is_file() {
|
||||
continue;
|
||||
}
|
||||
|
||||
// ---- Handle possibly non-UTF8 filenames ----
|
||||
#[cfg(unix)]
|
||||
let file_name = {
|
||||
let raw = entry.file_name();
|
||||
let bytes = raw.as_bytes();
|
||||
let (decoded, _, _) = WINDOWS_1250.decode(bytes);
|
||||
decoded.into_owned()
|
||||
};
|
||||
#[cfg(not(unix))]
|
||||
let file_name = entry.file_name().to_string_lossy().into_owned();
|
||||
|
||||
let path_segments: Vec<&str> = file_name.split('\\').filter(|s| !s.is_empty()).collect();
|
||||
|
||||
let mut restored_path = output_dir.to_path_buf();
|
||||
for segment in path_segments {
|
||||
restored_path.push(segment);
|
||||
}
|
||||
|
||||
if let Some(parent) = restored_path.parent() {
|
||||
fs::create_dir_all(parent)?;
|
||||
}
|
||||
|
||||
// Try to move, fall back to copy+delete if needed (cross-filesystem safe)
|
||||
match fs::rename(entry.path(), &restored_path) {
|
||||
Ok(_) => {}
|
||||
Err(_) => {
|
||||
fs::copy(entry.path(), &restored_path)?;
|
||||
// fs::remove_file(entry.path())?;
|
||||
}
|
||||
}
|
||||
println!(
|
||||
"Restored: {} -> {}",
|
||||
entry.path().display(),
|
||||
restored_path.display()
|
||||
);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
Reference in New Issue
Block a user