1.

Solve : Date manipulation in Windows XP batch scripts...?

Answer»

Several members have asked how to add dates to filenames etc.  The ability to manipulate dates in batch scripting is very LIMITED.  Day, Month and Year can be extracted from either the %date% environment variable or the "date /t" command only if the date format is known.  One cannot go back or forward with either of these two without checking for things like a Leap Year, new Month, new Year.  The following two codings will extract day month and year from the date format day mm/dd/yyyy.


Code: [Select]echo off
cls

FOR /f "tokens=1-4 delims=/ " %%a in ('date /t') do (
set day=%%a
set mm=%%b
set dd=%%c
set yy=%%d
)

echo %day% %dd% %mm% %yy%

Code: [Select]echo off
cls

set day=%date:~0,3%
set mm=%date:~4,2%
set dd=%date:~7,2%
set yy=%date:~-4%

echo %day% %dd% %mm% %yy%

But what if one wanted to use a date which was 3, 30 or even 300 days ago or hence without knowing the date format?  It is possible in batch scripting but not without screeds of error-prone coding.


For those who do not like to permanently install programs/routines on their pc the following code gives the opportunity to manipulate dates to suit individual requirements.  (Only one small temporary file is created in the %Temp% folder and is deleted when no longer required).  The code will be successful with any date format.

Code: [Select]:: Acknowledgement.  Dias de verano for his input to set up the .vbs file.

:: Syntax: filename only - set/display today's date.
::         filename -n   - set/display today's date minus n days.
::         filename +n   - set/display today's date plus  n days.

echo off
cls

:: Check for incorrect commandline entry..
set a=%1

if "%a%"=="" goto start
if "%a:~0,1%"=="+" goto start
if "%a:~0,1%"=="-" goto start

echo.&echo.
:syntax
echo                     Incorrect entry - RUN aborted
echo.
echo                     Press any key to continue....
pause > nul
cls & exit/b

:: Create/run temporary .vbs file (extracts date components).
:start
set vb=%temp%\newdate.vbs
echo Newdate = (Date()%1)>%vb%
echo Yyyy = DatePart("YYYY", Newdate)>>%vb%
echo   Mm = DatePart("M"   , Newdate)>>%vb%
echo   Dd = DatePart("D"   , Newdate)>>%vb%
echo   Wd = DatePart("WW"  , Newdate)>>%vb%
echo   Wn = DatePart("Y"   , Newdate)>>%vb%
echo   Ww = datepart("W"   , Newdate)>>%vb%

echo Wscript.Echo Yyyy^&" "^&Mm^&" "^&Dd^&" "^&Wd^&" "^&Ww^&" "^&Wn>>%vb%

FOR /F "tokens=1-6 delims= " %%A in ('cscript //nologo %vb%') do (
        set Yyyy=%%A
        set Mm=%%B
        set Dd=%%C
        set Week#=%%D
        set Weekday#=%%E
        set Day#=%%F
)
del %vb%
::                    [Environment Variables are:
::                     %Yyyy%     Year in the format yyyy
::                     %Mm%       Month in the format m or mm
::                     %Dd%       Number of the day in month in the format d or dd (range 1 thru'31)
::                     %Week#%    Week number in year in the format w or ww (range 1 thru' 52)
::                     %Weekday#% Day number in week in the format w
::                                     (range 1 thru' 7, day#1 is Sunday)
::                     %Day#%     day number in the year in the format d thru ddd
::                                     (range 1 thru' 366)]

set days=
if not "%1"=="" set days=days
if %Mm% lss 10 set Mm=0%Mm%
if %Dd% lss 10 set Dd=0%Dd%
 
echo.&echo.&echo.&echo.&echo.&echo.
echo            Date to be displayed is today %1 %days%
echo.
echo            Example of date in yyyy/mm/dd format:   %Yyyy%/%Mm%/%Dd%
echo               "    "   "    " mmddyyyy     "   :   %Mm%%Dd%%Yyyy%
echo.&echo.&echo.
echo            Press any key to continue...
pause > nul
cls




For those who have no objection to installing a small utility in order to manipulate dates please download DOFF.ZIP from http://www.jfitz.com/dos/index.html and unzip it to somewhere in your Path.  The code below will be successful with any date format.

Code: [Select]echo off
cls

:: Syntax: filename only - set/display today's date.
::         filename -n   - set/display today's date minus n days.
::         filename +n   - set/display today's date plus  n days.

for /f "tokens=1-3 delims=/ " %%a in ('doff mm/dd/yyyy %1') do (
    set mm=%%a
    set dd=%%b
    set yyyy=%%c
)

set days=
if not "%1"=="" set days=days

echo.&echo.&echo.&echo.&echo.&echo.
echo Date to be displayed is today %1 %days%
echo.
echo Date in the format dd mm yyyy           %dd% %mm% %yyyy%
echo.&echo.
echo Date in the format yyyy/mm/dd           %yyyy%/%mm%/%dd%


Note that syntax error checking is minimal or non-existent.  If the syntax is incorrect the outcomes are unpredictable.

Good luck.
Very useful, thank you.This may SOUND like a stupid question but can you explain the line:for /f "tokens=1-4delims= " %%a in ('date') do (

I know it a for loop but what I wondering is what it does because you did not comment your code. I would much appreciated if you could message me or explain it in the thread.Steppingpanther - welcome to the CH forums.

There is no such thing as a stupid question.

Perhaps you would like to enter FOR/? at the command prompt and read about the For command then ask for clarification of anything you don't understand. 

Basically the For command you quote searches the output of the Date /t command and sets individual parts of that output to Environment Variables.

Hope this helps & thank you for your interest.a bit off-topic, but why is your experience set to "Beginner", Dusty?"Beginner" just arrived one day, I didn't pay for it or ask for it, much like MS updates.  But now you know I'm in the Beginner category you'll be more likely to forgive any little transgressions, omissions or downright stupidity..

Edit: When deciding on one's Experience category one may not know what a byte is, even if it jumped up and nibbled or bit on your posterior, but still consider oneself to be an EXPERT.  I'm happy to be a Beginner, now if only I could find out how to stop also being an Expert.That's why I called myself a Guru, since my experience of them is that they say they are all-wise but without any proof.



Discussion

No Comment Found