CC1 · The fix and what went wrongQwen3.8-27B
Tested by running · 17 October 2026, Python 3, on copies of the folder- On Grace’s folder as it stands, it renamed the three remaining scans to 2026-10-14 1630, 2026-10-16 0932 and 2026-10-16 0932(1), and left the two misnamed files and the settings file alone. Each file kept its own contents.
- When the new name was already taken, it saved the scan as “2026-10-16 0932 scan (1).pdf” instead of overwriting.
- Run a second time, it changed nothing.
Checked by Brightwork · 1 correction- It says the old script would have made the two same-minute scans clash. In fact the old script crashes on the “(1)” file (“unconverted data remains”). The fixed script handles both, as tested.
What went wrong
The old script had the date order backwards.
The scanner names files like this:
Scan_DD-MM-YYYY_HHMM.pdf
So Scan_16-10-2026_0932.pdf means 16 October 2026 at 09:32.
The old script treated the date as month-day-year.
That is why it produced wrong dates for the first two files.
Then it stopped at Scan_14-10-2026_1630.pdf because it tried to use 14 as a month. There is no 14th month.
What the error does not show
Two files have already been renamed wrongly
The error log shows these lines:
Scan_02-10-2026_1015.pdf -> 2026-02-10 1015 scan.pdf
Scan_09-10-2026_1452.pdf -> 2026-09-10 1452 scan.pdf
Those new names are wrong.
The correct names should be:
2026-10-02 1015 scan.pdf
2026-10-09 1452 scan.pdf
So the two files currently in the folder that are named:
2026-02-10 1015 scan.pdf
2026-09-10 1452 scan.pdf
need to be renamed manually.
If Windows says a file with the new name already exists, stop and check manually. Do not let it replace the file.
The script stopped early
The folder still contains:
Scan_14-10-2026_1630.pdf
Scan_16-10-2026_0932.pdf
Scan_16-10-2026_0932(1).pdf
scanner-settings.ini
The old script did not finish the job.
The scanner settings file will also stop it
The old script expects every file to be a scan file with the scanner name pattern.
It will not handle scanner-settings.ini safely.
Same-minute duplicates will cause a name clash
The scanner adds (1), (2) and so on when two pages are scanned in the same minute.
The old script did not keep that marker.
Both of these files would try to become the same name:
Scan_16-10-2026_0932.pdf
Scan_16-10-2026_0932(1).pdf
The fixed script keeps the duplicate marker.
What to do now
- Do not run the old script again.
- Manually rename the two files that were already renamed wrongly:
2026-02-10 1015 scan.pdf to 2026-10-02 1015 scan.pdf
2026-09-10 1452 scan.pdf to 2026-10-09 1452 scan.pdf
- Replace the old script with the fixed script below.
- Run the fixed script on the scan folder.
- Check the printed lines before and after the run.
For the folder you showed, the fixed script should:
- leave the two already renamed files alone
- rename
Scan_14-10-2026_1630.pdf
- rename
Scan_16-10-2026_0932.pdf
- rename
Scan_16-10-2026_0932(1).pdf
- leave
scanner-settings.ini alone
It should not stop just because one file is not a scan file.
Fixed script
"""Rename scanner output so files sort by date.
Expected scanner names:
Scan_16-10-2026_0932.pdf
Scan_16-10-2026_0932(1).pdf
Renamed to:
2026-10-16 0932 scan.pdf
2026-10-16 0932 scan(1).pdf
Usage:
python rename_scans.py FOLDER
"""
import os
import re
import sys
from datetime import datetime
PATTERN = re.compile(
r"^Scan_(\d{2})-(\d{2})-(\d{4})_(\d{2})(\d{2})(?:\((\d+)\))?\.pdf$"
)
def make_unique_path(path, source_path):
"""Return a path that does not already exist.
This stops the script from overwriting an existing file.
"""
if not os.path.exists(path) or os.path.abspath(path) == os.path.abspath(source_path):
return path
root, ext = os.path.splitext(path)
number = 1
while True:
candidate = f"{root} ({number}){ext}"
if (
not os.path.exists(candidate)
or os.path.abspath(candidate) == os.path.abspath(source_path)
):
return candidate
number += 1
def main():
if len(sys.argv) != 2:
print("Usage: python rename_scans.py FOLDER")
return 1
folder = sys.argv[1]
if not os.path.isdir(folder):
print(f"Folder not found: {folder}")
return 1
renamed = 0
skipped = 0
for name in sorted(os.listdir(folder)):
match = PATTERN.match(name)
if not match:
print(f"Skipped (not a scanner PDF): {name}")
skipped += 1
continue
day = int(match.group(1))
month = int(match.group(2))
year = int(match.group(3))
hour = int(match.group(4))
minute = int(match.group(5))
duplicate = match.group(6)
try:
when = datetime(year, month, day, hour, minute)
except ValueError as exc:
print(f"Skipped (invalid date or time): {name}: {exc}")
skipped += 1
continue
base_name = when.strftime("%Y-%m-%d %H%M scan.pdf")
if duplicate:
target_name = base_name[:-4] + f"({duplicate}).pdf"
else:
target_name = base_name
source_path = os.path.join(folder, name)
target_path = make_unique_path(
os.path.join(folder, target_name),
source_path,
)
if os.path.abspath(target_path) == os.path.abspath(source_path):
print(f"Skipped (already renamed): {name}")
skipped += 1
continue
os.rename(source_path, target_path)
renamed += 1
print(f"{name} -> {os.path.basename(target_path)}")
print(f"Done. Renamed {renamed}, skipped {skipped}.")
return 0
if __name__ == "__main__":
sys.exit(main())