0

Recently my windows was backed with image restore software to previous version, so I had to install everything from scratch.

What really stresses me is that script that had worked before now doesn't. It's related to dates and datetime.

My colleague tried to run .bat file and he was able to get appropriate date format.

I tried to copy all the windows 10 date settings and still it doesn't work. Any ideas how to solve this? My output is:

C:\Software\scripts>for /F "usebackq tokens=1,2 delims==" %i in (`wmic os get LocalDateTime /VALUE 2>NUL`) do if '.%i.' == '.LocalDateTime.' set ldt=%j
C:\Software\scripts>set ldt=~0,4ldt:~4,2ldt:~6,2

The script:

for /F "usebackq tokens=1,2 delims==" %%i in (`wmic os get LocalDateTime /VALUE 2^>NUL`) do if '.%%i.'=='.LocalDateTime.' set ldt=%%j 
set ldt=%ldt:~0,4%-%ldt:~4,2%-%ldt:~6,2%
PAUSE
whatismyname123
  • 149
  • 1
  • 13
  • 1
    `) do if not defined ldt set ldt=%%j` – Gerhard Aug 25 '20 at 18:10
  • 1
    Just so we are perfectly clear, these are the only three lines in the script you are testing with? – Squashman Aug 25 '20 at 18:12
  • The first line of code needs the stderr redirection character escaped with `^` as is done in the second example. – lit Aug 25 '20 at 18:15
  • 1
    I recommend to use either the `wmic` solution (works also on Windows XP, but is slow) or the `robocopy` solution (requires Windows Vista or newer, but is fast) as posted and described in full details in my answer on [Time is set incorrectly after midnight](https://stackoverflow.com/a/60126994/3074564). After reading this long answer you should have also the knowledge why the currently used command lines produce not expected output and why the slightly different `wmic` solution posted by me always works as expected. – Mofi Aug 26 '20 at 05:54
  • Hello guys thanks everyone for help, especially Mofi, I understood where exactly is the problem. The thing is that wmic was not set as environment variable on my personal computer so I just used "C:\Windows\System32\wbem\wmic" as temporary solution and now it makes perfect sense. Although cmd didn't really report error well and it took me a lot of time to figure relative simple thing. – whatismyname123 Aug 26 '20 at 13:27
  • 1
    So there is missing `%SystemRoot%\System32\wbem` in your __system__ `Path` environment variable which is on all Windows since Windows 2000 by default the third path after `%SystemRoot%\System32;%SystemRoot%;`. That is indeed an interesting issue. Well, you know now why all my batch files posted on Stack Overflow contain full qualified file names for executables on which path and file extension are well known and can be specified in a batch file for being as independent as possible on often modified `PATH` and rarely modified `PATHEXT`. I have seen too often a corrupted `PATH`. – Mofi Aug 27 '20 at 05:35

0 Answers0