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

Current date stamp in batch file?

1,618 views
Skip to first unread message

Harry Potter

unread,
May 5, 2013, 12:14:18 PM5/5/13
to
I want to create a batch file in WinNT to automatically back up a program folder to a progam back-up folder quickly and easily for the purpose of having a copy to which to revert after experimentation or significant code change. In order to do this, I need to both extract the system date and report it in YYYY-MM-DD format into an archive target name. How do I do these?

foxidrive

unread,
May 5, 2013, 12:20:46 PM5/5/13
to
:: timestamp YYYY-MM-DD

@echo off
for /f "delims=" %%a in ('wmic OS Get localdatetime ^| find "."') do set dt=%%a
set date_format=%dt:~0,4%-%dt:~4,2%-%dt:~6,2%
echo %date_format%
pause



--
foxi

foxidrive

unread,
May 5, 2013, 12:22:01 PM5/5/13
to
On 6/05/2013 02:14, Harry Potter wrote:
> I want to create a batch file in WinNT

Did you really mean Win NT or something later, like XP Pro and after?


--
foxi

Harry Potter

unread,
May 5, 2013, 12:40:01 PM5/5/13
to
On Sunday, May 5, 2013 12:22:01 PM UTC-4, foxidrive wrote:
> Did you really mean Win NT or something later, like XP Pro and after? -- foxi

I don't fully get the idea, so I'll copy and use the given batch file and study it later. Thank you.

BTW, When I said WinNT, I actually had WinXP and WinVista in mind.

foxidrive

unread,
May 5, 2013, 12:44:11 PM5/5/13
to
The code will work on XP Pro and higher as they all have WMIC.EXE

For XP Home you'll need different code.




:: date time from Richie Lawrence
:: GetDate.cmd ::::::::::::::::::::::::::::::::::::::::::::::::::::::
@ECho off & Setlocal
Call :GetDate yy mm dd
Echo Today in ISO format = %yy%-%mm%-%dd%
Goto :Eof
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:GetDate yy mm dd
::
:: By: Ritchie Lawrence, 2002-06-15. Version 1.0
::
:: Func: Loads local system date components into args 1 to 3.
:: For NT4/2000/XP/2003.
::
:: Args: %1 var to receive year, 4 digits (by ref)
:: %2 var to receive month, 2 digits, 01 to 12 (by ref)
:: %3 Var to receive day of month, 2 digits, 01 to 31 (by ref)
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
setlocal ENABLEEXTENSIONS
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))
:: This line for an English locale
endlocal&set %1=%yy%&set %2=%mm%&set %3=%dd%&goto :EOF
:: replace previous line with the following line for a German locale
endlocal&set %1=%JJ%&set %2=%MM%&set %3=%TT%&goto :EOF
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::




--
foxi

Harry Potter

unread,
May 5, 2013, 12:59:57 PM5/5/13
to
That's a lot of code! Maybe I'll just create a temp. file using echo (i.e. echo Hello, world!>%temp%\t.txt), use for with the same file to set a variable to the date of the file, then delete the file. This leaves one problem, though: how do I format the date?

foxidrive

unread,
May 5, 2013, 1:07:34 PM5/5/13
to
There are simpler ways to do it, if you can limit it to the one machine where regional date settings
aren't ever changed.

Knowing the output of echo %date% would help.



--
foxi

Harry Potter

unread,
May 5, 2013, 1:41:31 PM5/5/13
to
On Sunday, May 5, 2013 1:07:34 PM UTC-4, foxidrive wrote:
> There are simpler ways to do it, if you can limit it to the one machine where regional date settings aren't ever changed. Knowing the output of echo %date% would help. -- foxi

I don't understand. I need the file to run, although with edits, on multiple computers, mostly on WinXP and WinVista. The date format is for easy sorting by name. I might want to program on Win98, but I *know* it can't do any of this at all. :(

If by regional you mean locale and country, then I am in the U.S. and only need to stay in the U.S, as do my software.

Harry Potter

unread,
May 5, 2013, 1:45:44 PM5/5/13
to
If you mean the contents of the %date% variable, I just opened an instance of the command prompt and typed set, and I didn't find the variable.

Harry Potter

unread,
May 5, 2013, 2:14:29 PM5/5/13
to
I did a little experimentation. I created a batch file that accepted a file parameter and returned the file's date/time stamp and tried it out. If I could do that with the test file as stated above and parsed it as stated by foxidrive, I could do it! I believe all the computers I'm using return the same date style. Thank you for your input. I wouldn't have thought of this without your help.

foxidrive

unread,
May 6, 2013, 12:53:56 AM5/6/13
to
Try this.

for /f "tokens= 1-4 delims=/- " %%a in ("%date%") do echo "%%a" "%%b" "%%c" "%%d"

%date% is a system variable and isn't listed in SET. Same as %random% etc.

Try: echo %date%


You're in the US but the regional settings can be changed to add the day name or remove it - you can't be
sure. That's why the more involved script is better: Run this and see...

The top 6 lines show you the result.


:: GetDate.cmd ::::::::::::::::::::::::::::::::::::::::::::::::::::::
@ECho off & Setlocal
Call :GetDate yy mm dd
Echo Today in ISO format = %yy%-%mm%-%dd%
pause
Goto :Eof
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:GetDate yy mm dd
::
:: By: Ritchie Lawrence, 2002-06-15. Version 1.0
::
:: Func: Loads local system date components into args 1 to 3.
:: For NT4/2000/XP/2003.
::
:: Args: %1 var to receive year, 4 digits (by ref)
:: %2 var to receive month, 2 digits, 01 to 12 (by ref)
:: %3 Var to receive day of month, 2 digits, 01 to 31 (by ref)
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
setlocal ENABLEEXTENSIONS
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))
endlocal&set %1=%yy%&set %2=%mm%&set %3=%dd%&goto :EOF
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::






