Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I have a file sfsview_monitor1.txt with the following content:

Total pages: 16320
Pages used: 142
Pages free: 16178

I want the output like this:

16320 142 16178

with the following code I am not able to do it:

@echo on 
 SETLOCAL EnableDelayedExpansion

SET myvar=
for /f "tokens=1-3" %%P in ('type c:\shafique\sfsview_monitor1.txt ^| FINDSTR /B /I "Total Pages"') do (
    SET myvar=%myvar% %%R
 )
echo %myvar%

can someone point me in the right direction?

share|improve this question

2 Answers 2

IMO this doesn't necessarily have to be as complicated as the other answer makes it out to be.

If those are the only lines in the text file then you can use the following:

@echo off
set var=
setlocal enabledelayedexpansion
for /f "tokens=2 delims=:" %%a in (sfsview_monitor1.txt) do set var=!var!%%a
set var=!var:~1!
echo !var!
endlocal

If the lines are present at random locations in the text file then you can use the following:

@echo off
set var=
setlocal enabledelayedexpansion
for /f "tokens=2 delims=:" %%a in ('findstr /c:"Total pages" /c:"Pages used" /c:"Pages free" sfsview_monitor1.txt') do set var=!var!%%a
set var=!var:~1!
echo !var!
endlocal

You don't even need the first version actually since the second one is more generic.


If you don't even care about storing the values in a variable and are simply interested in the output, the following one liners will do just as well:

@for /f "tokens=2 delims=:" %%a in (sfsview_monitor1.txt) do @echo | set /p var=%%a 

and

@for /f "tokens=2 delims=:" %%a in ('findstr /c:"Total pages" /c:"Pages used" /c:"Pages free" sfsview_monitor1.txt') do @echo | set /p var=%%a 

Important: In both these one line versions there is a space at the end of the line that is required! Also, as noted above you can simply ignore the first version and use the second, since the latter is the more generic of the two.

share|improve this answer

Try this:

@echo off &setlocal
set "tf=%temp%%random%"
set "pt=Total pages"
set "pu=Pages used"
set "pf=Pages free"
(echo(%pt%&echo(%pu%&echo(%pf%)>"%tf%"

for /f "tokens=1,2delims=:" %%i in ('^<"sfsview_monitor1.txt" findstr /g:"%tf%"') do (
    if "%%i"=="%pt%" for /f %%a in ("%%j") do set "npt=%%a"
    if "%%i"=="%pu%" for /f %%a in ("%%j") do set "npu=%%a"
    if "%%i"=="%pf%" for /f %%a in ("%%j") do set "npf=%%a"
)
del "%tf%"
SET "myvar=%npt% %npu% %npf%"
echo(%myvar%
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.