<http://www.netikka.net/tsneti/info/tscmd001.htm>
Copyright © 2003- by Prof. Timo Salmi  
Last modified Tue 28-Feb-2017 11:09:17

 
Assorted NT/2000/XP/.. CMD.EXE Script Tricks
From the html version of the tscmd.zip 1cmdfaq.txt file
To the Description and the Index
 

This page is edited from the 1cmdfaq.txt faq-file contained in my tscmd.zip command line interface (CLI) collection. That zipped file has much additional material, including a number of detached .cmd script files. It is recommended that you also get the zipped version as a companion.

Please see "The Description and the Index page" for the conditions of usage and other such information.



1} How to get today's date elements into environment variables?

The undisputed mother of all Command Line Interface (CLI) cmd.exe questions. Asked in several formats, one of the most common being how can I augment the date (and even the time) to be a part of my file's (or folder's) name? This alternative question is practically equivalent since the best first step is to get the date elements into environment variables and then utilize those in the task. For renaming files with information added see e.g. Item #12.

The easiest and most portable solution across different country configurations is simply to call DATET.CMD which comes as a part the FAQ package tscmd.zip. That option utilizes Visual Basic scripting, but you do not need to concern yourself with any of the details. Running DATET.CMD will set for you the following environment variables
  DatetDD
  DatetMM
  DatetYY
  DatetYYYY
  DatetWkD
  DatetMon
  DatetHH
  DatetMI
  DatetSS
Note: If you run DATET.CMD from within another CMD.EXE script, do not forget to use CALL to invoke.

There is an alternative version DATEG.CMD in tscmd.zip which utilizes UnxUtils GnuAWK instead of Visual Basic scripting. Else than that in works exactly the same way.

But on to the actual FAQ:
  @echo off & setlocal enableextensions
  ::
  rem Method 1a:
  ::
  :: The date presentation assumes the order DD.MM.YYYY (else customize)
  :: mode con: codepage select=850
  ::
  for /f "tokens=1-3 delims=./-" %%f in ("%date%") do (
    set today_=%%h%%g%%f
    set dd_=%%f
    set mm_=%%g
    set yyyy_=%%h)
  echo %today_%
  echo dd_=%dd_% mm_=%mm_% yyyy_=%yyyy_%
  ::
  :: Clean up for the next test (superfluous because of setlocal/endlocal)
  for %%v in (today_ dd_ mm_ yyyy_) do set %%v=
  endlocal & goto :EOF

The output could be e.g.
  D:\TEST>cmdfaq
  20040316
  dd_=16 mm_=03 yyyy_=2004

A diversion: Contrary to the usual CMD line programming, a for loop is case-sensivive. Try out the following snippet to see the difference
  @echo off & setlocal enableextensions
  for /f %%g in ("C:\_D\TEST\CMDFAQ.CMD") do (
    echo %%g
    echo %%G)
  endlocal & goto :EOF

Back to the task at hand. Method 1b: A slight variation of Method 1a. Kok Yong Lee posed the following question in microsoft.public.win2000.cmdprompt.admin

"I was trying to setup a routine backup of my data with the day of backup as the name of file. For example
  wzzip 24_03_2006.zip Z:\data\*.*
Currently DATE /t on my machine return "24/03/2006"
I been messing about with the ideas of using the | in order to retrieve the results from "DATE /t" and then feed it to wzzip as input by to avail. Any one care to shed some light on this?"


I'll give the custom code. Assume that DATE /T gives 24.03.2006 The outcome can differ between locales. (For an advanced option on getting date format information see Item #165.) But if the format is that (or 24/03/2006) then
  @echo off & setlocal enableextensions
  :: Get the date elements, allow for three different separators
  for /f "tokens=1-3 delims=./-" %%f in ('date /t') do (
    set today_=%%h%%g%%f
    set dd_=%%f
    set mm_=%%g
    set yyyy_=%%h)
  ::
  :: Remove this method's potential trailing space from the year
  set yyyy_=%yyyy_:~0,4%
  ::
  :: An extra precaution which may be unnecessary
  :: See to it that days 1-9 will always have a leading zero
  set dd_=0%dd_%
  set dd_=%dd_:~-2%
  ::
  :: When you are sure, remove the echo precaution from the next line
  echo wzzip %dd_%_%mm_%_%yyyy_%.zip Z:\data\*.*
  endlocal & goto :EOF

