中国DOS联盟论坛

China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --
Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
Guest | Log in | Register | Members | Search | China DOS Union
中国DOS联盟论坛
The time now is 2026-08-25 23:45
47,812 topics / 349,910 posts / today 0 new / 48,264 members
DOS批处理 & 脚本技术(批处理室) » Help!! How to concatenate multiple lines in a text into one line
Printable Version  3,630 / 25
Floor1 qinqong Posted 2006-07-19 13:58
高级用户 Posts 148 Credits 599
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
Floor2 zh159 Posted 2006-07-19 14:12
金牌会员 Posts 1,467 Credits 3,687
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
Floor3 qinqong Posted 2006-07-19 14:15
高级用户 Posts 148 Credits 599
If I don't know how many lines the original text has? What to do? Urgent, thanks.
Floor4 bagpipe Posted 2006-07-19 14:56
银牌会员 Posts 425 Credits 1,144 From 北京
```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
Floor5 qinqong Posted 2006-07-19 15:01
高级用户 Posts 148 Credits 599
Floor6 bagpipe Posted 2006-07-19 15:05
银牌会员 Posts 425 Credits 1,144 From 北京
Let me know the result after the test is completed.

[ Last edited by bagpipe on 2006-7-19 at 15:07 ]
Floor7 namejm Posted 2006-07-19 15:05
荣誉版主 Posts 1,737 Credits 5,226 From 成都
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?
Floor8 qinqong Posted 2006-07-19 15:21
高级用户 Posts 148 Credits 599
Well, it's done. I haven't used DOS for a long time and have almost forgotten it all. Thanks for bagpipe's help.
Floor9 doscc Posted 2006-07-19 16:37
中级用户 Posts 93 Credits 256 From 广东
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!
Floor10 zh159 Posted 2006-07-19 18:17
金牌会员 Posts 1,467 Credits 3,687
@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 ]
Floor11 3742668 Posted 2006-07-19 22:39
荣誉版主 Posts 718 Credits 2,013
In the practical application of batch processing, it is suggested not to use too many variables.

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.
Floor12 zh159 Posted 2006-07-19 22:52
金牌会员 Posts 1,467 Credits 3,687
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.
Floor13 bagpipe Posted 2006-07-20 09:05
银牌会员 Posts 425 Credits 1,144 From 北京
I didn't expect that SET /P can also continuously connect text content. I feel inferior..............
Floor14 amao Posted 2007-02-03 14:17
中级用户 Posts 152 Credits 316
@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 ]
Floor15 jmz573515 Posted 2007-02-04 01:18
银牌会员 Posts 464 Credits 1,212
```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
```
1 2  Next
[ Contact the Union admin team - 中国DOS联盟 - Standard version ]
Sponsored by ifanr Inc | © 2001–2023