Search code examples
windowsbatch-filewmic

wmic failure with concurrent calls on XP and Server 2003


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?


Solution

  • 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.