Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Re: Want a Batch Code for renaming a file with Current Date,Time

16 views
Skip to first unread message

Timo Salmi

unread,
Jan 29, 2007, 11:25:44 AM1/29/07
to
Timo Salmi <t...@uwasa.fi> wrote:
> Dr J R Stockton <repl...@merlyn.demon.co.uk> wrote:
>> ECHO.|DATE
>> gives something like
>> The current date is: 2007-01-28
>> Enter the new date: (yy-mm-dd)
>
> Now, John, that is an excellent point. So obvious, yet so far missed, at
> least be yours turly. With cmd.exe it is relatively easy to ascertain
> the order from the latter. Yes, I know that is for alt.msdos.batch.nt
> but it will be a good locale-independent method for my FAQ in there.
> Thanks for the idea. I'll try to write a suitable script for it
> requiring no third-party utility, and post there.

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

Timo Salmi

unread,
Jan 30, 2007, 1:53:37 AM1/30/07
to
Dr J R Stockton <repl...@merlyn.demon.co.uk> wrote:
> Timo Salmi <t...@uwasa.fi> posted: But what's the Finnish for yy-mm-dd
> ? <g>

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

foxidrive

unread,
Jan 30, 2007, 2:23:16 AM1/30/07
to
On Tue, 30 Jan 2007 08:53:37 +0200, Timo Salmi <t...@uwasa.fi> wrote:

>> 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"

Todd Vargo

unread,
Jan 30, 2007, 11:43:53 AM1/30/07
to

"foxidrive" <got...@woohoo.invalid> wrote in message
news:e9str2d7avc8foib2...@4ax.com...

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)

foxidrive

unread,
Jan 30, 2007, 12:08:06 PM1/30/07
to
On Tue, 30 Jan 2007 16:43:53 GMT, "Todd Vargo" <tlv...@sbcglobal.netz>
wrote:

>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.

Dr J R Stockton

unread,
Jan 30, 2007, 3:08:35 PM1/30/07
to
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?

--
(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.

Todd Vargo

unread,
Jan 31, 2007, 12:08:28 AM1/31/07
to

"foxidrive" <got...@woohoo.invalid> wrote in message
news:ppuur21cj9oa4a9o4...@4ax.com...

> On Tue, 30 Jan 2007 16:43:53 GMT, "Todd Vargo" <tlv...@sbcglobal.netz>
> wrote:
>
> >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.
>
> >@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

Correction: I forgot to include quotes in line above.

if ("%date%") NEQ ("%today%") goto :begin

Todd Vargo

unread,
Jan 31, 2007, 12:08:29 AM1/31/07
to

"Dr J R Stockton" <repl...@merlyn.demon.co.uk> wrote in message
news:5B+rgyPD...@invalid.uk.co.demon.merlyn.invalid...

> 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?

That depends on whether time/t returns a 12-hour format.
I get 24-hour output here using Win95cmd.

foxidrive

unread,
Jan 31, 2007, 12:15:00 AM1/31/07
to
On Tue, 30 Jan 2007 20:08:35 +0000, Dr J R Stockton
<repl...@merlyn.demon.co.uk> wrote:

>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.

0 new messages