Viktor
Viktor

Reputation: 3

Batch "For" loop outputs in two lines instead of one in TXT

I want this following for-loop to output the findings of these two searches in one line in the All_Metadata.txt

for %%a in (*.txt) do (
    findstr /B "589" %%a
    findstr /X /C:"1 1,96 " %%a
    ) >> All_Metadata.txt

This For-Loop output looks like this:

58998545
1 1,96

Instead of (this is what it should look like):

58998545 1 1,96

Operating System: Windows 10

Upvotes: 0

Views: 53

Answers (1)

Stephan
Stephan

Reputation: 56180

Capture the output of the both findstr commands into variables with for /f loops and echo them together (no need for a variable with the second one; just use the for variable):

@echo off
setlocal ENABLEDELAYEDEXPANSION 
for %%a in (*.txt) do (
    set "b="
    set "c="
    for /f "delims=" %%b in ('findstr /B "589" "%%a"') do set "b=%%b"
    for /f "delims=" %%c in ('findstr /i /X /C:"1 1,96 " "%%a"') do set "c=%%c"
    echo(!b! !c!
) 

Another way is, to use the set /p trick to write the first findstr result without a line feed and just add the result from the second findstr (doesn't need delayed expansion):

@echo off
setlocal
for %%a in (*.txt) do (
    set "b="
    for /f "delims=" %%b in ('findstr /B "589" "%%a"') do <nul set /p "=%%b "
    findstr /i /X /C:"1 1,96 " "%%a"
) 

Upvotes: 2

Related Questions