Subscribe to Windows IT Pro

 

Get Newsletters

  • Get the Latest News
  • Product Updates
  • Helpful Tricks
  • Productivity Tips

Subscribe Now!

August 09, 2000 12:00 AM

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

Windows IT Pro
InstantDoc ID #13575
Rating: (9)

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
REM -------------------------------------------------------------
IF %1.

. GoTo USAGE
Set CURRDATE=%TEMP%\CURRDATE.TMP
Set CURRTIME=%TEMP%\CURRTIME.TMP

DATE /T > %CURRDATE%
TIME /T > %CURRTIME%

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 RENAME %1 %1_%YYYYMMDD%%HHMM%
RENAME %1 %1_%YYYYMMDD%%HHMM%
GoTo END

:USAGE
Echo Usage: DateName filename
Echo Renames filename to filename_YYYYMMDDHHMM
GoTo END

:END
REM
TITLE Command Prompt

Example:

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<br>
    FOR /F "tokens=1-4 delims=/" %%i in ('date/t') do set file=%%i%%j%%k<br>
    Set LOG=drive:\directory\filename-%file%.log

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

You could also use

C:\&gt; <b>net time &gt;&gt; file.txt</b>

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


Related Content:

ARTICLE TOOLS

Comments
  • k
    2 years ago
    Oct 08, 2010

    Hi, I am using the below syntax.

    for %%i IN (%1) do set ab=%%~ti
    set mfl=%ab:~6,4%%ab:~0,2%%ab:~3,2%_%ab:~11,2%%ab:~14,2%%ab:~17,2%.TXT

    But, when execute this code in my local computer, it is working correct as,
    set ab=09/28/2010 11:08PM.

    And it is not working correct in the server, it brings up as,
    set ab=09/28/10 11:08p

    Is that any settings to be made on the server or on the code?

  • H
    3 years ago
    Dec 02, 2009

    Is there away to pass a file that contains a list of files that can have the date and time appended to it instead of typing the individual file as the parameter that is being passed?

    I want to automate the appending of the date and time to multiple files on a daily basis, using Scheduler to run the batch file.

    I am getting around it presently by having multiple batch files that are identical except for the file that gets the date and time appended to it. That works OK but it makes for a lot of files that all do the same thing. Thanks for posting the code it works great for me.

  • Anonymous User
    7 years ago
    Aug 24, 2005

    very good it solved my problem

  • Anonymous User
    7 years ago
    Aug 10, 2005

    This works too:
    copy c:\\somefolder\\filename.txt d:\\backupfolder\\filename_%date:~4,2%_%date:~7,2%_%date:~10,4%.txt

  • Anonymous User
    7 years ago
    Jul 14, 2005

    THANKS! This accomplished exactly what I needed.

You must log on before posting a comment.

Are you a new visitor? Register Here

advertisement

advertisement

White Papers

Get your Windows 7 deployment off to the right start by implementing PC lockdown. A locked-down environment is easier and cheaper to support since users are less likely to make unnecessary changes to the core system configuration - read more here!

Essential Guides

Is your iSCSI "lossy"? The reality is that most off-the-shelf Ethernet hardware deployed for iSCSI can lose packets, resulting in slow performance or application downtime. Learn how to assess your current iSCSI infrastructure and engineer an advanced iSCSI SAN infrastructure.

Web Seminars

What's the best way to keep your network safe from malware? In this web seminar, security expert Greg Shields suggests an alternative method to the traditional blacklisting approach that is common with anti-virus and anti-malware solutions.

eLearning Series

We bring the experts direct to you to share their real-world perspective and expertise. During each event, three sessions stream in real time, so you can learn, ask questions, and get solutions.
Upcoming event: Getting the Most with Exchange 2010 with Paul Robichaux

Subscribe to Windows IT Pro!

Windows is a trademark of the Microsoft group of companies. Windows IT Pro is used by Penton Media Inc. under license from owner.