wmic failure with concurrent calls on XP and Server 2003

ぐ巨炮叔叔 提交于 2019-12-20 07:15:19

问题


I am using wmic to get the time. I have narrowed this down to a one (1) line .bat file. I have learned from stackoverflow about piping stdin and stdout to avoid hangs.

C:> type t.bat
TYPE NUL | wmic os get localdatetime | findstr .

It seems that if I run wmic concurrently, it will occasionally fail. If I create two cmd shells and run the following command in each, errors similar to the one below appear.

for /l %i in (0,1,100) do (call t.bat)

...

C:> TYPE NUL | wmic os get localdatetime  | findstr .
Win32 Error: The process cannot access the file because it is being used by another process.

This does not appear to happen on Vista, 7, etc. I have only seen it happen on Windows XP SP3 and Windows Server 2003 SP2.

The "file" it is talking about may be TempWmicBatchFile.bat, but I do not know that for sure. I have not seen this file appear on the Windows Vista or Windows 7 machines.

Is there another way to get the current time? The %DATE% and %TIME% variables are formatted in the user's choice of representation, so I cannot use that. Any suggestions?


回答1:


I've never heard of the issue you are reporting.

You could try getTimeStamp.bat - a hybrid JScript/batch utility that provides robust, locale independent, date and time computation and formatting. The utility is pure script using native Windows functionality.

Assuming you have getTimeStamp.bat in your current directory, or better yet, somewhere in your path, then the following will create a variable named currentTimeStamp containing the current date and time in WMIC format.

call getTimeStamp -f {yyyy}{mm}{dd}{hh}{nn}{ss}.{fff}000{zzzz} -r currentTimeStamp

There are a great many formats available, as well as the ability to apply date and time offsets, etc. Full documentation is embedded within the script.




回答2:


This works, there is no need for the type nul pipe.

wmic os get localdatetime | findstr .


来源:https://stackoverflow.com/questions/21034354/wmic-failure-with-concurrent-calls-on-xp-and-server-2003

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!