Batch Rename Files in a Folder with a Script You Can Preview First

You get a PowerShell or Bash rename script that shows every old-to-new name before changing anything, writes an undo log, and includes plain run steps.

Prompt

Prompt
Act as a patient scripting helper for people who have never written code.

I want to batch rename files in one folder. Before writing anything, ask me these questions in one numbered message and wait for my answers:
1. Which operating system do I use: Windows, macOS or Linux?
2. What is the full folder path, and which files are included (all files or only certain extensions)?
3. What is my naming rule? I will give one real before-and-after example.
4. Should files in subfolders be included?

Then write one script, PowerShell for Windows or Bash for macOS and Linux, following these rules:
- It runs in preview mode by default. It prints every old name and new name and changes nothing.
- Files are renamed only when I run it again with a clearly named switch, such as --apply.
- In apply mode it writes an undo log (CSV: old name, new name) in the folder before each rename.
- It skips, and reports, any file whose new name already exists. It never overwrites.
- Every block has a one-line plain-English comment.

Output format:
1. What the script does (2-3 sentences)
2. The script in one code block
3. Numbered steps: save it, run the preview, run for real, undo
4. Sample preview output using my example

End with one line on what could go wrong and what to back up first.

Example output

Example for: a Mac user renaming 142 trip photos from IMG_4521.JPG to lisbon-2024-001.jpg, no subfolders.

1. What the script does

It finds every file starting with IMG_ and ending in .JPG in your Lisbon folder, then numbers them in name order as lisbon-2024-001.jpg, lisbon-2024-002.jpg and so on. By default it only prints the plan. With --apply it renames and logs each change so you can undo.

2. The script
#!/bin/bash
# Settings: folder and new name prefix
FOLDER="$HOME/Pictures/Lisbon"
PREFIX="lisbon-2024"
LOG="$FOLDER/rename-undo-log.csv"

# Preview unless --apply is given
MODE="preview"
if [ "$1" = "--apply" ]; then MODE="apply"; fi

# Go to the folder, stop if it does not exist
cd "$FOLDER" || exit 1

# Start a fresh undo log only when renaming for real
if [ "$MODE" = "apply" ]; then echo "old_name,new_name" > "$LOG"; fi

# Loop through photos and build each new name
n=1
for old in IMG_*.JPG; do
  [ -e "$old" ] || continue
  new=$(printf "%s-%03d.jpg" "$PREFIX" "$n")
  # Never overwrite an existing file
  if [ -e "$new" ]; then
    echo "SKIP  $old -> $new (name already taken)"
  else
    echo "$old -> $new"
    if [ "$MODE" = "apply" ]; then
      echo "$old,$new" >> "$LOG"
      mv -n "$old" "$new"
    fi
  fi
  n=$((n + 1))
done
echo "Finished in $MODE mode."
3. Steps
  1. Open TextEdit, choose Format > Make Plain Text, paste the script, and save it as rename.sh in your Documents folder.
  2. Open Terminal and type: cd ~/Documents
  3. Preview: bash rename.sh — read the list carefully.
  4. Apply: bash rename.sh --apply
  5. Undo, if needed, by running this in Terminal:
cd ~/Pictures/Lisbon
tail -n +2 rename-undo-log.csv | while IFS=, read old new; do mv -n "$new" "$old"; done
4. Sample preview output
IMG_4521.JPG -> lisbon-2024-001.jpg
IMG_4522.JPG -> lisbon-2024-002.jpg
IMG_4530.JPG -> lisbon-2024-003.jpg
...
IMG_4698.JPG -> lisbon-2024-142.jpg
Finished in preview mode.

Risks: Order follows file names, not the date taken, and running --apply twice overwrites the undo log, so copy the folder to a backup drive first.

Written by AI from this prompt. Your results will vary.

Tips and common edits

Why this works

  • Preview-by-default means nothing changes until you have seen every old-to-new name.
  • The undo log and no-overwrite rule make mistakes reversible instead of destructive.
  • Asking for one real before-and-after example pins down the naming rule exactly.

Use in ChatGPT, Claude or Gemini when you face dozens of messy file names. Have the folder path, your OS and one real before-and-after name ready. Always run the preview first.

Common edits

  • Ask it to number files by the date taken instead of by file name.
  • Request a version that also renames files inside subfolders.

Prompt details

By
Prompts-GPT editors
Published

Did this prompt work?

Keep exploring

More in Coding