3

How to prevent batch echo brackets

Batch file

@C:
@CD \Users
@for /D %%d in ("*") do (
 @echo "%%d"
)

Output

C:\Users>()
"Administrator"
...
C:\Users>()
"Public"

2 Answers 2

1

Add in the first line @echo off and remove in the next lines @

@echo off 

cd /d C:\Users
for /D %%d in ("*") do (
  echo "%%d"
)
  • Or just remove (), it is not necessary if you are going to use only one command/line in the loop
@CD /d C:\Users
@for /D %%d in ("*") do @echo "%%d"

Obs.:

CD /D C:\User
/D == goto Drive, so is the same:
C:
CD User


2

I don't see an answer for the specific OP's question: preventing batch echo of brackets when used on the FOR command as in the example. Yes, they can be removed in this case, but assuming one may indeed wish to suppress them from command echo (as was actually asked) since they do serve a useful purpose even for a single command if only for readability...

If command ECHO is OFF (the default), the leading @ (at)sign suppresses echoing the current command. In a FOR loop, everything after "do " ["d"-"o"-space] becomes a new current command for the purposes of iterating the FOR loop, including parenthetical () command blocks. Each (or the only) command in those blocks can be echo-suppressed with a leading @, as in the original example. If every command inside the block is echo-suppressed with a leading @, then the only thing echoed would be the empty set of brackets.

Alternatively, the entire command block can have all the next level commands within echo-suppressed by leading the entire block with @:

@C:
@CD \Users
@for /D %%d in ("*") do @(
 echo "%%d"
)

By moving the leading @ outside the command block, the empty set originally echoed is also echo-suppressed.

1
  • Additionally, this is not necessary for IF blocks because they don't use DO.
    – Yay295
    Feb 7, 2023 at 23:20

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .