How to rename a filename to current date with batch
Here are practical ways to rename a file (or many files) to the current date on Windows 11 and on Debian/Ubuntu.
Windows 11 (Batch / CMD)
Basic (single file) on Windows 11
@echo off
set "file=example.txt"
for /f %%i in ('powershell -NoProfile -Command "Get-Date -Format yyyy-MM-dd"') do set today=%%i
ren "%file%" "%today%.txt"
Multiple files (same extension) on Windows 11
Rename all .txt files to the current date + index:
@echo off
set count=1
for /f %%i in ('powershell -NoProfile -Command "Get-Date -Format yyyy-MM-dd"') do set today=%%i
for %%f in (*.txt) do (
ren "%%f" "%today%_!count!.txt"
set /a count+=1
)
Important: enable delayed expansion:
setlocal enabledelayedexpansion
Why PowerShell is used inside CMD
Windows cmd date format depends on system locale, so:
PowerShell Get-Date gives consistent format (yyyy-MM-dd)
Windows 11 (Pure PowerShell – easier way)
Rename-Item "example.txt" "$(Get-Date -Format 'yyyy-MM-dd').txt"
Multiple files:
$i=1
Get-ChildItem *.txt | ForEach-Object {
Rename-Item $_ -NewName "$(Get-Date -Format 'yyyy-MM-dd')_$i.txt"
$i++
}
Debian / Ubuntu (Bash)
Single file on Debian/Ubuntu
mv example.txt "$(date +%F).txt"
%F = YYYY-MM-DD
Multiple files on Debian/Ubuntu
i=1
for f in *.txt; do
mv "$f" "$(date +%F)_$i.txt"
((i++))
done
Rename while preserving original name
for f in *.txt; do
mv "$f" "$(date +%F)_$f"
done
Alternative (Linux rename tool)
rename 's/^/$(date +%F)_/' *.txt
(Some systems use a different rename version, so syntax may vary.)
Windows Command Line
In windows environment, it is possible to rename files through command prompt. You need "ren" command to complete the operation.
ren old-file-name new-file-name
It is also possible to use command prompt commands from batch files.
To rename test.log to YYYYMMDD.log create a rename.bat file with content
ren "test.log" "%date:~10,4%%date:~7,2%%date:~4,2%.log"
Using this batch command, you can rename filenames to current date.