83 lines
2.9 KiB
Bash
Executable File
83 lines
2.9 KiB
Bash
Executable File
#!/bin/bash
|
||
# ------------------------------------------------------------------
|
||
# Author: ChatGPT
|
||
# Generated on: $(date '+%Y-%m-%d')
|
||
#
|
||
# This script deletes existing README files matching "README - *.md"
|
||
# and then recursively creates a table-of-contents README file in
|
||
# each directory. The file is named "README - {Folder Name}.md".
|
||
#
|
||
# Each README includes:
|
||
# - An author/date header.
|
||
# - A header with the folder's name.
|
||
# - An alphabetically sorted list of immediate subdirectories,
|
||
# with links to that subdirectory’s own README.
|
||
# - An alphabetically sorted list of immediate files (excluding
|
||
# the generated README).
|
||
#
|
||
# It is written to handle blank spaces in file and folder names.
|
||
# ------------------------------------------------------------------
|
||
|
||
# Delete any existing README files matching "README - *.md"
|
||
find . -type f -name 'README - *.md' -delete
|
||
|
||
# Set the base directory (change "." if necessary)
|
||
base_dir="."
|
||
|
||
# Recursively find every directory starting from base_dir.
|
||
find "$base_dir" -type d -print0 | while IFS= read -r -d '' dir; do
|
||
# Get the folder's base name (handles spaces correctly)
|
||
base=$(basename "$dir")
|
||
# Define the path for the README file: "README - {Folder Name}.md"
|
||
readme="$dir/README - ${base}.md"
|
||
|
||
# Write the header into the README file, including author/date attribution.
|
||
{
|
||
echo "Generated by ChatGPT on $(date '+%Y-%m-%d')"
|
||
echo ""
|
||
echo "# Table of Contents for ${base}"
|
||
echo ""
|
||
} > "$readme"
|
||
|
||
#########################
|
||
# Process Subdirectories
|
||
#########################
|
||
subdirs=()
|
||
# Find immediate subdirectories (depth 1) in the current directory.
|
||
while IFS= read -r -d '' subdir; do
|
||
subdirs+=( "$(basename "$subdir")" )
|
||
done < <(find "$dir" -mindepth 1 -maxdepth 1 -type d -print0)
|
||
|
||
if [ ${#subdirs[@]} -gt 0 ]; then
|
||
# Sort subdirectory names alphabetically while preserving spaces.
|
||
mapfile -t sorted_subdirs < <(printf "%s\n" "${subdirs[@]}" | sort)
|
||
echo "## Subdirectories" >> "$readme"
|
||
for sub in "${sorted_subdirs[@]}"; do
|
||
# Link to the subdirectory's README file.
|
||
# The link format: [[Subfolder/README - Subfolder]]
|
||
echo "- [[${sub}/README - ${sub}]]" >> "$readme"
|
||
done
|
||
echo "" >> "$readme"
|
||
fi
|
||
|
||
#################
|
||
# Process Files
|
||
#################
|
||
files=()
|
||
# Find immediate files (depth 1), excluding the generated README file.
|
||
while IFS= read -r -d '' file; do
|
||
files+=( "$(basename "$file")" )
|
||
done < <(find "$dir" -mindepth 1 -maxdepth 1 -type f ! -path "$readme" -print0)
|
||
|
||
if [ ${#files[@]} -gt 0 ]; then
|
||
# Sort file names alphabetically while preserving spaces.
|
||
mapfile -t sorted_files < <(printf "%s\n" "${files[@]}" | sort)
|
||
echo "## Files" >> "$readme"
|
||
for f in "${sorted_files[@]}"; do
|
||
echo "- [[${f}]]" >> "$readme"
|
||
done
|
||
echo "" >> "$readme"
|
||
fi
|
||
done
|
||
|