4

Whenever I type an (, i.e ,an opening parenthesis in the command Prompt, this happens:

(
More? 

If I continue typing ( or any other character, it prompts me with the same message. This is stopped, if I enter ), i.e, the closing parenthesis, although I do get an error, which says :

) was unexpected at this time

Other than that, I couldn't find what this command does. Any ideas what it does ?

  • 2
    Parenthesis can be used to split commands across multiple lines, but maybe someone else can give you a more complete answer. – User552853 Mar 21 '17 at 9:04
  • @User552853 Not really. When "more" appears, it's more related to(and perhaps only related to),there's an incomplete command. Parens won't help here for example - you can't do for example dir(<new line>)/p So parenthesis aren't helping to split a command across multiple lines there. In fact caret is used to split whatever command , across multiple lines. Try echo ^ and it will say "More?". The round brackets would interfere with the command(unless the command uses them anyway). – barlop Mar 21 '17 at 9:21
  • ironically, if you put the ( in quotes like your question's title suggests, then you won't get 'More. And the ( would be treated as literal. – barlop Mar 21 '17 at 9:48
6

() isn't really a command but a grouping operator that allows you to group a sequence of commands together to use it in place where the CMD shell normally expects a single command to appear.
The commands between the () are actually executed in a separate instance of CMD. When you type "(" manually you enter that new instance and it waits for your commands.

if not exist c:\myfile (
  echo File doesn't exist
) else (
  echo File DOES exists
  del c:\myfile
)

Note that normally IF ELSE should be on the same line and you can only run a single command in the THEN and the ELSE clause.
Using the "()" treats the entire (xxxxx) block as a single command and even as if it appeared in the same line.

  • i'd agree () is used for grouping commands, but perhaps not just that. What about for %f in (*.txt) do echo %f The parens aren't there to group commands, and if you remove the parens from there then you get a syntax error. – barlop Mar 21 '17 at 9:32
  • The commands between the () are actually executed in a separate instance of CMD? Truly? Check in Task Manager… – JosefZ Mar 21 '17 at 11:00
  • @barlop The () in FOR IN () are part of the syntax of FOR itself. If you follow that up with DO () the parenthesis that go with DO are again grouping parenthesis. – Tonny Mar 21 '17 at 11:53
  • @JosefZ Logical instance with separate scope for e.g. setlocal. It is not a new process. – Tonny Mar 21 '17 at 11:54
  • @Tonny what do you mean "logical instance"? what'd it look like if it wasn't a new "logical instance"? Can you point to anything on google that mentions this thing that you mention about ( creating a new "logical instance"? – barlop Mar 21 '17 at 14:28
3

A lot of good information has already been posted here — e.g., parentheses are “a grouping operator that allows you to group a sequence of commands together to use it in place where the CMD shell normally expects a single command to appear” that “are like a begin block and end block — but some simple things have been overlooked:

1. How not to get the “unexpected at this time” error

You can get something useful to happen simply by typing command(s) at the More?  prompt:

C:\> (
More? date /t
More? )
Tue 04/11/2017

C:\> (
More? dir \Windows\win.ini
More? )
 Volume in drive C is OS
 Volume Serial Number is ▒▒▒▒-▒▒▒▒

 Directory of C:\Windows

08/13/2014  12:26 PM               435 win.ini
               1 File(s)            435 bytes
               0 Dir(s)  ▒▒▒,▒▒▒,▒▒▒,▒▒▒ bytes free

C:\>

You can type multiple commands on separate lines, and you can move the ) up to the end of the last line:

C:\> (
More? date /t
More? time /t
More? )
Tue 04/11/2017
06:18 PM

C:\> (
More? date /t
More? time /t)
Tue 04/11/2017
06:18 PM

C:\>

2. How do ( and ) affect the meaning of a command?

As others have pointed out, there are commands like

C:\> for %f in (\Windows\*.dll) do @echo %f

where the () are part of the syntax of the command itself (FOR, in this case).  If you remove the parentheses, you get a syntax error.  I’m not talking about that.

As stated ad nauseam, parentheses group commands.  Consider this command line:

echo cat & echo dog | findstr /n a

It means

  1. Do echo cat, and then
  2. Do echo dog | findstr /n a
    Since the word “dog” does not contain the letter “a”, this command will produce no output.

Accordingly, the output for the above is

C:\> echo cat & echo dog | findstr /n a
cat

Similarly,

C:\> echo cat & echo dog | findstr /n o
cat
1:dog

in which we get the additional 1:dog output, because “dog” does contain “o”, and “dog” is the first (1st) line of input to the findstr command.

Now consider

(echo cat & echo dog) | findstr /n a

(same as the above, but with added parentheses), which means

  1. Do echo cat,
    and then
    do echo dog, and
  2. Take the output from the above and pipe it into findstr /n a

So,

C:\> (echo cat & echo dog) | findstr /n a
1:cat

C:\> (echo cat & echo dog) | findstr /n o
2:dog

because the output from the left side (#1) of the command line is

cat
dog

and “a” is found on line 1 of that text, and “o” is found on line 2.  We no longer get an output line that says only cat (without a line number) because the echo cat is no longer a free-standing command, but is part of the group that makes up the left side of the pipeline.

Here’s a simpler example of command grouping:

C:\Temp> echo cat & echo dog > animals
cat

C:\Temp> type animals
dog

C:\Temp> (echo cat & echo dog) > animals

C:\Temp> type animals
cat
dog

And, of course, as shown above and in the other answers, you can add line breaks within the group:

C:\> (
More? echo cat
More? echo dog
More? ) | findstr /n a
1:cat

3. Do parentheses create a new instance of CMD?

I don’t think so; not in any obvious, meaningful way:

C:\> (cd windows)

C:\Windows> set a=aardvark

C:\Windows> (set a=bat)

C:\Windows> echo %a%
bat

Note that the cd in parentheses changes the working directory of the main CMD process, and the set in parentheses overrides the variable value that was set outside of parentheses.

2

The ( and ) are like a begin block and end block. Anything that you can put into a batch file (.bat) can be typed into the command line.

For example I can write a batch file with can put a block of statements into an if:

if "%1"=="" (
  echo Multiple
  echo Lines when no parameter
)
echo END

Now when I run the batch without parameters I get all the commands between the ( and ) to run. When a first parameter is supplied the only out put is 'END'.

Now back to the command line: I know of no tricks or techniques to actually use this by directly typing on the command line. But it does however follow the same rules. On command line if enter the following:

(echo "1" & echo "2")

The output is:

1
2
  • Your second example shows your first sentence to be wrong or inaccurate, as even if it were correct, they're not just for begin block and end block with multiple lines. You can do an IF on one line with parentheses too. – barlop Mar 21 '17 at 9:26
  • Also the reason why an open parens can sometimes let you start a new line, is not to do with opening or ending a block. For example it happens also with for %f in ( and And the non-do part of the 'for' (to the extent that any typical programming terminology can be applied to it) isn't generally considered a block. – barlop Mar 21 '17 at 9:30
  • note- when I wrote "second example" I was referring to ((echo "1" & echo "2") and btw there's no reason to put 1 and 2 in quotes. – barlop Mar 21 '17 at 9:47
  • Thanks for the notes. I totally agree in the second example that there is not need for the quotes. I was attempting to point out that the same behavior operates on command line as inside a batch file. BTW I would be very interested if someone can add a command line that must have the () to operate correctly. – miltonb Mar 21 '17 at 19:30
  • 1
    milton, you wrote "I would be very interested if someone can add a command line that must have the () to operate correctly" In response, this would be an example for %f in (*.*) do echo %f So, removing parens causes that command to not execute. – barlop Mar 21 '17 at 21:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.