Can anyone suggest a script that will rename a file with the
existing filename appended with date stamp? Something that
can be used in a .bat file?

Dani AI

Generated

's Python snippet is a solid cross-platform answer, but for a native Windows .bat approach here's a compact, practical batch that appends an ISO date (YYYY-MM-DD) to each filename. It calls PowerShell only to get a locale-independent date, handles spaces in names, preserves extensions, and skips a rename if the target already exists.

@echo off
setlocal enabledelayedexpansion

rem get ISO date (yyyy-MM-dd) reliably via PowerShell
for /f "usebackq delims=" %%D in (`powershell -NoProfile -Command "Get-Date -Format yyyy-MM-dd"`) do set "dt=%%D"

rem change (*) to a specific pattern like (*.jpg) if needed
for %%F in (*) do (
  set "name=%%~nF"
  set "ext=%%~xF"
  set "newname=!name! !dt!!ext!"
  if not exist "!newname!" ren "%%F" "!newname!"
)

endlocal

How it works: Get-Date -Format yyyy-MM-dd gives a stable date string regardless of regional %DATE% formats; the first FOR captures that into %dt%. The second FOR iterates files, %%~nF and %%~xF split name and extension, and delayed expansion (!var!) lets the loop build the new filename. The script skips renaming if the destination already exists.

Notes and tips: change the file pattern (for example for %%F in (*.txt)) to limit which files are processed. To include time, use a different PowerShell format (for example yyyy-MM-dd_HH-mm-ss). Test the batch in a copy folder first. If PowerShell isn't available on an older system you can use WMIC or parse %DATE%, but those approaches are more fragile because of locale differences. If you want automatic numeric suffixing when a target exists, say so and I’ll post that small extension.

Recommended Answers

All 2 Replies

Python or vbScript? This is the Python version

import os
import sys
import glob
import shutil
import datetime

for arg in sys.argv[1:]:
    for file in glob.glob(arg.replace("[", "[[]")):
        base,extn = os.path.splitext(file)
        newname = base + ' ' + str(datetime.date.today()) + extn
        if not os.path.exists(newname):
            shutil.move(file,newname)

Save it in a file like appenddate.py and run it by

appenddate file [file...]

where file is a file name or pattern (Windows wildcards * and ?) as in

appenddate *.jpg *.txt

That weird replace in the glob line is to account for [ and ] being valid Windows characters in file names but something else in a glob call.

You're welcome.

commented: excellent +0
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.