Skip to content

Instantly share code, notes, and snippets.

@josebraga
Created November 28, 2024 11:23
Show Gist options
  • Save josebraga/015019b4604c343f4c82410b59827073 to your computer and use it in GitHub Desktop.
Save josebraga/015019b4604c343f4c82410b59827073 to your computer and use it in GitHub Desktop.
Concatenate files
use std::{fs::{self, File}, io};
fn main() -> io::Result<()> {
// Create the output file
let mut output = File::create("catted")?;
// Find all files starting with "input_"
let mut input_files = fs::read_dir(".")?
.filter_map(|entry| {
let entry = entry.ok()?;
let path = entry.path();
if path.is_file() && path.file_name()?.to_str()?.starts_with("input_") {
Some(path)
} else {
None
}
})
.collect::<Vec<_>>();
// Sort the file paths alphabetically
input_files.sort();
if input_files.is_empty() {
eprintln!("No input files found starting with 'input_'.");
return Ok(());
}
// Process and concatenate each input file
for input_path in input_files {
eprintln!("Processing file: {}", input_path.display());
let mut input = File::open(&input_path)?;
io::copy(&mut input, &mut output)?;
}
eprintln!("All input files concatenated into 'catted'.");
Ok(())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment