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%