0phoi5 Posted January 7, 2016 Share Posted January 7, 2016 (edited) Hi all, I'm using... wmic /node:%Hostname% os get lastbootuptime ... which gives the output as a WMIDateTime (Last time a PC was rebooted)... 20160104102930 Is there a way for me to convert this output to a nicely formatted date/time from within the batch file? In this case, I'd like the output to be something like 04/01/2016 10:29 I've had a look around the net, but couldn't find what I needed. Surely batch can do this, without having to rely on Javascript/Powershell? Thank you. Edited January 7, 2016 by haze1434 Quote Link to comment Share on other sites More sharing options...
Jason Cooper Posted January 7, 2016 Share Posted January 7, 2016 If you've got your program output from wmic in an environment variable (%X% in the example) then you can use something like: set lastBootUpTime="%X:~06,2%/%X:~4,2%/%X:~0,4% %X:~8,2%:%X:~10,2%" Quote Link to comment Share on other sites More sharing options...
cooper Posted January 7, 2016 Share Posted January 7, 2016 I didn't know you could do that with variables. That's pretty cool. Quote Link to comment Share on other sites More sharing options...
0phoi5 Posted January 7, 2016 Author Share Posted January 7, 2016 Thanks Jason Is there any way to get this to work with... for /f "skip=1 tokens=1 delims=. " %%A in ('wmic /node:%Hostname% os get lastbootuptime') do if not defined BootTime set BootTime=%%A Quote Link to comment Share on other sites More sharing options...
Jason Cooper Posted January 7, 2016 Share Posted January 7, 2016 Try something like for /f "skip=1 tokens=2 delims==." %%A in ('wmic os get lastbootuptime /format:value') do set BootTime=%%A Quote Link to comment Share on other sites More sharing options...
0phoi5 Posted January 11, 2016 Author Share Posted January 11, 2016 Try something likefor /f "skip=1 tokens=2 delims==." %%A in ('wmic os get lastbootuptime /format:value') do set BootTime=%%A I'm afraid this didn't work. The output is still 20160104102930 My code at the moment is... for /f "skip=1 tokens=1 delims=. " %%A in ('wmic /node:%Hostname% os get lastbootuptime') do if not defined BootTime set BootTime=%%A I have also tried... for /f "skip=1 tokens=2 delims==." %%A in ('wmic os get lastbootuptime /format:value') do set BootTime=%%A ... but %BootTime% still echos 20160104102930 I also tried converting it using... set BOOTyear=%BootTime:~-4,4% set BOOTmonth=%BootTime:~-8,2% set BOOTday=%BootTime:~0,2% set BootTimeFin=%BOOTyear%%BOOTmonth%%BOOTday% ...but %BootTimeFin% randomly echos as 29300420 (absolutely no idea where these numbers are coming from!) Quote Link to comment Share on other sites More sharing options...
Jason Cooper Posted January 11, 2016 Share Posted January 11, 2016 The line for /f "skip=1 tokens=2 delims==." %%A in ('wmic os get lastbootuptime /format:value') do set BootTime=%%A is just to put the output of the wmic command into the BootTime environment variable, so the value of 20160104102930 is correct. You then nee to parse it. When you do set BOOTyear=%BootTime:~-4,4% set BOOTmonth=%BootTime:~-8,2% set BOOTday=%BootTime:~0,2% set BootTimeFin=%BOOTyear%%BOOTmonth%%BOOTday% you are setting BOOTyear to be the last 4 characters (2930) as you are asking it to start 4 characters from the end of the string and take the next 4 characters. You would want the -4 to be 0 to get the correct year. You are making the same sort of mistakes with BOOTmonth and BOOTday. Try converting it with the following set BOOTyear=%BootTime:~0,4% set BOOTmonth=%BootTime:~4,2% set BOOTday=%BootTime:~6,2% set BootTimeFin=%BOOTyear%%BOOTmonth%%BOOTday% If you want to get a better understanding of how substringing environment varaibles works in batch then try reading http://ss64.com/nt/syntax-substring.html Quote Link to comment Share on other sites More sharing options...
0phoi5 Posted January 11, 2016 Author Share Posted January 11, 2016 Thanks Jason. I still have much to learn, I see! I've managed to get delims, tokens, skip and FOR/DO down, looks like it's time to make sure I understand substrings work before moving on. Much appreciated :) Quote Link to comment Share on other sites More sharing options...
0phoi5 Posted January 12, 2016 Author Share Posted January 12, 2016 Perfect for /f "skip=1 tokens=1 delims=. " %%A in ('wmic /node:%Hostname% os get lastbootuptime') do if not defined BootTime set BootTime=%%A set BOOTyear=%BootTime:~0,4% set BOOTmonth=%BootTime:~4,2% set BOOTday=%BootTime:~6,2% set BOOThour=%BootTime:~8,2% set BOOTminute=%BootTime:~10,2% set "BootTime=%BOOTday%/%BOOTmonth%/%BOOTyear% at %BOOThour%:%BOOTminute%" Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.