concatenating string in FOR loop in batch file

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?

3 Answers

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.

The value of %myvar% inside the for loop will always be the value it was before the loop began. If you want to use the modified variable, use !myvar! instead.

@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:~1%

This will output

16320 142 16178

The %myvar:~1% part there simply removes the space that your for loop places at the beginning of the first number.

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%

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like