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

this is an expansion of a previous question asked
http://www.experts-exchange.com/Programming/Languages/Scripting/Shell/Batch/Q_24343086.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!

LVL 1
lesgetdownAsked:
Who is Participating?
 
t0t0Commented:
Comma-delimited text.

I've changed the style slightly and added ',' (commas). Please see below:


@echo off
setlocal enabledelayedexpansion
del list2.txt 2>nul
set tmp=

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

if defined tmp echo %tmp%>>list2.txt
0
 
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
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%

0
 
lesgetdownAuthor Commented:
that works, but seems to cut out the first variable.

instead of A B

i get <blank> B
everything else displays properly
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
t0t0Commented:
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
0
 
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
Ok, let's use a slightly changed version:

@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:~1!&set line=
   set line=!line! %%L
   set /A no=1-no
)
echo %line:~1%

0
 
t0t0Commented:
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%

0
 
lesgetdownAuthor Commented:
Both ways work perfectly!
0
 
t0t0Commented:
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?
0
 
t0t0Commented:
Wow! That was unexpectedly quick....

Thank you.

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.