set variable value to the output of commands

June 11, 2009 at 00:18:32
Specs: Windows XP
Hi,
Is there a direct way to set the value of a variable to the output of a line command.

For instance, I want to set the value of the variable str to the output given by a search command findstr in the file "input.txt".
I can do it using the following codes

findstr "apple" input.txt > dummy
set /p str=<dummy
echo.%str%
del dummy

I wonder can it be more simpler. I tried
set /p str=<findstr "apple" input.txt

and it gives an error.
Thanks for any help and comment.


See More: set variable value to the output of commands

Report •


#1
June 11, 2009 at 01:33:11
@echo off & setLocal EnableDelayedExpansion

for /f "tokens=* delims=" %%a in ('findstr "apple" ^< input.txt') do (
set str=%%a
)


=====================================
If at first you don't succeed, you're about average.

M2


Report •

#2
June 14, 2009 at 19:01:01
Hi,
Thanks for the quick advice on the solution.
I am facing another problem.
I have the following code
@echo off
setLocal EnableDelayedExpansion

for /f "tokens=* delims=" %%a in ('findstr "MATY(003)" input.txt') do (
set str=%%a
echo.!str!
)
for /f "skip=2 tokens=1* delims=]" %%a in ('find /v /n "" input.txt') do (
if "%%b"==!str! echo.SUCCESS
)


The input.txt file has the following content
dummy

MATY(003)=00011, VMAT(1,003)=-2.400E3,1.0E0,,,,
not blank
not blank

dummy
end of file


When the batch file is executed, I don't get the SUCCESS echo. I am puzzled why %%b in the 2nd FOR statement is not matching the !str! set in the 1st FOR statement. Is it because of some hidden characters? Please help.


Report •

#3
June 14, 2009 at 19:21:01
Hi,
Sorry to repost again. I found the answer.
I should remove the quotes around %%b in the 2nd FOR statement. Sorry to create unneccesarry posts.


Report •

Related Solutions


Ask Question