1

For example, this code works fine

wmic MEMORYCHIP get Capacity | findstr [0-9] >> D:\ram-cpu.txt

And give memory banks capacity as output

But if I put this command in cycle

Setlocal EnableDelayedExpansion
@SET /a sl_counter=0
@FOR /F "usebackq" %%a IN (`wmic MEMORYCHIP get Capacity | findstr [0-9]`) DO (
@ECHO SLOT !sl_counter! %%a >> D:\ram-cpu.txt
@SET /a sl_counter=sl_counter+1
)

I recieve error

Unexpected: |.
D:\>@FOR /F "usebackq" %a IN (`wmic MEMORYCHIP get Capacity | findstr [0-9]`) DO
 (

I guess I need some syntax to be added or fixed, but I have no idea how to do it.

alive-one
  • 33
  • 5

1 Answers1

2

You should escape | by ^| when you use it inside the for command

Commandline :

@FOR /F "usebackq" %a IN (`wmic MEMORYCHIP get Capacity ^| findstr [0-9]`) DO echo %a

Batch File :

@echo off
@FOR /F "usebackq" %%a IN (`wmic MEMORYCHIP get Capacity ^| findstr [0-9]`) DO Set "Memory=%%a"
    echo %Memory%
pause

Further Reading about : How-to: Escape Characters, Delimiters and Quotes at the Windows command line.

Hackoo
  • 18,337
  • 3
  • 40
  • 70