Why does the command “Timeout” in a batch file sud

2019-06-16 21:13发布

Did I miss a change of the Windows internal "timeout" command?

I have a batch file for a long time now that shows me the version number of a program. I added the timeout command to keep the CMD window opened for a few seconds. So my batch file now looks like this:

context -version
timeout 7

This worked fine but since some Windows update (obviously), the CMD windows closes directly as if the timeout command doesn't work anymore. When I start the file from a CMD window I get a message:

D:\CTX>timeout 7
Try 'timeout --help' for more information.

The file is as it was since I created it, but the behavior is new to me.

So can anybody tell me what I is going wrong here?

2条回答
beautiful°
2楼-- · 2019-06-16 21:53

TIMEOUT is not an internal command of cmd.exe (Windows command interpreter) like FOR or DIR.

It is an external command which means a console application being located in the Windows directory %SystemRoot%\System32.

On using just timeout without file extension and without full path, Windows command interpreter searches first in current directory with timeout.* for a file having a file extension listed in environment variable PATHEXT.

If no such file can be found in current directory, Windows command interpreter continues the search for timeout.* with a file extension listed in PATHEXT in the directories defined in environment variable PATH.

On Windows Vista and later versions of Windows the system PATH is defined with:

%SystemRoot%\system32;%SystemRoot%\system32;%SystemRoot%\System32\Wbem;%SYSTEMROOT%\System32\WindowsPowerShell\v1.0\;

For that reason it is usually found first %SystemRoot%\System32\timeout.exe on using just timeout in a batch file.

But if the current directory contains a file with name timeout.* having a file extension listed also in environment variable PATHEXT, or PATH was redefined locally or system wide by an installer which put other directory paths at beginning instead of appending them at end and one of these directories contain also a timeout.* file, Windows command interpreter runs this executable or script.

The suggestions for solving this problem:

  1. Use in the batch file %SystemRoot%\System32\timeout.exe because then Windows command interpreter must not search for this executable and it can't happen that the wrong timeout is executed mistakenly, except environment variable SystemRoot is modified locally which is very unlikely in comparison to PATH manipulations.

  2. Open Windows Control Panel - System - Advanced system settings (blue link on left side), select tab Advanced, click on button Environment Variables..., search in lower half in list of System variables for Path, select this environment variable, click on Edit, and move all directory paths left or above (depends on Windows version) of %SystemRoot%\system32 to end of the directories list.

  3. Please report to the author of the application or software bundle which modified system PATH and inserted their directory paths at beginning instead of appending them at end about this wrong modification of Windows system PATH.

As Magoo wrote already: a timeout.* ported from Unix to Windows is obviously executed on your Windows machine because of the output help statement. To get help on a command on Windows the command has to be executed usually with parameter /? and not with -h or --help as on Unix.

查看更多
祖国的老花朵
3楼-- · 2019-06-16 22:03

I believe you've probably acquired a different version of timeout since the help prompt --help is a \*nixy style option-specifier.

Try, from the prompt

where /T timeout.*

which should list the timeout versions on the path and may assist in locating the rogue version.

查看更多
登录 后发表回答