Rob van der Woude's Scripting Pages

Solutions found in alt.msdos.batch

and alt.msdos.batch.nt

Parse date and time

This post from Garry Deane at alt.msdos.batch.nt shows a further enhancement on Simon Sheppard's GetDate.bat to parse date and time and store them in environment variables.
Especially this line, which makes the parsing less dependent on regional settings:

IF "%date%A" LSS "A" (SET toks=1-3) ELSE (SET toks=2-4)

Note that the variable names still depend on the operating system language, but they are independent of regional/international settings.

Thanks for David Barberis for sending me this tip.

From: Garry Deane <>
Newsgroups: alt.msdos.batch.nt
Subject: Re: BTW
Date: Fri, 16 Aug 2002 04:52:49 GMT
Message-ID: <3d5c7f07.19125090@>
References: <> <> <> <>
X-Newsreader: Forte Free Agent 1.21/32.243
Lines: 80
X-Trace: 1029473582 7994

On Thu, 15 Aug 2002 19:09:05 -0700, "rachel" <>

>now.exe that I use is not the ms version. this version allows me to create a
>file or directory oh the current time. it's a great tool, but it's 16-bit. i
>wish i had a 32-bit version, but i have never found one. maybe there is a
>way to do that in the batch itself?

Yes, it can be done in batch. I'm not sure of the fields that NOW
produces but it looks like you are using the month (word), the day of
the month (numeric) and the hour of the day (numeric). The code below
will produce that or can be adjusted as required for similar results.

A couple of other points:

Since you're using W2k, you can just as easily use SET /p instead of
CHOICE. The only difference is that you need to press <return> after
the y/n reply.

@echo off
echo Would you like to back up that 
set /p choice="data to the floppy? (y/n): "
if /i "%choice%" == "y" (goto floppy-write) else goto end

Also, since you're using Xxcopy, you can use the date and time macros
in Xxcopy to create the directories without the need for the extra
date/time batch code e.g.

xxcopy sourcepath\ destpath\/$mondd$_/$hh$\

will automatically create and copy your source data to a directory
called destpath\Aug16_12\ or whatever is the current date/time.


Date/Time batch code follows:

@echo off
call :get_date
call :get_time
set /a tok=1%mm%-100
for /f "tokens=%tok%" %%a in (
  "Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec") do set mon=%%a
set dest=%mon%%dd%_%hh%
echo Destination name is: %dest%
goto :eof

:: ------------------------------------------------------------------
:: ------------------------------------------------------------------
:: Generic date parser
:: Sets %dd% (01-31), %mm% (01-12) & %yy% (2 or 4 digit)

if "%date%A" LSS "A" (set toks=1-3) else (set toks=2-4)
for /f "skip=1 tokens=2-4 delims=(-)" %%a in ('echo:ˆ|date') do (
  for /f "tokens=%toks% delims=.-/ " %%d in ('date/t') do (
    set %%a=%%d
    set %%b=%%e
    set %%c=%%f
    set toks=
goto :eof

:: ------------------------------------------------------------------
:: ------------------------------------------------------------------
:: Generic time parser
:: Returns time elements as 2 digit values (24 hr clock)
for /f "tokens=5-8 delims=:., " %%a in ('echo:ˆ|time') do (
  set hh=%%a
  set mn=%%b
  set ss=%%c
  set ds=%%d
if %hh% LSS 10 set hh=0%hh%
goto :eof

Back to the top of this page... ]


page last modified: 2016-09-19; loaded in 0.0039 seconds