Link to home
Start Free TrialLog in
Avatar of lesgetdown
lesgetdown

asked on

Batch - take list conver to delimited format and add new line

this is an expansion of a previous question asked
https://www.experts-exchange.com/questions/24343086/Batch-take-list-and-convert-to-delimited-format.html

I am querying a list
A
B
C
D
E
F

that i need to Display in the format of
A B
C D
E F
...

thx!

Avatar of Qlemo
Qlemo
Flag of Germany image

This solution will add a empty line at the beginning. Eliminating this would be

@echo off
setlocal EnableDelayedExpansion
set line=
 set no=0
for /F "tokens=*" %%L in (file.txt) do (
    if !no! == 0 if defined line (echo. !line!) & set line=%%L
    if !no! == 1 set line=!line! %%L
    set /A no=1-no
 )
 echo %line%

Avatar of lesgetdown
lesgetdown

ASKER

that works, but seems to cut out the first variable.

instead of A B

i get <blank> B
everything else displays properly
Hiya... Apologies for the intrusion to all those already present.

Does the following code give you the results you're looking for?


@echo off
setlocal enabledelayedexpansion
set tmp=

(for /f "tokens=*" %%a in (list1.txt) do (
   if not defined tmp (
      set tmp=%%a
   ) else (
      echo !tmp! %%a
      set tmp=
   )
))>list2.txt

if defined tmp echo %tmp%>>list2.txt
ASKER CERTIFIED SOLUTION
Avatar of t0t0
t0t0
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Oh... without the redirection to another file, this will simply output the results to the screen.

@echo off
setlocal enabledelayedexpansion
set tmp=
for /f "tokens=*" %%a in (list1.txt) do (
   if not defined tmp (
      set tmp=%%a
   ) else (
      echo !tmp! %%a
      set tmp=
   )
)
if defined tmp echo %tmp%

Both ways work perfectly!
Oh! Hiya Qlemo.... Your code works well. It's amazing how different our approaches are yet both perform as expected.

They both handle blank lines, multiple blank lines, lines with spaces, even number of lines and odd number of lines.

Not bad eh?
Wow! That was unexpectedly quick....

Thank you.