This commit is contained in:
Para Dox
2025-04-27 17:50:03 +07:00
parent 9f3710ddaa
commit ba2103d7c0

View File

@@ -82,7 +82,8 @@ for group_name in "${group_names[@]}"; do
echo "Processing group '$group_name' with $num_files ranges." echo "Processing group '$group_name' with $num_files ranges."
# Use an associative array to track which base paths to keep (for efficient lookup) # Use an associative array to track which base paths to keep (for efficient lookup)
declare -A files_to_keep # We will use the basename as the key to avoid potential issues with '/' in the full path key
declare -A files_to_keep_basenames
# Use a standard array to store base paths to move # Use a standard array to store base paths to move
files_to_move=() files_to_move=()
@@ -90,13 +91,12 @@ for group_name in "${group_names[@]}"; do
# Rule 1: Always keep the _0_499999 range if it exists # Rule 1: Always keep the _0_499999 range if it exists
first_range_kept=false first_range_kept=false
for base in "${sorted_bases[@]}"; do for base in "${sorted_bases[@]}"; do
filename=$(basename "$base") filename=$(basename "$base") # Get basename
if [[ "$filename" == *"_0_499999" ]]; then if [[ "$filename" == *"_0_499999" ]]; then
# Check if the key for this base path is already set in files_to_keep # Check if the basename key is already set in files_to_keep_basenames
# Use parameter expansion ${key+x} for safe check with set -u (Bash 4.0+) if [[ -z "${files_to_keep_basenames[$filename]+x}" ]]; then # <-- Check using basename
if [[ -z "${files_to_keep[$base]+x}" ]]; then
echo "Marking first range '$filename' to keep for group '$group_name'." echo "Marking first range '$filename' to keep for group '$group_name'."
files_to_keep["$base"]=1 # Mark this base path for keeping files_to_keep_basenames["$filename"]=1 # <-- Store using basename as key
first_range_kept=true first_range_kept=true
fi fi
# Don't break here; let it potentially be kept by Rule 2 as well if it's one of the last two # Don't break here; let it potentially be kept by Rule 2 as well if it's one of the last two
@@ -118,13 +118,12 @@ for group_name in "${group_names[@]}"; do
start_index=$((num_files - num_to_keep_last)) start_index=$((num_files - num_to_keep_last))
# Loop through the indices of the ranges to keep # Loop through the indices of the ranges to keep
for (( i=start_index; i<num_files; i++ )); do for (( i=start_index; i<num_files; i++ )); do
base="${sorted_bases[$i]}" # Get the base path from the sorted array base="${sorted_bases[$i]}" # Get the full base path
filename=$(basename "$base") filename=$(basename "$base") # Get the basename
# Mark for keeping only if it hasn't been marked already (e.g., by Rule 1) # Mark for keeping only if it hasn't been marked already (e.g., by Rule 1)
# Use parameter expansion ${key+x} for safe check with set -u (Bash 4.0+) if [[ -z "${files_to_keep_basenames[$filename]+x}" ]]; then # <-- Check using basename
if [[ -z "${files_to_keep[$base]+x}" ]]; then
echo " - $filename" echo " - $filename"
files_to_keep["$base"]=1 # Mark this base path for keeping files_to_keep_basenames["$filename"]=1 # <-- Store using basename as key
else else
# Already marked (likely the first range was also one of the last two) # Already marked (likely the first range was also one of the last two)
echo " - $filename (already marked to keep)" echo " - $filename (already marked to keep)"
@@ -132,15 +131,15 @@ for group_name in "${group_names[@]}"; do
done done
fi fi
echo "Total unique ranges marked to keep for group '$group_name': ${#files_to_keep[@]}" echo "Total unique ranges marked to keep for group '$group_name': ${#files_to_keep_basenames[@]}" # <-- Use new array name
# --- Identify and Move Files --- # --- Identify and Move Files ---
# Iterate through all sorted base paths for the group # Iterate through all sorted base paths for the group
for base in "${sorted_bases[@]}"; do for base in "${sorted_bases[@]}"; do
# If a base path is NOT marked to be kept (key doesn't exist in files_to_keep), move it filename=$(basename "$base") # Get the basename
# Use parameter expansion ${key+x} for safe check with set -u (Bash 4.0+) # If the basename is NOT marked to be kept (key doesn't exist in files_to_keep_basenames), move it
if [[ -z "${files_to_keep[$base]+x}" ]]; then if [[ -z "${files_to_keep_basenames[$filename]+x}" ]]; then # <-- Check using basename (This corresponds to the line that previously failed)
files_to_move+=("$base") # Add base path to the list of files to move files_to_move+=("$base") # Add the full base path to the list of files to move
fi fi
done done