Since the thread started in alt.msdos.batch but the solution is for
alt.msdos.batch.nt I'll crosspost to both but set the followups to the
latter, only.
So, locale-independent date element's extraction with XP cmd.exe with no
third-party utilities.
@echo off & setlocal enableextensions
::
:: Still debugging?
set debug=true
::
:: Get the date from the first line of the date command
for /f "tokens=5 delims= " %%a in ('echo.^|date') do (
if not defined date_ set date_=%%a
)
if defined debug echo %date_%
::
:: Get the date format from the first line of the date command
for /f "tokens=5 delims= " %%a in ('echo.^|date') do (
set dateform_=%%a)
if defined debug echo %dateform_%
::
:: Get the order of the date information
set field1=%dateform_:~1,2%
set field2=%dateform_:~4,2%
set field3=%dateform_:~7,2%
if defined debug echo %field1% %field2% %field3%
echo.
::
:: Test and set according to the date format order
:: dd-mm-yy
if "%field1%"=="dd" (
set day_=%date_:~0,2%
if "%field2%"=="mm" (
set month_=%date_:~3,2%
set year_=%date_:~6,4%
)
)
:: mm-dd-yy
if "%field1%"=="mm" (
set month_=%date_:~0,2%
if "%field2%"=="dd" (
set day_=%date_:~3,2%
set year_=%date_:~6,4%
)
)
:: yy-mm-dd
if "%field1%"=="yy" (
set year_=%date_:~0,4%
if "%field2%"=="mm" (
set month_=%date_:~5,2%
set day_=%date_:~8,2%
)
)
:: yy-dd-mm
if "%field1%"=="yy" (
set year_=%date_:~0,4%
if "%field2%"=="dd" (
set day_=%date_:~5,2%
set month_=%date_:~8,2%
)
)
::
:: Display the result
if not defined day_ (
echo Error: Unindentified date format
goto :EOF
)
echo day_=%day_%
echo month_=%month_%
echo year_=%year_%
endlocal & goto :EOF
The output might be e.g.
C:\_D\TEST>cmdfaq
29.01.2007
(dd-mm-yy)
dd mm yy
day_=29
month_=01
year_=2007
Not tested for all the eventualities!
All the best, Timo
--
Prof. Timo Salmi ftp & http://garbo.uwasa.fi/ archives 193.166.120.5
Department of Accounting and Business Finance ; University of Vaasa
mailto:t...@uwasa.fi <http://www.uwasa.fi/~ts/> ; FIN-65101, Finland
Useful script files and tricks ftp://garbo.uwasa.fi/pc/link/tscmd.zip
Good question, John, but again there is the VBS non-thrid-party option
for the recent OS:es. Anyway, that's not truly my FAQs' concern, since
they are all-in-English FAQs. In am sure that there are many other
details that would need honing for a Finnish or any other translated
version. I'm not going there.
> I suggest that the script should be coded to exit gracefully if any
> of yy mm dd are not found.
It does. Take a look at the solution draft in alt.msdos.batch.nt (I've
included and reset) where the script exist if the date is not identified.
All the best, Timo
--
Prof. Timo Salmi ftp & http://garbo.uwasa.fi/ archives 193.166.120.5
Department of Accounting and Business Finance ; University of Vaasa
mailto:t...@uwasa.fi <http://www.uwasa.fi/~ts/> ; FIN-65101, Finland
Timo's FAQ materials at http://www.uwasa.fi/~ts/http/tsfaq.html
>> I suggest that the script should be coded to exit gracefully if any
>> of yy mm dd are not found.
>
>It does. Take a look at the solution draft in alt.msdos.batch.nt (I've
>included and reset) where the script exist if the date is not identified.
Timo, I thought to myself "that's a good idea" and did some experimenting
before realising that Ritchie Lawrence beat all of us to the punch, as he
uses that technique in his date routine. I've posted his routines for date
and time below (in a basic format).
:: DateTime - Windows NT4 and above
@echo off
setlocal EnableExtensions
:: Date and time routines by Ritchie Lawrence
set t=2&if "%date%z" LSS "A" set t=1
for /f "skip=1 tokens=2-4 delims=(-)" %%a in ('echo/^|date') do (
for /f "tokens=%t%-4 delims=.-/ " %%d in ('date/t') do (
set %%a=%%d&set %%b=%%e&set %%c=%%f))
(set yy=%yy%&set mm=%mm%&set dd=%dd%)
for /f "tokens=5-8 delims=:. " %%a in ('echo/^|time') do (
set hh=%%a&set nn=%%b&set ss=%%c&set cs=%%d)
if 1%hh% LSS 20 set hh=0%hh%
(set hh=%hh%&set nn=%nn%&set ss=%ss%&set tt=%cs%)
:: The following lines set and echo the timestamp variable
set timestamp=%yy%-%mm%-%dd%_%hh%.%nn%.%ss%
echo timestamp=%timestamp%
:: remove the following echo to rename File.txt
:: to File_YYYY-MM-DD_hh.mm.ss_.txt
for %%a in ("file.txt") do echo ren %%a "%%~na_%timestamp%_%%~xa"
Only issue not addressed is midnight rollover, which can be verified by
checking date has not changed at conclusion of time storage.
@echo off
setlocal EnableExtensions
:: Date and time routines by Ritchie Lawrence
:begin
set today=%date%
set t=2&if "%date%z" LSS "A" set t=1
for /f "skip=1 tokens=2-4 delims=(-)" %%a in ('echo/^|date') do (
for /f "tokens=%t%-4 delims=.-/ " %%d in ('date/t') do (
set %%a=%%d&set %%b=%%e&set %%c=%%f))
(set yy=%yy%&set mm=%mm%&set dd=%dd%)
for /f "tokens=5-8 delims=:. " %%a in ('echo/^|time') do (
set hh=%%a&set nn=%%b&set ss=%%c&set cs=%%d)
if 1%hh% LSS 20 set hh=0%hh%
(set hh=%hh%&set nn=%nn%&set ss=%ss%&set tt=%cs%)
if (%date%) NEQ (%today%) goto :begin
:: The following lines set and echo the timestamp variable
set timestamp=%yy%-%mm%-%dd%_%hh%.%nn%.%ss%
echo timestamp=%timestamp%
:: remove the following echo to rename File.txt
:: to File_YYYY-MM-DD_hh.mm.ss_.txt
for %%a in ("file.txt") do echo ren %%a "%%~na_%timestamp%_%%~xa"
--
Todd Vargo
(Post questions to group only. Remove "z" to email personal messages)
>Only issue not addressed is midnight rollover, which can be verified by
>checking date has not changed at conclusion of time storage.
Nice catch, Todd.
>Only issue not addressed is midnight rollover, which can be verified by
>checking date has not changed at conclusion of time storage.
Does it handle the dreadful 12-hour clock possibility?
--
(c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 IE 6.
Web <URL:http://www.merlyn.demon.co.uk/> - w. FAQish topics, links, acronyms
PAS EXE etc : <URL:http://www.merlyn.demon.co.uk/programs/> - see 00index.htm
Dates - miscdate.htm moredate.htm js-dates.htm pas-time.htm critdate.htm etc.
Correction: I forgot to include quotes in line above.
if ("%date%") NEQ ("%today%") goto :begin
That depends on whether time/t returns a 12-hour format.
I get 24-hour output here using Win95cmd.
>In alt.msdos.batch.nt message <dBKvh.384$gj4...@newssvr14.news.prodigy.
>net>, Tue, 30 Jan 2007 16:43:53, Todd Vargo <tlv...@sbcglobal.netz>
>posted:
>
>>Only issue not addressed is midnight rollover, which can be verified by
>>checking date has not changed at conclusion of time storage.
>
>Does it handle the dreadful 12-hour clock possibility?
Yup.