Fs::copy can't see files/folders with . in title

I am trying to make a small application that backs up my files to a NAS at home. I am having issues with the fs::copy and fs::create_dir function. When it tries to make a folder or copy a folder with the character '.' in the path it fails and wont run

This is the code:

fn get_files(dir: &str, file_count: &mut u64) -> std::io::Result<()> {

    match fs::read_dir(Path::new(&format!("{}{}", LOCAL_BASE, dir))) {
        Ok(local_files) => {

            for file in local_files{
                let local_path = file.unwrap().path();

                let fnas_dir = &format!("{}{}", FNAS_BASE, dir);
                let fnas_path = Path::new(fnas_dir);

                if local_path.to_str().unwrap().contains(".git") == false {
                 
                    if local_path.is_dir(){

                        if fnas_path.exists() == false {
                            match fs::create_dir(fnas_path) {
                                Ok(_) => println!("Created new folder: {}", fnas_path.to_str().unwrap()), 
                                Err(error) => println!("Folder: {}: {}", error, local_path.to_str().unwrap()),

                            }
                        }


                        get_files(&remove_starting_directory(local_path.to_str().unwrap()), file_count)?
                    }
                    else{
                        if fnas_path.exists() == false {
                            match fs::copy(&local_path, &fnas_path) {
                                Ok(_) => println!("Created file: {}", fnas_path.to_str().unwrap()), 
                                Err(error) => println!("File: {}: {}", error, local_path.to_str().unwrap()),
                            }
                        }
                    }
                }
            }
        }
            Err(err) => {println!("Permission denied: {}", err)},  
    };
    Ok(())
}

And this is the output it gives me

Created new folder: //fnas2/FTP/Jack/Documents/Computing/Rust\file_backup
Created file: //fnas2/FTP/Jack/Documents/Computing/Rust\file_backup.vscode
Created file: //fnas2/FTP/Jack/Documents/Computing/Rust\file_backup\src
Created file: //fnas2/FTP/Jack/Documents/Computing/Rust\file_backup\target
File: The system cannot find the path specified. (os error 3):
:\Documents\Computing\Rust\file_backup\target\debug.cargo-lock
Folder: The system cannot find the path specified. (os error 3): D:\Documents\Computing\Rust\file_backup\target\debug.fingerprint
Folder: The system cannot find the path specified. (os error 3): D:\Documents\Computing\Rust\file_backup\target\debug.fingerprint\aho-corasick-0608655a052e88a1
File: The system cannot find the path specified. (os error 3): D:\Documents\Computing\Rust\file_backup\target\debug.fingerprint\aho-corasick-0608655a052e88a1\dep-lib-aho_corasick
File: The system cannot find the path specified. (os error 3): D:\Documents\Computing\Rust\file_backup\target\debug.fingerprint\aho-corasick-0608655a052e88a1\invoked.timestamp
File: The system cannot find the path specified. (os error 3): D:\Documents\Computing\Rust\file_backup\target\debug.fingerprint\aho-corasick-0608655a052e88a1\lib-aho_corasick
File: The system cannot find the path specified. (os error 3): D:\Documents\Computing\Rust\file_backup\target\debug.fingerprint\aho-corasick-0608655a052e88a1\lib-aho_corasick.json
Folder: The system cannot find the path specified. (os error 3): D:\Documents\Computing\Rust\file_backup\target\debug.fingerprint\aho-corasick-32bba41e4cfc03c0

The first failure is on a file at this location:

file_backup\target\debug.cargo-lock

It appears to be failing because the parent folder (file_backup\target) does not exist.

Earlier, we see this:

Created file: //fnas2/FTP/Jack/Documents/Computing/Rust\file_backup\target

But if target is a directory, then we would expect to see "Created new folder" instead of "Created file".

In the code below, you set fnas_path to FNAS_BASE + dir, but shouldn't you be setting it to something like FNAS_BASE + dir + file.file_name()? Otherwise your create_dir or copy call will write to the location corresponding to dir rather than file.

                let fnas_dir = &format!("{}{}", FNAS_BASE, dir);
                let fnas_path = Path::new(fnas_dir);

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.