TransWikia.com

How to separate command switches (/x) from arguments in windows?

Super User Asked by Herdsman on October 3, 2020

I know in linux, I can just write command -a -b - "arg1" "arg2"..., where single hyphen is "option/switch" separator. But how can I do similar in windows cmd?

I would like to see only particular option/switch from help:
c:> setx /? | findstr /r '/m', where the /m switch I want to find. but the findstr uses it as another switch:

FINDSTR: Bad command line.

How to achieve that?

One Answer

I want to find the /m switch

I'm using:

setx /? | findstr /r '/m'
  1. You do not need the /r switch (which specifies a regular expression) as '/m' is not a regular expression.

  2. You are using the wrong kind of quotes (single ' instead of "), it should be "/m"

  3. setx /? outputs /M so you either need to use /I (ignore case) or "/M"

  4. The / in "/m" needs to be escaped (using ), ie "/m", if you are not using the /C: (Use string as a literal search string) option

The following commands all do what you want:

setx /? | findstr "/M"
setx /? | findstr /c:"/M"
setx /? | findstr /i "/m"
setx /? | findstr /i /c:"/m"

Example output:

F:test>setx /? | findstr "/M"
    SETX [/S system [/U [domain]user [/P [password]]]] var value [/M]
    SETX [/S system [/U [domain]user [/P [password]]]] var /K regpath [/M]
         /F file {var {/A x,y | /R x,y string}[/M] | /X} [/D delimiters]
    /M                     Specifies that the variable should be set in
    SETX MACHINE "COMPAQ COMPUTER" /M
         NTCurrentVersionCurrentBuildNumber" /M
         CurrentVersionCurrentBuildNumber" /M

F:test>setx /? | findstr /c:"/M"
    SETX [/S system [/U [domain]user [/P [password]]]] var value [/M]
    SETX [/S system [/U [domain]user [/P [password]]]] var /K regpath [/M]
         /F file {var {/A x,y | /R x,y string}[/M] | /X} [/D delimiters]
    /M                     Specifies that the variable should be set in
    SETX MACHINE "COMPAQ COMPUTER" /M
         NTCurrentVersionCurrentBuildNumber" /M
         CurrentVersionCurrentBuildNumber" /M

F:test>setx /? | findstr /i "/m"
    SETX [/S system [/U [domain]user [/P [password]]]] var value [/M]
    SETX [/S system [/U [domain]user [/P [password]]]] var /K regpath [/M]
         /F file {var {/A x,y | /R x,y string}[/M] | /X} [/D delimiters]
    /M                     Specifies that the variable should be set in
    SETX MACHINE "COMPAQ COMPUTER" /M
         NTCurrentVersionCurrentBuildNumber" /M
         CurrentVersionCurrentBuildNumber" /M

F:test>setx /? | findstr /i /c:"/m"
    SETX [/S system [/U [domain]user [/P [password]]]] var value [/M]
    SETX [/S system [/U [domain]user [/P [password]]]] var /K regpath [/M]
         /F file {var {/A x,y | /R x,y string}[/M] | /X} [/D delimiters]
    /M                     Specifies that the variable should be set in
    SETX MACHINE "COMPAQ COMPUTER" /M
         NTCurrentVersionCurrentBuildNumber" /M
         CurrentVersionCurrentBuildNumber" /M

F:test>

Further Reading

Correct answer by DavidPostill on October 3, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP