batch file - Set date with WMIC (Invalid Verb Switch) -
i have revised question comments below pointing me in new directions. tried date.exe fails set command in instances, it's returned timestamp.
so moved on wmic. however, cannot seem set date. believe may understanding of wmic. can spot errors in approach?
@echo off /f "tokens=2 delims==" %%a in ('wmic os localdatetime /value') set "dt=%%a" set "trail=%dt:~8%" & set "backdt=%dt:~0,8%" set "targetstamp=20150419%trail%" echo %targetstamp% targetstamp wmic os set localdatetime=%targetstamp% /f "tokens=2 delims==" %%a in ('wmic os localdatetime /value') set "ndt=%%a" set "newtrail=%ndt:~8%" echo %backdt&&newtrail% wmic os set localdatetime=%backdt%%newtrail% pause
ok, current time, replace date while keeping time , tz offset, set datetime, work right after wmic os set
command, , revert current time.
your code correct. note in order set date/time via wmic
need run command elevated privileges, in windows-start-"cmd"-right click-"run administrator".
edit:
no, see cmd.exe
leaves behind after treating unicode output of wmic
. needs heavy string pulling rid of '\r' in output.
running without errors on pc:
@echo off setlocal set dt= /f "tokens=*" %%a in ('wmic os localdatetime /value ^| findstr "."') @set dt=%%a set dt=%dt:~14,-1% set "trail=%dt:~8%" set "saveddt=%dt:~0,8%" set targetstamp=20150419%trail% wmic os set localdatetime=%targetstamp% rem work... pause set dt= /f "tokens=*" %%a in ('wmic os localdatetime /value ^| findstr "."') @set dt=%%a set dt=%dt:~14,-1% set "newtrail=%dt:~8%" wmic os set localdatetime=%saveddt%%newtrail%
running in privileged cmd reports having set new date. however, nothing changes on pc - wmic os set localdatetime
doesn't change date. ymmv.