How to handle a comma in a filename with WMIC?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
npocmaka_
Posts: 516
Joined: 24 Jun 2013 17:10
Location: Bulgaria
Contact:

Re: How to handle a comma in a filename with WMIC?

#31 Post by npocmaka_ » 31 Jan 2014 16:48

Sponge Belly wrote:Thanks for the pointers, npocmaka. :-)

But this thread on a Russian forum proved to be instructive. Retouching the script I found there, I ended up with:

Code: Select all

set ^"js=javascript:close(new ActiveXObject('Scripting.^
FileSystemObject'^).GetStandardStream(1^).Write(new String(new ^
Enumerator(new ActiveXObject('WbemScripting.SWbemLocator'^).^
ConnectServer('.','root\\cimv2'^).ExecQuery('Select LocalDateTime ^
from Win32_OperatingSystem where Primary=True'^)^).item(^).^
LocalDateTime^)^)^);^"

for /f %%t in ('mshta "%js%"') do echo(WMI LocalDateTime: %%t


Long-winded, but it works! And it should work for everyone, even any poor Windows XP Home bunnies still left out there. ;-)

- SB


Yes.There's no big difference when you use WMI classes through the activex objects?But after I saw this code started to wonder if it is necessary to create WMI objects to the time? Both javascript and vbscript have a good set date/time functions. According to me these classes are most interesting for enhancements of batch capabilities

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: How to handle a comma in a filename with WMIC?

#32 Post by foxidrive » 31 Jan 2014 19:15

Sponge Belly wrote:

Code: Select all

set ^"js=javascript:close(new ActiveXObject('Scripting.^
FileSystemObject'^).GetStandardStream(1^).Write(new String(new ^
Enumerator(new ActiveXObject('WbemScripting.SWbemLocator'^).^
ConnectServer('.','root\\cimv2'^).ExecQuery('Select LocalDateTime ^
from Win32_OperatingSystem where Primary=True'^)^).item(^).^
LocalDateTime^)^)^);^"

for /f %%t in ('mshta "%js%"') do echo(WMI LocalDateTime: %%t


Long-winded, but it works! And it should work for everyone, even any poor Windows XP Home bunnies still left out there. ;-)



It even returns the date time string in Windows 2000 Pro.

Sponge Belly
Posts: 231
Joined: 01 Oct 2012 13:32
Location: Ireland
Contact:

Re: How to handle a comma in a filename with WMIC?

#33 Post by Sponge Belly » 03 Feb 2014 09:48

npocmaka wrote:Yes.There's no big difference when you use WMI classes through the activex objects?But after I saw this code started to wonder if it is necessary to create WMI objects to the time? Both javascript and vbscript have a good set date/time functions.


True, but look at the output from this little snippet:

Code: Select all

for /f "delims=" %%d in ('mshta ^"javascript:close(^
new ActiveXObject('Scripting.FileSystemObject'^).^
GetStandardStream(1^).Write(new Date(^)^)^);^"') ^
do echo(%%d

Mon Feb 3 12:58:56 UTC 2014


Mon? Feb? Not exactly locale-independent. You can use GetYear(), GetMonth() and friends to access unambiguous numeric parts of the date, but why bother when there’s WMI’s numeric timestamp?

Code: Select all

set ldt=For Each objItem in GetObject(""winMgmts:\\.\root\cimv2""^).^
ExecQuery(""Select LocalDateTime from Win32_OperatingSystem ^
where Primary=True""^):CreateObject(""Scripting.FileSystemObject""^).^
GetStandardStream(1^).Write objItem.LocalDateTime:Next:Close

for /f %%l in ('mshta vbscript:execute("%ldt%"^)') ^
do echo(%%l

20140203152208.013000+000


It’s trivial to chop up this string into year, month, day and time. And you can even calculate the weekday using the functions posted by Aacini in How Do I State that a Variable Is a Date? Best of all, the above code should work across all versions of Windows from 2000 Pro (according to Foxi) onwards regardless of locale.

- SB

PS: See also updated LastModified code snippet from several posts back.

Post Reply