英文:
Create nested directories in rust
问题
I'm working on a small application to organize my photos with the exif data in the jpeg file. Most importantly I want the date. I should be able to run this on a directory of jpegs.
我正在开发一个小应用程序,用于使用JPEG文件中的Exif数据整理我的照片。最重要的是我想要获取日期。我应该能够在一个包含JPEG文件的目录上运行它。
I want my directory structure to be:
我希望我的目录结构如下:
yyyy/
yyyy-mm-dd/
photos
I can create the parent directory (yyyy) but I'm not sure how to create the child directory (yyyy-mm-dd) within it.
我可以创建父目录(yyyy),但我不确定如何在其中创建子目录(yyyy-mm-dd)。
The most obvious solution to me was to write this:
对我来说,最明显的解决方案是编写以下代码:
let child_dir_name = format!("{}-0{}-0{}", &year, &month, &day);
let parent_dir_name = format!("{}", &year);
let path = (parent_dir_name/child_dir_name);
fs::create_dir(path);
But this doesn't work, for obvious reasons. Any help would be greatly appreciated, still new to Rust so any references/tips would be nice too.
但这不起作用,显然有问题。非常感谢您的帮助,我还不太熟悉Rust,所以任何参考资料/建议也会很有用。
EDIT: I want to use the variable names as the directory names so
编辑:我希望使用变量名作为目录名称,所以
fs::create_dir_all(parent_path/child_path);
returns an error: cannot divide &path by &path
返回错误:不能将 &path 除以 &path
英文:
I'm working on a small application to organize my photos with the exif data in the jpeg file. Most importantly I want the date. I should be able to run this on a directory of jpegs.
I want my directory structure to be:
yyyy/
yyyy-mm-dd/
photos
I can create the parent directory (yyyy) but I'm not sure how to create the child directory (yyyy-mm-dd) within it.
The most obvious solution to me was to write this:
let child_dir_name = format!("{}-0{}-0{}", &year, &month, &day);
let parent_dir_name = format!("{}", &year);
let path = (parent_dir_name/child_dir_name);
fs::create_dir(path);
But this doesn't work, for obvious reasons. Any help would be greatly appreciated, still new to Rust so any references/tips would be nice too.
EDIT: I want to use the variable names as the directory names so
fs::create_dir_all(parent_path/child_path);
returns an error: cannot divide &path by &path
答案1
得分: 1
你可以使用 std::fs::create_dir_all
来创建路径中的所有目录。详见 https://doc.rust-lang.org/std/fs/fn.create_dir_all.html。
英文:
You can use std::fs::create_dir_all
to create all the directories in a path. See https://doc.rust-lang.org/std/fs/fn.create_dir_all.html
答案2
得分: 0
Rust正在将两个变量名称之间的斜杠视为除法运算符。
您可以通过将(parent_dir_name/child_dir_name)
替换为format!("{}{}", &parent_dir_name, &child_dir_name)
来修复它。
此外,您可以将此字符串传递给Path::new()
(使用use std::path::Path;
),以使此代码在各种操作系统上都能正常工作。
另外,正如其他人指出的,要递归创建目录,您需要使用std::fs::create_dir_all
而不是std::fs::create_dir
。
英文:
What is happening is that Rust is considering the slash between the two variable names as a divide operator.
You can fix it by replacing (parent_dir_name/child_dir_name)
with format!("{}/{}", &parent_dir_name, &child_dir_name)
Additionally you could give this string to Path::new()
(with use std::path::Path;
) to make this code OS-independant.
Also, as noted by others, to create directories recursively, you will need to use std::fs::create_dir_all
instead of std::fs::create_dir
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论