Update All Files Recursively In A Directory With A Give Extension With Rust
July 2023
This is a very simple process that recursively loops over a directory and updates any files with a given extension:
use fs;
use PathBuf;
use WalkDir;
Notes
-
The
update_filesfunction is responsibe for finding the filew with the extension. It passes them toupdate_file -
The
update_filefunction does the work. In this example I'm looking for the string-- site:in the file. I add it to the end of the file (after truncating it) if it's not already there -
This code uses the
walkdircreate that can be installed withcargo add walkdir - One imporvement would be to move the file extension into an argument that's passed to the function
- Adding more error handling is also possible, but I'm fine with this panicing if something goes wrong. It's effectively a one-off script.
end of line