79567413

Date: 2025-04-10 19:20:48
Score: 0.5
Natty:
Report link

After trying several approaches, I found that many solutions would fail when trying to parse an entire project of sub-folders and convert everything. I tried using globstar for recursive matching but that would fail if the directory had spaces in it which was problematic for me.

The method that worked best for me was using the find command to specifically locate files, using the name parameter for the file extension.

I created a new file at the root of my project called convert_tga_to_png.sh and made it executable using the command:

chmod +x convert_tga_to_png.sh

Inside the file I wrote one find command which located all the files with .tga.meta extension and used the git mv command to rename it to .png.meta, and another find command which located all files with .tga extension and converted them to .png using ImageMagick. If the conversion was successful, i used git rm to delete the original .tga file.

I also added a check at the beginning to make sure ImageMagick is installed.

#!/bin/bash

# Check if ImageMagick is installed
if ! command -v magick &> /dev/null
then
  echo "Error: ImageMagick (magick) is not installed."
  exit 1
fi

echo "Starting rename and convert process..."

# Count total files first
meta_total=$(find . -type f -name "*.tga.meta" | wc -l)
tga_total=$(find . -type f -name "*.tga" | wc -l)
total_files=$((meta_total + tga_total))
count=0

echo "Total operations to perform: $total_files"

echo
echo "Renaming .tga.meta -> .png.meta"

find . -type f -name "*.tga.meta" -print0 | while IFS= read -r -d '' file; do
  newfile="${file%.tga.meta}.png.meta"
  if mv "$file" "$newfile"; then
    ((count++))
    echo "[$count/$meta_total] Renamed: $file -> $newfile"
  else
    echo "[$count/$meta_total] ERROR renaming: $file"
  fi
done

echo
echo "Converting .tga -> .png using ImageMagick"

find . -type f -name "*.tga" -print0 | while IFS= read -r -d '' file; do
  newfile="${file%.tga}.png"
  if magick "$file" "$newfile"; then
    rm "$file" # delete original tga file
    ((count++))
    echo "[$count/$tga_total] Converted: $file -> $newfile"
  else
    echo "[$count/$tga_total] ERROR converting: $file"
  fi
done

echo
echo "Done!"

Then I ran the file, making sure Git Bash was set to the right directory, and this command was the correct relative path to the script:

./convert_tga_to_png.sh
Reasons:
  • Blacklisted phrase (1.5): any solution
  • Long answer (-1):
  • Has code block (-0.5):
  • Low reputation (0.5):
Posted by: nopunintendo