1

Intended software: windows command line processor (version 6.1.7601.17514)

Hi,

I've been trying to build a multiple-statement command line that runs within a short-cut. My goal is to be able to click one short-cut that checks if my hosted network is started or not, and then takes appropriate action based on the check. The code that starts and stops the hosted network is fine, and for the most part, the logic works, but I notice odd behavior when I check the outputs of the logic. I suspect that my problem has to do with the way I structured the statements, but I'm having difficulty properly interpreting the built-in documentation and the documentation I can find in the MSDN library. If it's possible, I want to avoid using batch files for this solution.

To keep things simple, I've substituted my lengthy "netsh" commands with "echo" commands that show the errorcode. The code below is what I'm using to test my logic:

Test Code

netsh wlan show hostednetwork | find "Not" && echo found %errorlevel% || echo lost %errorlevel%

Currently, the way I'm reading this is:

  1. Show me hostednetwork's status and send the output to input
  2. Attempt to find the string "Not" in the input
  3. If the attempt succeeds, output "found" and the errorcode to the screen
  4. If the attempt fails, then output "lost" and the errorcode to the screen

Notice that I'm not using any flags on the find command. I'm doing this because I want to reduce the chance of finding a false match. To clarify what I mean, I'll show the output if I just put in
netsh wlan show hostednetwork:

Sample Output of Hostednetwork Status

C:\Windows\system32>netsh wlan show hostednetwork

Hosted network settings
-----------------------
    Mode                   : Allowed
    SSID name              : "TestHost"
    Max number of clients  : 100
    Authentication         : WPA2-Personal
    Cipher                 : CCMP

Hosted network status
---------------------
    Status                 : Not started

If I search for the string "Not", then that's sufficient to tell me that the hosteadnetwork is not started, because when the hosteadnetwork is started, the output shows "Started".

The way I'm simulating the conditions of the hostednetwork is with the following commands:

netsh wlan start hostednetwork
netsh wlan stop hostednetwork

I expect that when I open a command prompt (as an administrator):

  1. If the hostednetwork is not started, I should see a "found 0" in the output, meaning that the string was found and that there were no errors.
  2. If the hostednetwork is started, I should see a "lost 1" in the output, meaning that the string was not found and that there was an error.

Case #1 works, but case #2 doesn't work on the first try. Here's my output when the hostednetwork is already started:

Output With Hostednetwork Started

C:\Windows\system32>netsh wlan start hostednetwork
The hosted network started.


C:\Windows\system32>netsh wlan show hostednetwork | find "Not" && echo found %er
rorlevel% || echo lost %errorlevel%
lost 0

C:\Windows\system32>netsh wlan show hostednetwork | find "Not" && echo found %er
rorlevel% || echo lost %errorlevel%
lost 1

Other Attempted Solutions

The way I've written the test code is the best I could come up with so far. In previous attempts, I've tried:

  1. Setting a custom variable instead of using the errorlevel variable, but I get the same output on case #2.
  2. Changing the code into an if else equivalent, but that didn't pan out very well.
  3. Wrapping the conditional statements in brackets "()" after the pipe and using different combinations of the special symbols "&" and "|".

Other Questions

This question is related to another that I've been trying to figure out. If I wanted to search for three different strings in a command's output and exit on a different error code for each string, how can I do this? The syntax below is my starting point:

myCommand [/options] | ((find "string1" && exit /b 2 || ver>nul) && 
(find "string2" && exit /b 3 || ver>nul) && (find "string3" && exit /b 4 || ver>nul))

For the same reasons above, I didn't use any flags on the "find" commands. Also, I used "ver>nul" in an attempt to keep the syntax correct since I know the "ver" operation succeeds.

Any assistance is appreciated.

4

2 回答 2

0

我不明白您为什么要避免使用批处理脚本。您的快捷方式可以简单地指向一个小批量脚本,生活会轻松得多。

但是可以做你想做的事。的值%errolevel%是在解析过程中确定的,整个快捷方式在一次解析中被解析,因此您可以获得在执行 FIND 命令之前存在的值。您需要延迟扩展!errorlevel!才能获得所需的结果。

批量使用setlocal enableDelayedExpansion,但这不适用于命令行(或快捷方式)。/V:ON相反,您必须使用该选项实例化一个额外的 CMD.EXE 。

netsh wlan show hostednetwork | cmd /v:on /c "find "Not" && echo found !errorlevel! || echo lost !errorlevel!"

有多个级别的引用正在进行,这有时会导致问题。如果您对特殊字符进行转义,则可以消除包含命令的引号。

netsh wlan show hostednetwork | cmd /v:on /c find "Not" ^&^& echo found !errorlevel! ^|^| echo lost !errorlevel!



关于你的第二个问题,我看到了 2 个问题。

1)我不明白设计用于退出不同错误代码的快捷方式的意义。你怎么可能利用返回的错误代码?

2) 您不能通过管道将内容传送到多个 FIND 命令中。第一个 FIND 命令将消耗所有内容并关闭管道,然后后续的 FIND 命令将无限期地等待来自键盘的内容。

您必须将命令输出重定向到临时文件,然后将每个 FIND 命令的输入重定向到临时文件。

于 2013-06-08T14:35:01.697 回答
0

您不能评估同一行中的变量。它需要延迟扩展和!errorlevel!要使用的。

在批处理文件中执行此操作,使用延迟扩展不会有问题。

于 2013-06-08T11:10:51.663 回答