one-Line Iso-Ish-Date Format Thing For CMD

I love CMD. I love batch files. I love FOR. I love being able to do things in one line, without resorting to VBScript or a custom EXE.

Today's task: make a unique folder for each day of metabase collection. I figured that getting a folder-creation-capable date format out of %date% and %time% should be reasonably easy somehow, so investigated non-FOR options but nothing really worked. Back to FOR it is!

I then figured that if you wanted to collect data more often than daily, you could probably add the time in without too much trouble, so did that too.

As a warning, this won't localize directly: Yank-To-ISO-ish format is %%k%%i%%j-%%l%%m , for example.

@echo off
for /f "tokens=2,3,4,5,6 delims=:./ " %%i in ("%date%.%time%") do SET isodate=%%k%%j%%i-%%l%%m

 

md c:\logs\%isodate%

REM now do whatever...
Copy C:\Windows\System32\inetsrv\History c:\logs\%isodate%

To play around with FOR commands on the command line, you don't need to double the % signs, so:

for /f "tokens=2,3,4,5,6 delims=:./ " %i in ("%date%.%time%") do @echo %k%j%i-%l%m

Again: Localization or customization is needed if you're in a non-Brit-style (day, month, year) locale, or if your short date doesn't look like mine. For reference (think I'm at Australian defaults):

C:\Tools>echo %date%
Thu 20/07/2006

C:\Tools>echo %time%
20:12:56.55

Um, that's all I had to share right now. Be fruitful and collect logs!

--

The Revised Efficientified Edition (Edits from (and thanks to) Kiliman)

Two lines, using SET statements, get a pretty reliable outcome. At first I was getting weird results, then realized that copy/paste had appended a space to a couple of the lines - if there's a space at the end of any SET statement, it can come back later to cause weirdness.

@echo off

rem Be careful to not leave a trailing space

rem at the end of any SET line below

 

rem -- for US Date Format
rem SET isodate=%DATE:~10%%DATE:~4,2%%DATE:~7,2%-%TIME:~0,2%%TIME:~3,2%

 

rem -- for AU/UK Date Format
SET isodate=%DATE:~10%%DATE:~7,2%%DATE:~4,2%-%TIME:~0,2%%TIME:~3,2%

rem replace space with 0
SET isodate=%isodate: =0%

md c:\logs\%isodate%

rem now do whatever...
Copy C:\Windows\System32\inetsrv\History c:\logs\%isodate%

Thanks to all that share(d) their expertise in the comments area.