Txtcombine
BASH Scripts
SH
txtcombine.sh
#!/bin/bash
# Get the name of the current folder
current_folder_name=$(basename "$PWD")
# Create the output file with the current folder's name
output_file="${current_folder_name}.txt"
# Initialize an empty string to hold all the content
all_content=""
# Loop through each text file in the current directory
for text_file in ./*.txt; do
# Check if the file exists (in case there are no text files)
if [ -e "$text_file" ]; then
# Get the filename without the extension
base_name=$(basename -- "$text_file")
name_without_extension="${base_name%.*}"
# Read the content of the file and prepend each line with the filename
while IFS= read -r line; do
all_content+="${name_without_extension}: ${line}\n"
done < "$text_file"
fi
done
# Write the accumulated content to the output file
printf "%b" "$all_content" > "$output_file"
echo "All text files have been combined into '${output_file}'."