Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
196 views
in Technique[技术] by (71.8m points)

windows - Capture output command CMD

Platform Windows XP

When writing a command file (.bat) how can i "catch" the output from a command into a variable ?

I want to do something like this

SET CR='dir /tw /-c b.bat | findstr /B "[0-9]"'

But this do not work

Regards Stefan

PS No, I can not dowload grep, cygwin or any other software, it have to be the CMD DS

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can use for /f for that:

for /f %%L in ('dir /tw /-c b.bat ^| findstr /b "[0-9]"') do set CR=%%L

This assumes that there is only a single line of output, though. You cannot (trivially or usefully) capture more than one line in a variable.

I can only guess what you're really trying to do here, though. If you need the file size (just guessing because of the /-c) then it's certainly easier to use

for %%X in (b.bat) do set size=%%~zX

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...