#Can I create a subdirectory?

4 messages · Page 1 of 1 (latest)

winter quiver
#

I'm trying to create a nested directory structure to store files. Right now, I can create a root directory, but I can't make directories within it:

await createDir('YibbusMR', { dir: BaseDirectory.Document, recursive: true });
await createDir('Client', { dir: `${BaseDirectory.Document}/YibbusMR`, recursive: true });

Trying to save a file without creating the intermediatary directories doesn't work either:

await writeBinaryFile(`YibbusMR/Client/avatar.${filetype}`, avatarFile, {
            dir: BaseDirectory.Document
        });
Unhandled Promise Rejection: path: /Users/dani/Documents/YibbusMR/Client/avatar.png: No such file or directory (os error 2)

Any suggestions?

ember kite
#

I suggest you to use the fs::create_dir_all() filesystem function. You just need to put the directory path and it will automatically creates the directory path, Then after that you can create the file you want. But you need to create a custom tauri command at first.
You can create it by:
In the main.rs file, Create a custom function and put #[tauri::command] macro on top of it to specify that it's a tauri command, and then take the passed argument in the function, use the fs::create_dir_all() in it with the passed argument, And don't forget to invoke the generated handler of the custom command, Like so:

// Also in main.rs
fn main() {
  tauri::Builder::default()
    // This is where you pass in your commands
    .invoke_handler(tauri::generate_handler![my_custom_command])
    .run(tauri::generate_context!())
    .expect("failed to run app");
}
winter quiver
#

Oh I see. So it's not an out-of-the-box javascript function? I'm not super familiar with rust, so I can give it a shot...

steep mantle
#

You're calling createDir with recursive which should create the intermediary directories:

await createDir('YibbusMR/Client', { dir: BaseDirectory.Document, recursive: true });
await writeBinaryFile(`YibbusMR/Client/avatar.${filetype}`, avatarFile, { dir: BaseDirectory.Document });