Convert new line delimiter to semicolon

March 20, 2012 at 23:18:47
Specs: Windows XP
Hi I have data as mentione below:
a.txt
b.txt
c.txt

Desired output is
a.txt;b.txt;c.txt

Please help to write dos script for this .


See More: Convert new line delimiter to semicolon

Report •

#1
March 20, 2012 at 23:44:41
Try this:

setlocal enabledelayedexpansion
set ctxt=
for /f %%i in (data.txt) do set ctxt=!ctxt!%%i;
set ctxt=%ctxt:~0,-1%


Report •

#2
March 21, 2012 at 03:19:28
Thanks .this seems to be working but it is returning ouptput for each line . How can i read only Last output line which contains all rows with delimiter ;

At present output returned is :
a.txt;
a.txt;b.txt
a.txt;.txt;c.txt


Report •

#3
March 21, 2012 at 04:32:47
Hi ,
I used echooff in the beginning and an getting output as below :

a.txt; b.txt; c.txt;

This output has two problems:

1.It added extra spaces in beginning of each string.
2.It is assing delimiter ; at the end of last string also c.txt; Which is not desired output . Please suggest a way to fix this.


Report •

Related Solutions

#4
March 21, 2012 at 09:39:21

setlocal enabledelayedexpansion
set ctxt=
for /f %%i in (data.txt) do set ctxt=!ctxt!%%i;
rem removes all spaces, doing this first to eliminate ending space, which is why the
rem semicolon remains at the end in the last output.
set ctxt=%ctxt: =$
rem removes last semicolon, this is why it was in sburtchin's origional
set ctxt=%ctxt:~0,-1%

mike


Report •

#5
March 22, 2012 at 03:44:06
Thanks for your explanation .But i have one issue.I am getting output as below :

a.txt; b.txt; c.txt

As you can see there is additional tab between consecutive fields .How can w e remove it .Please advise.


Report •

#6
March 22, 2012 at 03:53:22

Thanks for your explanation .But i have one issue.I am getting output as below :

a.txt; b.txt; c.txt

As you can see there is additional tab between consecutive fields and an additional line is alsoa dded at the end .How can we remove it .Please advise.



Report •

#7
March 22, 2012 at 05:49:43
@echo off & setlocal enabledelayedexpansion
for /f %%i in (%1) do (
    set ctxt=!ctxt!%%i;
)
set ctxt=!ctxt:~0,-1!
echo !ctxt!


Report •

#8
March 22, 2012 at 06:17:00
sorry thought it was a space which is what the

set ctxt=%ctxt: =$

was for

mike


Report •

#9
March 23, 2012 at 02:09:18
Just put a tab in there instead of the space!

set ctxt=%ctxt:	=$

NOTE: You may have to do it yourself. I'm not sure if this forum editor can accept tabs. Also, I think the "$" might be a typo for "%", but mikelinus probably knows more about this stuff than I do.

AFAIK the following are equivalent (delayed variable expansion (with "!") is needed in the FOR loop). I'm still trying to decide for myself if code portability (using %) outweighs the benefits of self consistency (using !).

setlocal enabledelayedexpansion
set ctxt=
for /f %%i in (data.txt) do set ctxt=!ctxt!%%i;
set ctxt=!ctxt: =!
set ctxt=!ctxt:~0,-1!
echo !ctxt!> data2.txt

setlocal enabledelayedexpansion
set ctxt=
for /f %%i in (data.txt) do set ctxt=!ctxt!%%i;
set ctxt=%ctxt: =%
set ctxt=%ctxt:~0,-1%
echo %ctxt%> data2.txt

ECHO adds a carriage return + line feed at the end. Not really an extra line. I don't know if there is any way in batch to eliminate that, and don't know it it would ever matter (in batch).

You never said what you wanted to do with data2.txt?


Report •

#10
March 23, 2012 at 07:33:42
not true, slipped it was %

mike


Report •

Ask Question