China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --

Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
DOS stands for freedom, openness and progress. Let us work hard, learn from the openness and GNU spirit of FreeDOS and Linux, and together build and grow a free GNU GPL world!

中国DOS联盟论坛
The time now is 2026-06-27 16:24
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » Help!! How to concatenate multiple lines in a text into one line View 3,401 Replies 25
Original Poster Posted 2006-07-19 13:58 ·  中国 上海 电信
高级用户
★★
Credits 599
Posts 148
Joined 2003-10-30 00:00
22-year member
UID 12192
Gender Male
Status Offline
The problem is like this. There is a text file with the following content:

/lib/AdaptiveMQ.jar
/lib/AdaptiveMQ.jar
/lib/FIX.jar

How can I put it into another file whose content is:

/lib/AdaptiveMQ.jar ;/lib/AdaptiveMQ.jar ;/lib/FIX.jar
Floor 2 Posted 2006-07-19 14:12 ·  中国 广西 玉林 博白县 电信
金牌会员
★★★★
Credits 3,687
Posts 1,467
Joined 2005-08-08 12:00
20-year member
UID 44210
Status Offline
These days I'm also compiling some batch scripts and have learned some simple ones.
@echo off
setlocal EnableDelayedExpansion
set N=1
for /f %%a in (original text.txt) do (set New!N!=%%a
set /a N=!N! + 1)

echo %New1% ;%New2% ;%New3%>new text.txt

Among them, %New1%~%NewN% increase incrementally according to the number of lines
Floor 3 Posted 2006-07-19 14:15 ·  中国 上海 电信
高级用户
★★
Credits 599
Posts 148
Joined 2003-10-30 00:00
22-year member
UID 12192
Gender Male
Status Offline
If I don't know how many lines the original text has? What to do? Urgent, thanks.
Floor 4 Posted 2006-07-19 14:56 ·  中国 北京 联通
银牌会员
★★★
DOS联盟捡破烂的
Credits 1,144
Posts 425
Joined 2005-10-20 00:00
20-year member
UID 43784
From 北京
Status Offline
```batch
@echo off
setlocal enabledelayedexpansion
for /f "delims=" %%a in (原文件.txt) do (
if not defined a (set a=%%a) else (set a=!a!;%%a)
)
echo %a%>生成文件.txt
```

It can be determined that if the file length is too large, it will definitely not work, because there are certain requirements for the SET setting variable in cmd. Also, please ask the expert to think about how to write this batch processing... The owner can try the above code. If it doesn't work, it means the file length is too large
Floor 5 Posted 2006-07-19 15:01 ·  中国 上海 电信
高级用户
★★
Credits 599
Posts 148
Joined 2003-10-30 00:00
22-year member
UID 12192
Gender Male
Status Offline
Floor 6 Posted 2006-07-19 15:05 ·  中国 北京 联通
银牌会员
★★★
DOS联盟捡破烂的
Credits 1,144
Posts 425
Joined 2005-10-20 00:00
20-year member
UID 43784
From 北京
Status Offline
Let me know the result after the test is completed.

[ Last edited by bagpipe on 2006-7-19 at 15:07 ]
Floor 7 Posted 2006-07-19 15:05 ·  中国 四川 成都 鹏博士宽带
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
Hehe, bagpipe has made another move.

What does "defined" mean in the sentence "if not defined a (set a=%%a) else (set a=!a!;%%a)"? I'm seeing this usage for the first time. Can you explain this code?
Floor 8 Posted 2006-07-19 15:21 ·  中国 上海 电信
高级用户
★★
Credits 599
Posts 148
Joined 2003-10-30 00:00
22-year member
UID 12192
Gender Male
Status Offline
Well, it's done. I haven't used DOS for a long time and have almost forgotten it all. Thanks for bagpipe's help.
Floor 9 Posted 2006-07-19 16:37 ·  中国 北京 海淀区 IDC机房
中级用户
★★
Credits 256
Posts 93
Joined 2006-03-26 22:12
20-year member
UID 52853
Gender Male
From 广东
Status Offline
Originally posted by namejm at 2006-7-19 15:05:
  Hehe, bagpipe has made a move again.

  What does "defined" mean in the sentence "if not defined a (set a=%%a) else (set a=!a!;%%a)"? I've never seen this usage before. Can you explain this code...

