Skip to content

Instantly share code, notes, and snippets.

@rafuwu
Last active November 10, 2024 17:48
Show Gist options
  • Save rafuwu/e31706ad00a644357a4c2c07b13fdf2a to your computer and use it in GitHub Desktop.
Save rafuwu/e31706ad00a644357a4c2c07b13fdf2a to your computer and use it in GitHub Desktop.
YNOproject.net screenshot renamer script
#!/bin/sh
# This is free and unencumbered software released into the public domain.
# For more information, please refer to <http://unlicense.org/>
#
# This script renames YNOproject.net screenshots taken with the feature inside the website
#
# It supports all of the following naming formats as inputs (given as examples):
# * 'ynoproject_2kki_screenshot_2023-1-6-0-49-2.png'
# * 'ynoproject_2kki_screenshot_2023-01-06-00h49m02s.png'
# * 'ynoproject_2kki_The_Nexus_screenshot_2023-01-06-00h49m02s.png'
#
# The files are renamed to something like:
# '2023-01-06_00-49-02_ynoproject_2kki_The Nexus.png'
#
#
# [WARNING] Please consider creating a backup of your screenshots before using this software,
# especially if the naming format doesn't quite match. This software isn't always
# updated to the latest YNO screenshot namings, and using it inappropriately might
# lead do data loss in the form of incorrect filenames or even the removal of your
# screenshots altogether.
#
counter=0
underscore="_"
dotpng=".png"
for i in ynoproject*.png
do
file_name_without_map_name="$i"
# Get map name. Uses a RegEx Capturing Group.
# If it matches, jump to label x (do nothing). Otherwise, replace everything with nothing and quit (sed) with exit status 100.
# https://stackoverflow.com/a/66365284
map_name="$(echo "$i" | sed -E 's/^ynoproject_*_[^_]*_(. ?)_screenshot.*$/\1/;tx;s/.*//;q100;:x')"
# If map name exists, alter the variable for use below as if the map name wasn't present.
# And edit map name to a string that begins with an underscore and has the rest of underscores replaced with spaces.
if [ "$map_name" ]; then
file_name_without_map_name="$(echo "$i" | sed -E "s/_$map_name//")"
map_name="$(echo "$map_name" | sed -E 's/_/ /g;s/(.*)/_\1/')"
fi
sitename_gamecode="$(echo "$file_name_without_map_name" | awk -F '_' '{printf "%s_%s", $1, $2}')"
date="$(echo "$file_name_without_map_name" | awk -F '_' '{printf "%s", $4}' | sed 's/.png//g')"
date_noletters="$(echo "$date" | sed 's/h/-/g; s/m/-/g; s/s//g')"
date_final="$(echo "$date_noletters" | awk -F '-' '{printf "d-d-d_d-d-d", $1, $2, $3, $4, $5, $6}')"
newname="$date_final$underscore$sitename_gamecode$map_name$dotpng"
mv "$i" "$newname"
echo \'"$newname"\' \<- \'"$i"\'
echo "\"$i\",\"$newname\"" >> renaming_old_new.csv
counter=$((counter 1))
done
echo Renamed $counter files
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment