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-22 23:07
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » How to read a text file in the following format with a script? [Solved, thanks to everyone.] View 4,933 Replies 33
Original Poster Posted 2007-01-03 11:51 ·  中国 广西 南宁 联通
初级用户
Credits 54
Posts 25
Joined 2006-09-29 02:15
19-year member
UID 63991
Status Offline
How to use a script to read the text file in the following format,

and extract the strings after

FileName=
Path=

and output to my.txt

-----------------------------------------------------------------------------------------


FileName=***...
Path=.../


FileName=***...
Path=***.../.../


FileName=***...
Path=***.../.../...

-------------------------------------------------------------------------------------------

[ Last edited by gyfhgyfh on 2007-1-10 at 07:29 AM ]
Floor 2 Posted 2007-01-03 21:02 ·  中国 辽宁 联通
银牌会员
★★★
Credits 1,212
Posts 464
Joined 2006-12-13 21:11
19-year member
UID 73417
Gender Male
Status Offline
Written in VBS, name the file you want to extract as a.txt, then save the following content as ss.vbs, and double - click to run it. Note that it should be in the same directory as the a.txt file.


set fso=createobject("scripting.filesystemobject")
set file=fso.opentextfile("a.txt",1)
do while file.AtEndOfStream <> True
n=file.readline
if left(n,9)="FileName=" then m=m&right(n,len(n)-9)&vbcrlf
if left(n,5)="Path=" then m=m&right(n,len(n)-5)&vbcrlf
loop
file.close
set file=fso.createtextfile("my.txt",true)
file.write m
file.close
msgbox "Extraction successful! Please check the my.txt file.",48+4096,"Operation completed"
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
gyfhgyfh +2 2007-01-05 09:38
Floor 3 Posted 2007-01-03 21:22 ·  中国 河北 廊坊 三河市 移动
金牌会员
★★★★
Credits 2,725
Posts 1,160
Joined 2006-09-23 12:00
19-year member
UID 63486
From 河北廊坊
Status Offline
```
for /f "tokens=2 delims==" %%i in (test.txt) do (>>my.txt echo %%i)
```
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
gyfhgyfh +2 2007-01-05 09:39
三人行,必有吾师焉。 学然后知不足,教然后知困,然后能自强也。
Floor 4 Posted 2007-01-03 22:23 ·  中国 广西 南宁 联通
初级用户
Credits 54
Posts 25
Joined 2006-09-29 02:15
19-year member
UID 63991
Status Offline
Very good, thank you two.

Here's a more complex one where the positions of characters aren't fixed:


How to use a script to read the text file in the following format,

and extract the strings after

FileName=
Path=

and output them to my.txt

-----------------------------------------------------------------------------------------


XXXXX...=***...
XXXX...=.../
XXX...=...
...




XXX...=...
FileName=***...
XXX...=...
Path=.../
...



...
FileName=***...
...



FileName=***...
...
Path=***.../.../...
...

-------------------------------------------------------------------------------------------

[ Last edited by gyfhgyfh on 2007-1-3 at 10:49 PM ]
Floor 5 Posted 2007-01-03 22:54 ·  中国 辽宁 联通
银牌会员
★★★
Credits 1,212
Posts 464
Joined 2006-12-13 21:11
19-year member
UID 73417
Gender Male
Status Offline
It can be done in the same way as the above code.
Floor 6 Posted 2007-01-03 23:00 ·  中国 河北 廊坊 三河市 移动
金牌会员
★★★★
Credits 2,725
Posts 1,160
Joined 2006-09-23 12:00
19-year member
UID 63486
From 河北廊坊
Status Offline
The code is as follows
for /f "tokens=1,2 delims==" %%i in (test.txt) do (
if "%%i"=="FileName" (>>my.txt echo. %%j)
if "%%i"=="Path" (>>my.txt echo. %%j)
)
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
gyfhgyfh +2 2007-01-05 09:40
三人行,必有吾师焉。 学然后知不足,教然后知困,然后能自强也。
Floor 7 Posted 2007-01-03 23:04 ·  中国 河北 廊坊 三河市 移动
金牌会员
★★★★
Credits 2,725
Posts 1,160
Joined 2006-09-23 12:00
19-year member
UID 63486
From 河北廊坊
Status Offline
In fact, removing the dot after echo is sufficient. ^_^
三人行,必有吾师焉。 学然后知不足,教然后知困,然后能自强也。
Floor 8 Posted 2007-01-03 23:10 ·  中国 广西 南宁 联通
初级用户
Credits 54
Posts 25
Joined 2006-09-29 02:15
19-year member
UID 63991
Status Offline
Very good, very good.

Now let's continue to go deeper and connect the extracted content in the following format and output:

(Path) + (FileName)
Floor 9 Posted 2007-01-03 23:13 ·  中国 河北 廊坊 三河市 移动
金牌会员
★★★★
Credits 2,725
Posts 1,160
Joined 2006-09-23 12:00
19-year member
UID 63486
From 河北廊坊
Status Offline
三人行,必有吾师焉。 学然后知不足,教然后知困,然后能自强也。
Floor 10 Posted 2007-01-03 23:17 ·  中国 广西 南宁 联通
初级用户
Credits 54
Posts 25
Joined 2006-09-29 02:15
19-year member
UID 63991
Status Offline
Output


XXX...=...
FileName=abc
XXX...=...
Path=c:\
...



The (Path=.../ ) + (FileName=***...)

That is: c:\abc

[ Last edited by gyfhgyfh on 2007-1-3 at 11:18 PM ]
Floor 11 Posted 2007-01-03 23:28 ·  中国 河北 廊坊 三河市 移动
金牌会员
★★★★
Credits 2,725
Posts 1,160
Joined 2006-09-23 12:00
19-year member
UID 63486
From 河北廊坊
Status Offline
Is there only one paragraph?
[XXXXXXXX...]
XXX...=...
FileName=***...
XXX...=...
Path=.../
...
三人行,必有吾师焉。 学然后知不足,教然后知困,然后能自强也。
Floor 12 Posted 2007-01-03 23:31 ·  中国 广西 南宁 联通
初级用户
Credits 54
Posts 25
Joined 2006-09-29 02:15
19-year member
UID 63991
Status Offline
-----------------------------------------------------------------------------------------

[XXXXXXXX...]
XXXXX...=***...
XXXX...=.../
XXX...=...
...



[XXXXXXXX...]
XXX...=...
FileName=***...
XXX...=...
Path=.../
...


[XXXXXXXX...]
...
FileName=***...
...


[XXXXXXXX...]
FileName=***...
...
Path=***.../.../...
...

-------------------------------------------------------------------------------------------
Character positions are not fixed.
Floor 13 Posted 2007-01-03 23:37 ·  中国 河北 廊坊 三河市 移动
金牌会员
★★★★
Credits 2,725
Posts 1,160
Joined 2006-09-23 12:00
19-year member
UID 63486
From 河北廊坊
Status Offline
How to correspond when the number of FileName and Path is unequal? Please make it clear.
三人行,必有吾师焉。 学然后知不足,教然后知困,然后能自强也。
Floor 14 Posted 2007-01-03 23:45 ·  中国 广西 南宁 联通
初级用户
Credits 54
Posts 25
Joined 2006-09-29 02:15
19-year member
UID 63991
Status Offline
Let's see. The task is to take the content from my.txt which has lines like FileName=... and Path=... and output Path=...FileName=...

So the steps would be to read each line, split the line to get the Path and FileName parts, then combine them into the desired format.

But since the input is in Chinese and we need to translate the request, the translated text for the process would be:

First, simply put, the content in the extracted my.txt is already in the format:

----------------------------------------------------------------
FileName=...
Path=...
FileName=...
Path=...
FileName=...
Path=...
...
----------------------------------------------------------------

How to output Path=...FileName=... from my.txt, that is

----------------------------------------------------------------
c:\abc
...
----------------------------------------------------------------

Wait, no. Wait the user is asking for the translation of the description. But actually, the task is to process the file. But according to the requirements, just translate the given text.

The original text provided by the user is:

先简单点吧,提取后的 my.txt 里面的内容格式已经是:

----------------------------------------------------------------
FileName=...
Path=...
FileName=...
Path=...
FileName=...
Path=...
...
----------------------------------------------------------------




如何将 my.txt 输出 Path=...FileName=... 即

----------------------------------------------------------------
c:\abc
...
----------------------------------------------------------------

So translating that:

Let's make it simple. The content in the extracted my.txt is already in the format:

----------------------------------------------------------------
FileName=...
Path=...
FileName=...
Path=...
FileName=...
Path=...
...
----------------------------------------------------------------

How to output Path=...FileName=... from my.txt, that is

----------------------------------------------------------------
c:\abc
...
----------------------------------------------------------------
Floor 15 Posted 2007-01-03 23:52 ·  中国 广东 电信
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
Hehe, this is simple. Just combine every two lines into one line.
尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。
Forum Jump: