use std::fs;
pub fn my_create_dir(input: &str) -> std::io::Result<()>{
if input.is_empty() {
return Err(std::io::Error::new(std::io::ErrorKind::InvalidInput, "Empty directory name"));
}
fs::create_dir(input)?;
Ok(())
}
pub fn does_folder_exist(input: &str) -> bool{
let file = fs::OpenOptions::new().read(true).open(input);
if let Err(..) = file {
return false
}
true
}
pub fn move_file(input: &str, output: &str) {
copy_file(input, output);
match fs::remove_file(input) {
Ok(()) => println!("original deleted"),
Err(err) => println!("Error: {}", err),
}
}
pub fn copy_file(input: &str, output: &str) {
match fs::copy(input, output) {
Ok(bytes) => println!("{} bytes copied", bytes),
Err(err) => println!("Error: {}", err),
}
}
pub fn recursive_copy(input: &str, output: &str){
if does_folder_exist(output) == false{
if let Ok(_) = my_create_dir(output){
} }
let input_path = std::path::Path::new(input);
let output_path = std::path::Path::new(output);
if input_path.is_dir() && output_path.is_dir() {
if let Ok(element) = fs::read_dir(input) {
for res in element {
if let Ok(item) = res {
if let Ok(meta) = fs::metadata(item.path()) {
if meta.is_dir() {
let copy_output = output_path.join(item.path().file_name().unwrap());
if let Ok(_) = my_create_dir(copy_output.to_str().unwrap()) {
} else if let Err(error) = my_create_dir(copy_output.to_str().unwrap()) {
eprintln!("error creating dir {}",error);
}
recursive_copy(item.path().to_str().unwrap(), copy_output.to_str().unwrap())
}
if meta.is_file() {
let copy_output = output_path.join(item.path().file_name().unwrap());
if let Ok(_) = fs::copy(item.path(), ©_output) {
} else {
eprintln!("error copying");
}
}
}
}
}
}
}
}
pub fn recursive_move(input: &str, output: &str){
recursive_copy(input, output);
if let Ok(_) = fs::remove_dir_all(input) {
println!("original removed");
} else {
eprintln!("error in removing original");
}
}