How can I append the date and time to a file?

Pinched this from the Windows IT Pro site.  I used it to “rollover” system log files back in 2001.  My particular choice is the one I’ve highlighted in blue.

How can I append the date and time to a file?

A. You can use the batch file below which will rename a file to filename_YYYYMMDDHHMM.

@Echo OFF
TITLE DateName
REM DateName.CMD
REM takes a filename as %1 and renames as %1_YYMMDDHHMM
REM ————————————————————-
IF %1.==. GoTo USAGE


Set PARSEARG="eol=; tokens=1,2,3,4* delims=/, "
For /F %PARSEARG% %%i in (%CURRDATE%) Do SET YYYYMMDD=%%l%%k%%j

Set PARSEARG="eol=; tokens=1,2,3* delims=:, "
For /F %PARSEARG% %%i in (%CURRTIME%) Do Set HHMM=%%i%%j%%k


Echo Usage: DateName filename
Echo Renames filename to filename_YYYYMMDDHHMM

TITLE Command Prompt


D:Exchange> datetype logfile.log
RENAME logfile.log logfile.log_199809281630

Another method is as follows without temporary files. Also a leading zero is inserted for hour values below 10:
for /f "tokens=1,2" %%u in (‘date /t’) do set d=%%v
for /f "tokens=1" %%u in (‘time /t’) do set t=%%u
if "%t:~1,1%"==":" set t=0%t%
set timestr=%d:~6,4%%d:~3,2%%d:~0,2%%t:~0,2%%t:~3,2%
echo %timestr%

Other date options include LOGTIME.EXE which enables you to specify a string and then writes the time followed by the string to the file logtime.log at the current default directory.

The other option is NOW.EXE which just replaces itself with the date and time, e.g.

D:temp>now Batch complete
Mon Sep 28 15:54:19 1998 — Batch complete

Both of the above utilities are part of the resource kit.

Another way is by using the following FOR command, a log file can be created using real dates.

rem created unique log filename, e.g. Wed0804
FOR /F "tokens=1-4 delims=/" %%i in (‘date/t’) do set file=%%i%%j%%k
Set LOG=drive:directoryfilename-%file%.log

The result is a file named filename-date.log. Easier and works great!

You could also use

C:> net time >> file.txt

which also adds the time to the bottom of a file (but also has a success message so one of the other methods is better).

You can also use
Echo | more | time | find "current">>file.txt

Bookmark and Share