Quarksoft Blog

This is where we post useful information for everyone. Lots of it is technical, but some can be used by anyone.

    Getting date information into environment variables

    << Need to make screen captures for documentation?  Some more command line fun - sending email >>

    If you have ever tried to script data downloads and log file manipulation you probably have run across the need to embed the date into a filename during a copy/move operation within a batch file.  I have never been able to find a built-in way to do this within Windows.  Here are a few lines that allows you to get the month, day and year into their respective environment variables.

    If you add the following to your command/batch file:

    @echo off
    FOR /F "TOKENS=2* DELIMS= " %%A IN ('date/t') DO SET mdy=%%A
    FOR /F "TOKENS=1* DELIMS=/" %%A IN ('echo %mdy%') DO SET month=%%A
    FOR /F "TOKENS=2* DELIMS=/" %%A IN ('echo %mdy%') DO SET day=%%A
    FOR /F "TOKENS=3* DELIMS=/" %%A IN ('echo %mdy%') DO SET year=%%A

    You will then be able to use the following environment variables:

    %month% to return the month
    %day% to return the day of the month
    %year% to return the 4 digit year.

    If you need any assistance with this, feel free to comment below.

    Leave a Reply