?
Solved

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

Posted on 2009-04-28
9
Medium Priority
?
395 Views
Last Modified: 2012-05-06
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!

0
Comment
Question by:lesgetdown
  • 5
  • 2
  • 2
9 Comments
 
LVL 71

Expert Comment

by:Qlemo
ID: 24251552
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
 
LVL 1

Author Comment

by:lesgetdown
ID: 24251640
that works, but seems to cut out the first variable.

instead of A B

i get <blank> B
everything else displays properly
0
 
LVL 16

Expert Comment

by:t0t0
ID: 24252264
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
Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

 
LVL 16

Accepted Solution

by:
t0t0 earned 1000 total points
ID: 24252324
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
 
LVL 71

Assisted Solution

by:Qlemo
Qlemo earned 1000 total points
ID: 24252358
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
 
LVL 16

Expert Comment

by:t0t0
ID: 24252388
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
 
LVL 1

Author Closing Comment

by:lesgetdown
ID: 31575479
Both ways work perfectly!
0
 
LVL 16

Expert Comment

by:t0t0
ID: 24252473
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
 
LVL 16

Expert Comment

by:t0t0
ID: 24252632
Wow! That was unexpectedly quick....

Thank you.

0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Using dates in 'DOS' batch files has always been tricky as it has no built in ways of extracting date information.  There are many tricks using string manipulation to pull out parts of the %date% variable or output of the date /t command but these r…
YESTERDAY YESTERDAY.BAT is inspired by a previous article I wrote entitled: TOMORROW.BAT (http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/A_4196-Advanced-Batch-File-Programming-TOMORROW-BAT.html). The crux of this batch f…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Screencast - Getting to Know the Pipeline
Suggested Courses

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question