Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
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 codesfindstr "apple" input.txt > dummy
set /p str=<dummy
echo.%str%
del dummyI wonder can it be more simpler. I tried
set /p str=<findstr "apple" input.txtand it gives an error.
Thanks for any help and comment.

@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

Hi,
Thanks for the quick advice on the solution.
I am facing another problem.
I have the following code
@echo off
setLocal EnableDelayedExpansionfor /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
dummyMATY(003)=00011, VMAT(1,003)=-2.400E3,1.0E0,,,,
not blank
not blankdummy
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.

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.

![]() |
![]() |
![]() |
| Login or Register to Reply | |
| Login | Register |
| Ads by Google |