How to Create Timestamps in Batch Files

Save

You create and use batch files to automate different system administration tasks, such as backups, program and database updates or recording errors, when an application problems occur. DOS commands that are used in the batch files can include a variable, such as date and time. By using the string of the output from the date and time DOS commands you can create timestamps in the batch files to record time information during execution of system maintenance procedures.

  • Click "Start," "Notepad" to open Notepad editing program.

  • Click "File," "New."

  • Enter the following commands in the open window:

    @echo off

    cls

    rem Timestamp Generator

    rem Parse the date (e.g., Fri 02/08/2008)

    set cur_yyyy=%date:~10,4%

    set cur_mm=%date:~4,2%

    set cur_dd=%date:~7,2%

    rem Parse the time (e.g., 11:17:13.49)

    set cur_hh=%time:~0,2%

    if %cur_hh% lss 10 (set cur_hh=0%time:~1,1%)

    set cur_nn=%time:~3,2%

    set cur_ss=%time:~6,2%

    set cur_ms=%time:~9,2%

    rem Set the timestamp format

    set timestamp=%cur_yyyy%%cur_mm%%cur_dd%-%cur_hh%%cur_nn%%

    cur_ss%%cur_ms%

    rem Do something with it

    echo Timestamp: %timestamp%

    rem Clear the environment variables

    set cur_yyyy=

    set cur_mm=

    set cur_dd=

    set cur_hh=

    set cur_nn=

    set cur_ss=

    set cur_ms=

    set timestamp=

  • Click "File," "Save." Enter "mybatchfile.bat" in the "Save As" box. Click "OK" to create the batch file with timestamps.

References

  • Photo Credit Jupiterimages/Photos.com/Getty Images
Promoted By Zergnet

Comments

You May Also Like

Related Searches

Check It Out

Geek Vs Geek: Robot battles, hoverboard drag race, and more

M
Is DIY in your DNA? Become part of our maker community.
Submit Your Work!