Computing.Net > Forums > Windows 2000 > set variable value to the output of commands

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

set variable value to the output of commands

Reply to Message Icon

Name: suitto
Date: June 11, 2009 at 00:18:32 Pacific
OS: Windows XP
Subcategory: Software Problems
Comment:

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.



Sponsored Link
Ads by Google

Response Number 1
Name: Mechanix2Go
Date: June 11, 2009 at 01:33:11 Pacific
Reply:

@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


0

Response Number 2
Name: suitto
Date: June 14, 2009 at 19:01:01 Pacific
Reply:

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.


0

Response Number 3
Name: suitto
Date: June 14, 2009 at 19:21:01 Pacific
Reply:

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.


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More






Use following form to reply to current message:

Login or Register to Reply
LoginRegister


Sponsored links

Ads by Google


Results for: set variable value to the output of commands

installing programs manualy www.computing.net/answers/windows-2000/installing-programs-manualy/23829.html

the event log file is corrupt ? www.computing.net/answers/windows-2000/the-event-log-file-is-corrupt-/42635.html

how to speed up the shutdown? www.computing.net/answers/windows-2000/how-to-speed-up-the-shutdown/34120.html