It means that if a is not defined, then execute set a=%%a. If it is defined, then execute the statement after else!
Floor 10 Posted 2006-07-19 18:17 ·  中国 广西 玉林 博白县 电信
金牌会员
★★★★
Credits 3,687
Posts 1,467
Joined 2005-08-08 12:00
20-year member
UID 44210
Status Offline
@echo off
setlocal EnableDelayedExpansion
set N=1
for /f "delims=" %%a in (original file.txt) do (set New!N!=%%a
set /a N=!N! + 1)
set /a N=%N%-1

echo echo %%New1%%>Temp.bat
if %N% LEQ 1 goto End
set M=2

:len
for /f "delims=" %%a in (Temp.bat) do set X=%%a
echo %X% ;%%New%M%%%^>new file.txt>Temp.bat
set /a M=%M%+1
if %M% GTR %N% goto End
goto len

:End
call Temp.bat
del Temp.bat


Tried with 400 lines and it was okay, but had an error with 500 lines. It seems like the for loop didn't continue. It didn't even execute to Temp.bat. The efficiency is worse than bagpipe's, but the SET variable setting isn't as large as bagpipe's
[ Last edited by zxcv on 2006-7-19 at 18:54 ]
Floor 11 Posted 2006-07-19 22:39 ·  中国 湖北 荆门 电信
荣誉版主
★★★
Credits 2,013
Posts 718
Joined 2006-02-18 07:07
20-year member
UID 50550
Status Offline
In the practical application of batch processing, it is suggested not to use too many variables.

for /f "delims=" %i in (source file.txt) do @set /p "var=%i;" <nul >> target file.txt

Run it in the command prompt. If you want to write it into a script, change %i to %%i. There is no error handling for the situation where quotes appear in the text.
Floor 12 Posted 2006-07-19 22:52 ·  中国 广西 玉林 博白县 电信
金牌会员
★★★★
Credits 3,687
Posts 1,467
Joined 2005-08-08 12:00
20-year member
UID 44210
Status Offline
Still, the version of moderator 3742668 is powerful. The 500 lines of mine and bagpipe's hung up, but the 1200 lines of moderator 3742668 are still okay.
Floor 13 Posted 2006-07-20 09:05 ·  中国 北京 联通
银牌会员
★★★
DOS联盟捡破烂的
Credits 1,144
Posts 425
Joined 2005-10-20 00:00
20-year member
UID 43784
From 北京
Status Offline
I didn't expect that SET /P can also continuously connect text content. I feel inferior..............
Floor 14 Posted 2007-02-03 14:17 ·  美国 北达科他州立大学
中级用户
★★
Credits 316
Posts 152
Joined 2006-06-18 13:01
20-year member
UID 57204
Gender Male
Status Offline
@sed -e :a -e "N;$!ba;s/ *\n/ ;/g;s/*$//" test.txt

Referenced the relevant code of Bamboo Wu Nai He and made modifications, applicable for over 3600 lines.

[ Last edited by amao on 2007-2-3 at 02:58 PM ]
Floor 15 Posted 2007-02-04 01:18 ·  中国 辽宁 本溪 联通
银牌会员
★★★
Credits 1,212
Posts 464
Joined 2006-12-13 21:11
19-year member
UID 73417
Gender Male
Status Offline
```vbscript
set fso=createobject("scripting.filesystemobject")
set file=fso.opentextfile("a.txt")
do while file.atendofstream<>true
s=s & file.readline & " ;"
loop
file.close
set file=fso.createtextfile("b.txt")
file.write left(s,len(s)-1)
file.close
msgbox "Operation successful",4096
```
Forum Jump: