-2

I have gone through many similar questions such as the below, and cannot get a working output. I just need "YYYYMMDD-hhmmss" in a string. Below working is immediately ready to copy/paste into a command prompt and you can see the result. I do not know why there gaps/spaces between my variables in the final output (I thought there were trailing spaces, but I tried various techniques to strip those and none worked, there are no trailing spaces in the variables from what I can see). EDIT: I cannot use PowerShell (I would if I could, Gerhard correctly suggests that below but I am constrained by existing systems to batch).

Can someone advise how to create the expected string output of "YYYYMMDD-hhmmss" please?

Expected result: 20200716-100205

Actual result: 2020 07 16-10 02 05

Windows batch: formatted date into variable

:: for /f %x in ('wmic path win32_localtime get /format:list ^| findstr "="') do set %x
:: Inside a script use:
:: for /f %%x in ('wmic path win32_localtime get /format:list ^| findstr "="') do set %%x

set Year=2020
set Year=00%Year% && set Year=%Year:~-2% && echo %Year%
set Month=7
set Month=00%Month% && set Month=%Month:~-2% && echo %Month%
set Day=16
set Day=00%Day% && set Day=%Day:~-2% && echo %Day%
set Hour=10
set Hour=00%Day% && set Hour=%Hour:~-2% && echo %Hour%
set Minute=2
set Minute=00%Minute% && set Minute=%Minute:~-2% && echo %Minute%
set Second=5
set Second=00%Second% && set Second=%Second:~-2% && echo %Second%

set yyyymmddhhmmss=%Year%%Month%%Day%-%Hour%%Minute%%Second%
echo %yyyymmddhhmmss%
YorSubs
  • 1,438
  • 1
  • 12
  • 20
  • I do not want to set the date on the system (but I do want to *collect* the date from the system and use that to populate the variables). I want to collect "YYYYMMDD-hhmmss" into a variable, and then use that variable for another task (append it to a log filename). I found that I have to pad the numbers since they are often single digit as above. But I cannot get the final combined string to output without spaces between each variable. The for /f before the SET statements does collect from the system correctly. I will use that in my final script. What I put above is example output for ease. – YorSubs Jul 19 '20 at 08:19
  • using `cmd` or `batch-file`? – Gerhard Jul 19 '20 at 08:20
  • I'll put the final into a script (so replacing % by %% etc), but yeah, this is cmd and not old-old-old-school DOS :) (these will run on Windows 2012 servers so cmd compatible). – YorSubs Jul 19 '20 at 08:22
  • 1
    The reason for the spaces is: you included them: `set Minute=%Minute:~-2% && ...` sets %Minute% to `%Minute:~-2%`plus a space. Either write `set Minute=%Minute:~-2%&& ...` or preferably use safe syntax: `set "Minute=%Minute:~-2%" && ...` – Stephan Jul 19 '20 at 09:08
  • oh, of course, I forget how awful Batch is at times... and ok, "safe syntax", I did not know about that. Great, thanks. – YorSubs Jul 19 '20 at 09:12
  • 1
    Solution 1: `for /F "tokens=1-6 delims=/: " %%I in ('%SystemRoot%\System32\robocopy.exe "%SystemDrive%\|" . /NJH') do set "yyyymmddhhmmss=%%I%%J%%K-%%L%%M%%N" & goto HaveDateTime` with `:HaveDateTime` on next line. Solution 2: `for /F "tokens=2 delims==." %%I in ('%SystemRoot%\System32\wbem\wmic.exe OS GET LocalDateTime /VALUE') do set "yyyymmddhhmmss=%%I"` and `set "yyyymmddhhmmss=%yyyymmddhhmmss:~0,8%-%yyyymmddhhmmss:~8,6%"` on next line. Both solutions are explained in full detail in my answer on [Time is set incorrectly after midnight](https://stackoverflow.com/a/60126994/3074564). – Mofi Jul 19 '20 at 09:13
  • oh - and to use a variable after setting it in the same line, you'd need [delayed expansion](https://stackoverflow.com/questions/30282784/variables-are-not-behaving-as-expected/30284028#30284028) – Stephan Jul 19 '20 at 09:22

1 Answers1

1

This answer has been extended numerous times, so I am removing everything and giving a single solution.

@
for /f "tokens=2 delims=.=" %%i in ('wmic os get localdatetime /format:list') do set result=%%i
echo %result%
Gerhard
  • 18,114
  • 5
  • 20
  • 38
  • 1
    Attention: `wmic path win32_localtime` doesn't give leading zeros (great for doing math, but not so good for building a date/time string). Better use [wmic os get localdatetime](https://stackoverflow.com/questions/7727114/batch-command-date-and-time-in-file-name/18024049#18024049) – Stephan Jul 19 '20 at 09:04
  • Unfortunately, I can't use PowerShell (my preferred language of choice that I use for *all* Windows work) due to constraints with old Production scripts that I have to maintain (I dislike using batch very much). The padding still doesn't work though, do you have a solution for that? i.e. the output from your script just now was "2020719-95759", which is non sortable (Month "7" must be "07" and hour "9" must be "09" for it to be sortable). – YorSubs Jul 19 '20 at 09:05
  • 1
    ok, so that is simple, just test for month to see if it is less or equal to 9, if it is, add a 0. See edit. I am leaving the `powershell` part in the answer though as it might be helpful to future readers. – Gerhard Jul 19 '20 at 09:28
  • Really like the "leq" part, I didn't know this was possible. Thanks. – YorSubs Jul 19 '20 at 10:34
  • 1
    Yes. See `if /?` you can use various conditions. – Gerhard Jul 19 '20 at 10:40
  • Unfortunately, it does not work. :( `@echo off for /f %%x in ('wmic path win32_localtime get /format:list ^| findstr "="') do set %%x if "%month%" leq 9 set "month=0%month%" if "%day%" leq 9 set "day=0%day%" if "%hour%" leq 9 set "hour=0%hour%" if "%minute%" leq 9 set "minut=0%minute%" if "%second%" leq 9 set "second=0%second%" set "result=%year%%month%%day%%hour%%minute%%second%" echo %result%` result is: 2020070210747056 (which is all wrong, 056 instead of 56 seconds, 021 instead of 21 for the day). Can you think of a way to fix this? – YorSubs Jul 21 '20 at 06:50
  • 1
    scrap all this, I changed the answer. – Gerhard Jul 21 '20 at 07:22
  • ok, wow, that works. ah, I see that Stephan's point helped there. Thanks Gerhard, that's great, I think this is absolutely perfect for my needs, very useful. – YorSubs Jul 21 '20 at 07:30
  • @Stephan yeah, you're absolutely correct. I actually just amended OP's code, but `wmic os get localdatetime` makes it much easier for this purpose. – Gerhard Jul 21 '20 at 07:35