--
foxi

Harry Potter

unread,
May 6, 2013, 10:21:17 AM5/6/13
to
Does this batch file also work under WinVista?

foxidrive

unread,
May 6, 2013, 10:39:12 AM5/6/13
to
On 7/05/2013 00:21, Harry Potter wrote:

> Does this batch file also work under WinVista?

The one by Ritchie Laurence does. Not sure which one you meant.

Google groups has a quote option - try and trim unneeded bits when you use it.
It also double spaces - use a Usenet client if you plan to visit regularly, and help others too.

Thunderbird handles Usenet as well as email and is free.


--
foxi

Zaidy036

unread,
May 6, 2013, 5:35:54 PM5/6/13
to
>a little simpler:
echo %date% gives my system date = Mon 05/06/2013
SET DOW=%date:~-14,3% then echo %dow% gives: Mon
SET dd=%date:~-10% then echo %dd% gives: 05/06/2013




SET DOW=%date:~-14,3%

Harry Potter

unread,
May 7, 2013, 12:03:06 PM5/7/13
to
foxidrive: I think I can implement your batch file separately to return the date in YYYY-MM-DD format and return it in a variable. Another batch file (i.e. my backup prg. batch file) can call it and use the results. All the batch files could be in the same folder and this batch file can be located on the path. I will work on this at my earliest convenience. Thank you.

Harry Potter

unread,
May 7, 2013, 12:07:22 PM5/7/13
to
Zaidy026: Does your method *always* read the date in the given format? If it does, I can replace the batch file for program backup in a different thread.

foxidrive

unread,
May 6, 2013, 12:44:59 AM5/6/13
to
On 6/05/2013 04:14, Harry Potter wrote:
Try this.

for /f "tokens= 1-4 delims=/- " %%a in ("%date%") do echo "%%a" "%%b" "%%c" "%%d"

%date% is a system variable and isn't listed in SET. Same as %random% etc.

Try: echo %date%


You're in the US but the regional settings can be changed to add the day name or remove it - you can't be
sure. That's why the more involved script is better: Run this and see...

The top 6 lines show you the result.


:: GetDate.cmd ::::::::::::::::::::::::::::::::::::::::::::::::::::::
@ECho off & Setlocal
Call :GetDate yy mm dd
Echo Today in ISO format = %yy%-%mm%-%dd%
pause
Goto :Eof
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:GetDate yy mm dd
::
:: By: Ritchie Lawrence, 2002-06-15. Version 1.0
::
:: Func: Loads local system date components into args 1 to 3.
:: For NT4/2000/XP/2003.
::
:: Args: %1 var to receive year, 4 digits (by ref)
:: %2 var to receive month, 2 digits, 01 to 12 (by ref)
:: %3 Var to receive day of month, 2 digits, 01 to 31 (by ref)
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
setlocal ENABLEEXTENSIONS
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))
endlocal&set %1=%yy%&set %2=%mm%&set %3=%dd%&goto :EOF
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::






--
foxi

foxidrive

unread,
May 6, 2013, 10:30:08 AM5/6/13
to
On 7/05/2013 00:21, Harry Potter wrote:
> Does this batch file also work under WinVista?

foxidrive

unread,
May 6, 2013, 10:33:56 AM5/6/13
to
On 7/05/2013 00:21, Harry Potter wrote:

> Does this batch file also work under WinVista?

Zaidy036

unread,
May 7, 2013, 6:02:27 PM5/7/13
to
On 5/7/2013 12:07 PM, Harry Potter wrote:
> Zaidy026: Does your method *always* read the date in the given format? If it does, I can replace the batch file for program backup in a different thread.
>
The variations are to extract part of any variable so what you get
depends on your system default for date.

See: http://ss64.com/nt/syntax-substring.html

mokomoji

unread,
Jul 8, 2013, 10:34:18 AM7/8/13
to
2013년 5월 6일 월요일 오전 1시 14분 18초 UTC+9, Harry Potter 님의 말:
> I want to create a batch file in WinNT to automatically back up a program folder to a progam back-up folder quickly and easily for the purpose of having a copy to which to revert after experimentation or significant code change. In order to do this, I need to both extract the system date and report it in YYYY-MM-DD format into an archive target name. How do I do these?

http://blog.naver.com/mokomoji/130159916842
registry
reg query "HKCU\Control Panel\International" /v sLongDate
reg query "HKCU\Control Panel\International" /v sShortDate
your system date information



world date processing
TEST OS : XP

@echo off
set datex=%date%
for /f "usebackq tokens=3 delims= " %%f in (`reg query "HKCU\Control Panel\International" /v sShortDate`) do (
set var=%%f
)

for /f "usebackq tokens=1-3 delims=- " %%a in ('%datex%') do (
for /f "usebackq tokens=1-3 delims=- " %%g in ('%var%') do (
set xx%%g=%%a
set xx%%h=%%b
set xx%%i=%%c
)
)
set xx
echo %xxyyyy% %xxMM% %xxdd%%xxDD%
pause

mokomoji

unread,
Jul 8, 2013, 10:35:11 AM7/8/13
to
2013년 5월 6일 월요일 오전 1시 14분 18초 UTC+9, Harry Potter 님의 말:
> I want to create a batch file in WinNT to automatically back up a program folder to a progam back-up folder quickly and easily for the purpose of having a copy to which to revert after experimentation or significant code change. In order to do this, I need to both extract the system date and report it in YYYY-MM-DD format into an archive target name. How do I do these?

for /f "usebackq tokens=3 delims= " %%f in (`reg query "HKCU\Control Panel\International" /v sShortDate`) do (

delims= <---TAB
0 new messages