Rename Files with ChatGPT: Preview, Collision Checks and Verified Undo
A filename-cleanup script can produce attractive names while losing data. This tutorial asks ChatGPT for a conservative renaming tool, then checks that collisions are refused, file bytes survive and undo refuses edited content. The demonstration uses a disposable flat folder with fictional files.
Define the naming rule before the command
The demonstrated rule lowercases ASCII letters, replaces runs outside a-z, digits and periods with a hyphen, and trims leading or trailing hyphens. It is a particular policy, not a universal rule for every language or filesystem. Unicode characters are replaced rather than transliterated.
Two starting files, Client A.txt and Client_A.txt, both map to client-a.txt. That is a collision even when the original contents differ. The tool must calculate the complete mapping and reject duplicate or occupied targets before changing the first filename.
Create a Python standard-library flat-folder renamer.
Provide plan, apply and undo commands. Validate the whole mapping first.
Never overwrite an existing target. Reject non-regular entries.
Save an undo manifest outside the folder, with original names,
new names and a SHA-256 for every file. Refuse undo if bytes changed.
Explain concurrency and interrupted-operation limitations.Read the proposed implementation
Save the actual response as rename_tool.py and inspect it before execution. A message saying “safe rename” is not a filesystem guarantee. Python’s Path.rename documentation describes platform-dependent destination behavior, so review the tool’s explicit no-overwrite checks and move operation rather than assuming that a generic rename refuses existing files.
The recorded implementation inventories regular files, validates basenames, detects target collisions conservatively with case-folded names, and stores hashes in the manifest. Keep the working directory separate from the original practice inputs. Do not point these commands at a real documents folder as a first experiment.
Run the deliberate collision first
python3 rename_tool.py plan demo-filesWith both Client files present, this preview must refuse the mapping. Confirm that all filenames and bytes remain as they were. In a fresh disposable copy, remove only the deliberately conflicting demonstration file and run the preview again. Inspect every old-to-new pair, including Final Report.TXT becoming final-report.txt.
A preview that succeeds does not reserve the names. The demonstrated apply command calculates and validates the mapping again. Keep other programs from modifying the folder during this single-user exercise.
Apply with a separate manifest
python3 rename_tool.py apply demo-files undo-manifest.jsonRun this from the directory containing demo-files, so the new manifest is outside the renamed folder. Use an unused manifest path. Verify the actual filenames and compare each renamed file’s SHA-256 with its original value. Matching names alone would miss a content overwrite.
Preserve the manifest as recovery evidence. It records the mapping and the content the tool expects to encounter; it is not a backup containing another copy of every file.
Prove that undo can refuse safely
Change one renamed file in the disposable copy, then try:
python3 rename_tool.py undo demo-files undo-manifest.jsonThe changed hash must cause refusal before any original filenames are restored. Restore the exact demonstration content from your preserved copy, repeat undo and verify both original filenames and bytes. Also test the fresh challenge where a normalized target already exists. The recorded independent check passed collision refusal, exact renamed bytes, changed-file undo refusal and complete restoration.
This exercise is not a crash-atomic transaction and is not designed for concurrent writers. Stop and inspect the manifest and files if an operation is interrupted; blindly repeating commands can obscure the recovery state. Keep an independent backup for real work. If a later audit depends on timestamps, define that separately using the fixed UTC modification-time workflow. Find the other lessons in Tutorials.
James Kennedy is a writer and product researcher at Drives Hero with a background in IT administration and consulting. He has hands-on experience with storage, networking, and system performance, and regularly improves and optimizes his home networking setup.


