Rename a file (or folder) by appending the current date and time to the existing filename:
@ECHO off SETLOCAL IF [%1] NEQ  goto s_start Echo StampMe.cmd Echo Rename a file with the DATE/Time Echo: Echo Syntax Echo STAMPME TestFile.txt Echo: Echo STAMPME "Test File.txt" Echo: Echo STAMPME "c:\docs\Test File.txt" Echo: Echo Will rename the file in the format "Test Filefirstname.lastname@example.org" Echo: Echo In a batch file use CALL STAMPME ... GOTO :eof :s_start Set _file=%~n1% Set _pathname=%~f1% Set _ext=%~x1% For /f "skip=8 tokens=2,3,4,5,6,7,8 delims=: " %%D in ('robocopy /l * \ \ /ns /nc /ndl /nfl /np /njh /XF * /XD *') do ( set "_dow=%%D" set "_month=%%E" set "_day=%%F" set "_HH=%%G" set "_MM=%%H" set "_SS=%%I" set "_YYYY=%%J" ) :: Display the date/time in ISO 8601 format: Set _datetimestamp=%_YYYY%-%_MM%-%_day%@%_HH%-%_MM%-%_SS% Echo %_datetimestamp% :: Rename the file REN "%_pathname%" "%_file%-%_datetimestamp%%_ext%"
The last line of the batch file does the REName. Based on SO: How get data/time independent from localization: code by npocmaka.
If you save this as a batch script (stampme.cmd) on the desktop, you can drag and drop any file, from a local drive or UNC path, and drop it onto the icon - that will rename the file without moving it from the original location.
c:\> StampMe "sample file.txt"
c:\> StampMe "C:\logs\data\errorlog.txt"
c:\> StampMe "C:\docs\example folder"
An alternative 'quick and dirty' way to do the same thing in one line is shown below, this does work but is less robust as the results will vary according to regional/control panel settings.
c:\> REN "sample file.txt" "* %Date:/= % %Time::=.%.*"
“The time you enjoy wasting is not wasted time” ~ Bertrand Russell
Standard date and time notation - YYYY-MM-DD.
How-to: DelOlder.cmd - Delete files more than n days old.
How-to: GetDate and GetTime scripts.
How-to: Stampme.ps1 - Rename files with PowerShell.