Testing this script one will get
  C:\_D\TEST>cmdfaq
  wzzip 24_03_2006.zip Z:\data\*.*
Naturally, 20060324.zip would make more sense (sort better), but that is beside the current issue.

  @echo off & setlocal enableextensions
  rem Method 2:
  mode con: cp select=850>nul
  set dd_=%date:~0,2%
  set mm_=%date:~3,2%
  set yyyy_=%date:~6,4%
  echo dd_=%dd_% mm_=%mm_% yyyy_=%yyyy_%
  endlocal & goto :EOF

The output might be e.g.
  D:\TEST>cmdfaq
  dd_=16 mm_=11 yyyy_=2003
However, in the above option one assumes that the day and the month always are two digits long, i.e. a leading zero is present when necessary. (For more on this see the code of method 1b and the similar Item #16 about the time.) At the same time note the substring denotation syntax. It is %var_:~Start,Count% where the Start commences at 0, not the perhaps more customary 1. I.e. the index of the first character is 0.

If one wants to be hyper-careful, then
  @echo off & setlocal enableextensions
  rem Method 2b:
  mode con: cp select=850>nul
  set date_=%date%
  set dd_=%date_:~0,2%
  set mm_=%date_:~3,2%
  set yyyy_=%date_:~6,4%
  echo dd_=%dd_% mm_=%mm_% yyyy_=%yyyy_%
  endlocal & goto :EOF
Also see Item #11.

  @echo off & setlocal enableextensions
  rem Method 3:
  for /f "tokens=5-8 delims=.-/ " %%a in (
    'echo.^|date^|find "current"') do (
    set dd_=%%a
    set mm_=%%b
    set yyyy_=%%c
    )
  echo dd_=%dd_% mm_=%mm_% yyyy_=%yyyy_%
  ::
  for /f "tokens=5-8 delims=.-/: " %%a in (
    'echo.^|time^|find "current"') do (
    set hh_=%%a
    set mn_=%%b
    set ss_=%%c
    )
  echo hh_=%hh_% mn_=%mn_% ss_=%ss_%
  endlocal & goto :EOF

The output might be e.g.
  C:\_D\TEST>cmdfaq
  dd_=11 mm_=10 yyyy_=2006
  hh_=23 mn_=07 ss_=09

Octals are (confusingly) identified by a leading zero. Therefore consider carefully when wish to have the leading zero and when you might wish to drop it. For example, if you add at the end of the above code
  set /a ssplus1_=%ss_%+1
you'll get
  C:\_D\TEST>cmdfaq
  dd_=11 mm_=10 yyyy_=2006
  hh_=23 mn_=07 ss_=09
  Invalid number. Numeric constants are either decimal (17),
  hexadecimal (0x11), or octal (021).

  @echo off & setlocal enableextensions
  rem
  rem Method 4: (with time)
  rem
  rem Assuming locale dependent date format DD.MM.YYYY
  rem 24-hour time format H:MN:SS
  rem Else customize
  set date_=%date%
  set time_=%time%
  set yyyy_=%date_:~6,4%
  set mm_=%date_:~3,2%
  set dd_=%date_:~0,2%
  set mn_=%time_:~3,2%
  set hh_=%time_:~0,2%
  set hh_=0%hh_: =%
  set hh_=%hh_:~-2%
  echo %yyyy_%%mm_%%dd_%%hh_%%mn_%
  endlocal & goto :EOF

The output might be e.g.
  C:\_D\TEST>cmdfaq
  200803232042

The problem can also be solved with a Visual Basic Script (VBScript) aided command line script. It is much more complicated, but it has the advantage of being more stable, i.e. less region dependent than the pure CMD.EXE solutions. With addition to the date elements also the time elements are included for demonstration.
  @echo off & setlocal enableextensions
  ::
  :: Make a temporary folder
  if not exist c:\mytemp mkdir c:\mytemp
  ::
  :: Build a Visual Basic Script (the skip variable prevents the findstr row finding itself)
  set skip=
  findstr "'%skip%VBS" "%~f0" > c:\mytemp\tmp$$$.vbs
  :: Run it with Microsoft Windows Script Host Version 5.6
  cscript //nologo c:\mytemp\tmp$$$.vbs
  ::
  :: Call the command line script which the script host built
  call c:\mytemp\tmp$$$.cmd
  ::
  :: Clean up
  for %%f in (c:\mytemp\tmp$$$.vbs c:\mytemp\tmp$$$.cmd) do (
    if exist %%f del %%f)
  rmdir c:\mytemp
  ::
  :: Show the results
  echo Date by %%date%% %date%
  echo Time by %%time%% %time%
  echo dd_=%dd_%
  echo mm_=%mm_%
  echo yyyy_=%yyyy_%
  echo hh_=%hh_%
  echo mi_=%mi_%
  echo ss_=%ss_%
  endlocal & goto :EOF
  '
  'The Visual Basic Script
  Const ForReading = 1, ForWriting = 2, ForAppending = 8 'VBS
  Dim DateNow, TimeNow, fso, f 'VBS
  DateNow = Date 'VBS
  TimeNow = Time 'VBS
  Set fso = CreateObject("Scripting.FileSystemObject") 'VBS
  Set f = fso.OpenTextFile("c:\mytemp\tmp$$$.cmd", ForWriting, True) 'VBS
  f.WriteLine "@set dd_=" & Right(0 & DatePart("d", DateNow), 2) 'VBS
  f.WriteLine "@set mm_=" & Right(0 & DatePart("m", DateNow), 2) 'VBS
  f.WriteLine "@set yyyy_=" & DatePart("yyyy", DateNow) 'VBS
  f.WriteLine "@set hh_=" & Right(0 & DatePart("h", TimeNow), 2) 'VBS
  f.WriteLine "@set mi_=" & Right(0 & DatePart("n", TimeNow), 2) 'VBS
  f.WriteLine "@set ss_=" & Right(0 & DatePart("s", TimeNow), 2) 'VBS
  f.Close 'VBS

An example of the output
  D:\TEST>cmdfaq
  Date by %date% 16.03.2004
  Time by %time% 8:27:15.47
  dd_=16
  mm_=03
  yyyy_=2004
  hh_=08
  mi_=27
  ss_=15

In the above "Whenever an expression is not a string, it is converted to a String subtype". So we could as well have e.g.
Right("0" & DatePart("d", DateNow), 2)

A more concise presentation
  @echo off & setlocal enableextensions
  set vbs_=%temp%\tmp.vbs
  echo FixedNow=Now>"%vbs_%"
  echo WScript.Echo Right(0 ^& Day(FixedNow), 2) ^&" "^& ^
    Right(0 ^& Month(FixedNow), 2) ^&" "^& ^
    Year(FixedNow) ^&" "^& ^
    Right(0 ^& Hour(FixedNow), 2) ^&" "^& ^
    Right(0 ^& Minute(FixedNow), 2) ^&" "^& ^
    Right(0 ^& Second(FixedNow), 2) >> "%vbs_%"
  cscript //nologo "%vbs_%"
  for %%f in ("%vbs_%") do if exist %%f del %%f
  endlocal & goto :EOF

The output might be e.g.
  C:\_D\TEST>cmdfaq
  22 10 2006 12 57 26

Another way of writing a VBScript solution
  @echo off & setlocal enableextensions
  >"%temp%\tmp$$$.vbs" echo WScript.Echo Day(Date)
  for /f %%a in ('cscript //nologo "%temp%\tmp$$$.vbs"') do set dd_=%%a
  >"%temp%\tmp$$$.vbs" echo WScript.Echo Right(0 ^& DatePart("d", Date), 2)
  for /f %%a in ('cscript //nologo "%temp%\tmp$$$.vbs"') do set dd_=%%a
  >"%temp%\tmp$$$.vbs" echo WScript.Echo Right(0 ^& DatePart("m", Date), 2)
  for /f %%a in ('cscript //nologo "%temp%\tmp$$$.vbs"') do set mm_=%%a
  >"%temp%\tmp$$$.vbs" echo WScript.Echo DatePart("yyyy", Date)
  for /f %%a in ('cscript //nologo "%temp%\tmp$$$.vbs"') do set yyyy_=%%a
  ::
  >"%temp%\tmp$$$.vbs" echo WScript.Echo Right(0 ^& DatePart("h", Time), 2)
  for /f %%a in ('cscript //nologo "%temp%\tmp$$$.vbs"') do set hh_=%%a
  >"%temp%\tmp$$$.vbs" echo WScript.Echo Right(0 ^& DatePart("n", Time), 2)
  for /f %%a in ('cscript //nologo "%temp%\tmp$$$.vbs"') do set mi_=%%a
  >"%temp%\tmp$$$.vbs" echo WScript.Echo Right(0 ^& DatePart("s", Time), 2)
  for /f %%a in ('cscript //nologo "%temp%\tmp$$$.vbs"') do set ss_=%%a
  ::
  for %%f in ("%temp%\tmp$$$.vbs") do if exist %%f del %%f
  ::
  :: Demonstrate the result
  echo dd_=%dd_%
  echo mm_=%mm_%
  echo yyyy_=%yyyy_%
  echo hh_=%hh_%
  echo mi_=%mi_%
  echo ss_=%ss_%
  endlocal & goto :EOF

The output could be e.g.
  C:\_D\TEST>cmdfaq
  dd_=03
  mm_=04
  yyyy_=2008
  hh_=19
  mi_=21
  ss_=07

There is still another Visual Basic Script (VBScript) aided command line solution formulation in Item #10 to demonstrate command scripting modularity and FOR loops which you should take a look at.

Q: Ok, fine and dandy. But how do I get the day of the week into an environment variable?

This can be solved e.g. with G(nu)AWK as follows
  @echo off
  for /f %%d in ('gawk "BEGIN{printf\"%%s\n\",strftime(\"%%A\")}"') do (
    set wd_=%%d)
  echo %wd_%

The output might be e.g.
  Sunday
However, there is a complication. The above will only work on 32-bit Windows, not on a 64-bit! (For testing your current bit status in a script file, see #Item 187.) You have to get a 32-bit G(nu)AWK. And let's rename it to unxgawk.exe for distinction (as we do all throughout this FAQ collection). The solution below will work both for 32 and 64 bits.
  @echo off
  for /f "usebackq" %%d in (`unxgawk "BEGIN{printf\"%%s\n\",strftime(\"%%A\")}"`) do (
    set wd_=%%d)
  echo Today is %wd_%
  goto :EOF
(The useful usebackq-trick is used here for demonstration, only. It is not required in this particular solution.)

Q: Ok, but how about the number of the week?

It is up to seeing a G(nu)AWK documentation. In this case the formatting argument is "V" instead of the "A". For better backwards compatibility, let's use an alternative trick for putting the value into the environment variable.
  gawk 'BEGIN{printf"@set wk_=%%s\n",strftime("%%V")}'>"%temp%\tmp$$$.bat"
  for %%c in (call del) do %%c "%temp%\tmp$$$.bat"
  echo The week number is = %wk_%
  set wk_=
Note that there are different week number options and standards. See Item #70. Also see Item #149 for code alternatives. To add to the confusion, the "unxgawk" solution
  @echo off
  unxgawk "BEGIN{printf\"@set wk_=%%s\n\",strftime(\"%%W\")}">"%temp%\tmp$$$.bat"
  for %%c in (call del) do %%c "%temp%\tmp$$$.bat"
  set /a wk_+=1
  echo The week number is = %wk_%
  set wk_=

Q: Ok, but how about the number of the day (1-366)?

Using VBScript
  @echo off & setlocal enableextensions
  :: Make a temporary folder
  if not exist c:\mytemp mkdir c:\mytemp
  :: Build a Visual Basic Script
  findstr "'%skip%VBS" "%~f0" > c:\mytemp\tmp$$$.vbs
  :: Run it with Microsoft Windows Script Host Version 5.6
  cscript //nologo c:\mytemp\tmp$$$.vbs
  :: Call the command line script the script host built
  call c:\mytemp\tmp$$$.cmd
  :: Clean up
  for %%f in (c:\mytemp\tmp$$$.vbs c:\mytemp\tmp$$$.cmd) do if exist %%f del %%f
  rmdir c:\mytemp
  :: Show the result
  echo Day Number dn_=%dn_%
  endlocal & goto :EOF
  '
  'The Visual Basic Script
  Const ForReading = 1, ForWriting = 2, ForAppending = 8 'VBS
  Dim DateNow, fso, f 'VBS
  DateNow = Date 'VBS
  Set fso = CreateObject("Scripting.FileSystemObject") 'VBS
  Set f = fso.OpenTextFile("c:\mytemp\tmp$$$.cmd", ForWriting, True) 'VBS
  f.Write "@set dn_=" & DatePart("y", DateNow) 'VBS
  f.Close 'VBS

Alternatively, much more concisely
  @echo off & setlocal enableextensions
  if not exist c:\mytemp mkdir c:\mytemp
  echo>c:\mytemp\tmp$$$.vbs WScript.Echo DatePart("y", Date)
  for /f %%n in ('cscript //nologo c:\mytemp\tmp$$$.vbs') do set dn_=%%n
  for %%f in (c:\mytemp\tmp$$$.vbs) do if exist %%f del %%f
  rmdir c:\mytemp
  echo Day Number dn_=%dn_%
  endlocal & goto :EOF

An output example:
  C:\_D\TEST>echo %date%
  23.05.2006
  C:\_D\TEST>cmdfaq
  Day Number dn_=143

Consider an application needing today's date. Say we wish to find all the files made today in a folder:
  @echo off & setlocal enableextensions enabledelayedexpansion
  for %%f in ("c:\mytest\*.*") do (
    echo "%%~tf" | find "%date%" > nul
    if !errorlevel! EQU 0 echo %%~tf %%~zf "%%~ff"
    )
  endlocal & goto :EOF

Incidentally, for demonstration, that can be written with an alternative syntax, not needing the enabledelayedexpansion
  @echo off & setlocal enableextensions
  for %%f in ("d:\bas\*.*") do (
    echo "%%~tf" | find "%date%" > nul && echo %%~tf %%~zf "%%~ff"
    )
  endlocal & goto :EOF
or, finding the files NOT made today
  @echo off & setlocal enableextensions
  for %%f in ("c:\mytest\*.*") do (
    echo "%%~tf" | find "%date%" > nul || echo %%~tf %%~zf "%%~ff"
    )
  endlocal & goto :EOF
For more on the conditional processing symbol | | see (give the command):
  hh ntcmds.chm::/ntcmds_shelloverview.htm
  (Dropped after XP but, you can copy the following files from your old XP system: ntcmds.chm ntdef.chm Uabrand.gif
  or visit Microsoft's Command shell overview.)


Back to getting the date and time. An advanced script method. Note, however, that the WMIC (Windows Management Instrumentation Command-Line Utility) is only available since XP, and might require special privileges.
  @echo off & setlocal enableextensions
  for /f "tokens=* skip=1" %%i in ('wmic OS Get LocalDateTime') do (
    set LocalDateTime=%%i)
  echo LocalDateTime: %LocalDateTime%
  ::
  set year_=%LocalDateTime:~0,4%
  set month_=%LocalDateTime:~4,2%
  set day_=%LocalDateTime:~6,2%
  set hour_=%LocalDateTime:~8,2%
  set minute_=%LocalDateTime:~10,2%
  set second_=%LocalDateTime:~12,2%
  set sec100_=%LocalDateTime:~15,3%
  set tz_=%LocalDateTime:~21,5%
  set tz_=%tz_: =%
  ::
  echo year_ %year_%
  echo month_ %month_%
  echo day_ %day_%
  echo hour_ %hour_%
  echo minute_ %minute_%
  echo second_ %second_%
  echo sec100_ %sec100_%
  echo tz_ %tz_%
  endlocal & goto :EOF

The output might be e.g.
  C:\_D\TEST>cmdfaq
  LocalDateTime: 20060806213955.468000+180
  year_ 2006
  month_ 08
  day_ 06
  hour_ 21
  minute_ 39
  second_ 55
  sec100_ 468
  tz_ +180.

A reasonably locale-independent date elements' extraction with XP cmd.exe with no third-party utilities involved:
  @echo off & setlocal enableextensions
  ::
  :: Still debugging?
  set debug=true
  ::
  :: Get the date from the first line of the date command
  for /f "tokens=2,5 delims= " %%a in ('echo.^|date') do (
    if not defined date_ set lang_=%%a
    if not defined date_ set date_=%%b
    )
  if defined debug echo %lang_%
  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_%
  ::
  :: Local, but not that local that it is not English
  if not "%lang_%"=="current" (
    echo Error: The OS is not in English
    goto :EOF
    )
  ::
  :: 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%
  if defined debug 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: Unidentified 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

If one has a UNIX-port date (let's call it unxdate for distinction) we can have
  @echo off & setlocal enableextensions
  for /f %%a in ('unxdate "+%%Y%%m%%d"') do (
    set date_=%%a)
  echo date_=%date_%
  for /f %%a in ('unxdate "+%%H%%M%%S"') do (
    set time_=%%a)
  echo time_=%time_%
  for /f %%a in ('unxdate "+%%Y%%m%%d%%H%%M%%S"') do (
    set datetime_=%%a)
  echo datetime_=%datetime_%
  endlocal & goto :EOF

The output could be e.g.
  C:\_D\TEST>cmdfaq
  date_=20081007
  time_=125955
  datetime_=20081007125955

There is another Visual Basic Script (VBScript) aided command line script solution based on the List the Local Time code in the Microsoft TechNet Script Repository. It requires at least XP.
  @echo off & setlocal enableextensions
  ::
  :: Build a Visual Basic Script and run it
  set vbs_=%temp%\tmp$$$.vbs
  set skip=
  findstr "'%skip%VBS" "%~f0" > "%vbs_%"
  for /f "tokens=1-6 delims= " %%a in ('cscript //nologo "%vbs_%"') do (
    set yyyy_=%%a
    set mm_=%%b
    set dd_=%%c
    set hh_=%%d
    set mn_=%%e
    set ss_=%%f)
  ::
  :: Clean up
  for %%f in ("%vbs_%") do if exist %%f del %%f
  ::
  echo yyyy_=%yyyy_%
  ::
  set mm_=0%mm_%
  set mm_=%mm_:~-2%
  echo mm_=%mm_%
  ::
  set dd_=0%dd_%
  set dd_=%dd_:~-2%
  echo dd_=%dd_%
  ::
  set hh_=0%hh_%
  set hh_=%hh_:~-2%
  echo hh_=%hh_%
  ::
  set mn_=0%mn_%
  set mn_=%mn_:~-2%
  echo mn_=%mn_%
  ::
  set ss_=0%ss_%
  set ss_=%ss_:~-2%
  echo ss_=%ss_%
  ::
  endlocal & goto :EOF
  '
  'The Visual Basic Script 'VBS
  Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2") 'VBS
  Set colItems = objWMIService.ExecQuery("Select * from Win32_LocalTime") 'VBS
  For Each objItem in colItems 'VBS
    WScript.StdOut.Write     objItem.Year &   " " 'VBS
    WScript.StdOut.Write     objItem.Month &  " " 'VBS
    Wscript.StdOut.Write     objItem.Day &    " " 'VBS
    Wscript.StdOut.Write     objItem.Hour &   " " 'VBS
    Wscript.StdOut.Write     objItem.Minute & " " 'VBS
    Wscript.StdOut.WriteLine objItem.Second 'VBS
  Next 'VBS

The output could be e.g.
  C:\_M>C:\_D\TEST\CMDFAQ.CMD
  yyyy_=2009
  mm_=04
  dd_=12
  hh_=07
  mn_=16
  ss_=12

Using JavaScript in WSH
  @echo off & setlocal enableextensions
  >  "%temp%\tmp.js" echo var D = new Date();
  >> "%temp%\tmp.js" echo D = (D.getFullYear()*100+D.getMonth()+1)*100+D.getDate();
  >> "%temp%\tmp.js" echo WScript.Echo(D);
  for /f %%a in ('cscript //nologo "%temp%\tmp.js"') do set YYYYMMDD=%%a
  for %%f in ("%temp%\tmp.js") do if exist %%f del %%f
  echo %YYYYMMDD%
  endlocal & goto :EOF

The output could be e.g.
  C:\_M>C:\_D\TEST\CMDFAQ.CMD
  20091230

The date extraction task can be soved in so many ways. If you can get hold of QBASIC
  @echo off & setlocal enableextensions
  if defined ProgramW6432 (
    echo/
    echo Exiting: %~f0 is incompatible with 64-bit Windows
    goto :EOF)
  > tmp$$$.bas echo OPEN "tmp$$$.bat" FOR OUTPUT AS #1
  >>tmp$$$.bas echo PRINT #1, "@set date_=";
  >>tmp$$$.bas echo PRINT #1, MID$(DATE$,7,4);MID$(DATE$,1,2);MID$(DATE$,4,2)
  >>tmp$$$.bas echo CLOSE #1
  >>tmp$$$.bas echo SYSTEM
  ::
  qbasic /run tmp$$$.bas
  call tmp$$$.bat
  for %%f in (tmp$$$.bas tmp$$$.bat) do if exist %%f del %%f
  ::
  echo %date_%
  ::
  endlocal & goto :EOF

How to convert date information into one's own language? An example can be found in this FAQ e.g. in Item 300.

References/Comments: (If a Google message link fails try the links within the brackets.)
  Google Groups Mar 2 2004, 8:13 pm [M] [T]
  Google Groups Feb 15 2006, 1:10 am [M]
  Google Groups Oct 21 2006, 9:26 pm [M] [T]
  A Description of the Windows Management Instrumentation (WMI) Command-Line Utility (Wmic.exe)
  Microsoft Technet Script Center: Script Repository: Operating System: Dates and Times
  How to automatically include the current date in a batch created file
  Google Groups Jan 29 2007, 6:42 pm [M] [T]
  Google Groups Jan 30 2007, 9:23 am [M] [T]
  Google Groups Feb 20 2008, 6:40 pm [M] [T]
  Date and Time Formats Dr J R Stockton
  Google Groups 5 Oct 2008 23:00:50 +0100 [M] [T]
  Google Groups Dec 29 2009, 7:20 pm [M] [T]
  Google Groups Jun 12 2011, 9:58 pm
  Stack Overflow: How to get current datetime on windows command line, in a suitable format for using in a filename?