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 21:38
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » How to read a text file in the following format with a script? [Solved, thanks to everyone.] View 4,931 Replies 33
Floor 16 Posted 2007-01-04 00:07 ·  中国 广东 电信
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
To let you see the effect, make a demo code:

@echo off
for /f "tokens=2 delims==" %%i in (my.txt) do call :link "%%i"
pause
goto :eof

:link
set str=%1
set "str=%str:~1,-1%"
set /a num+=1
set "str=%str%%var%"
set "var=%str%"
if %num% equ 2 (
echo "%str%"
set num=0
set str=
set var=
)
goto :eof
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
gyfhgyfh +1 2008-01-07 13:38
尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。
Floor 17 Posted 2007-01-04 00:22 ·  中国 辽宁 联通
银牌会员
★★★
Credits 1,212
Posts 464
Joined 2006-12-13 21:11
19-year member
UID 73417
Gender Male
Status Offline
set fso=createobject("scripting.filesystemobject")
set file=fso.opentextfile("a.txt",1)
do while file.AtEndOfStream <> True
m="%%%%%%%%%%%%%%%%%"
n=file.readline
if left(n,9)="FileName=" then a=right(n,len(n)-9)&vbcrlf
if left(n,5)="Path=" then m=right(n,len(n)-5)&a
if m<>"%%%%%%%%%%%%%%%%%" then mm=mm&m
m="%%%%%%%%%%%%%%%%%"
loop
file.close
set file=fso.createtextfile("my.txt",true)
file.write mm
file.close
msgbox "提取内容成功!请查看my.txt文件。",48+4096,"操作完成"
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
gyfhgyfh +2 2007-01-06 22:47
Floor 18 Posted 2007-01-04 00:40 ·  中国 广西 南宁 联通
初级用户
Credits 54
Posts 25
Joined 2006-09-29 02:15
19-year member
UID 63991
Status Offline
Thanks everyone, finally understand.

Wish everyone a happy new year!!!
Floor 19 Posted 2007-01-04 04:36 ·  中国 河北 廊坊 三河市 移动
金牌会员
★★★★
Credits 2,725
Posts 1,160
Joined 2006-09-23 12:00
19-year member
UID 63486
From 河北廊坊
Status Offline
### test.txt content is as follows:

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




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



...
FileName=456
...
Path=d:\


FileName=789
...
Path=e:\
...



### my.bat code is as follows:

@echo off
for /f "tokens=1,2 delims==" %%i in (test.txt) do (
if "%%i"=="FileName" (>>a.txt echo %%j)
if "%%i"=="Path" (>>a.txt echo %%j)
)

setlocal enabledelayedexpansion
for /f "delims=: tokens=1*" %%i in ('findstr /n .* a.txt') do set num=%%i && set "%%i=%%j"
set str=
for /l %%i in (1,1,%num%) do (
if defined str (>>my.txt echo,!%%i!!str!&& set "str="
) else (
set "str=!%%i!"
)
)
>>my.txt echo,!str!&& set "str="
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
gyfhgyfh +2 2008-01-07 13:38
三人行,必有吾师焉。 学然后知不足,教然后知困,然后能自强也。
Floor 20 Posted 2007-01-04 04:39 ·  中国 河北 廊坊 三河市 移动
金牌会员
★★★★
Credits 2,725
Posts 1,160
Joined 2006-09-23 12:00
19-year member
UID 63486
From 河北廊坊
Status Offline
Running result
The content of my.txt is as follows:
c:\123
d:\456
e:\789
三人行,必有吾师焉。 学然后知不足,教然后知困,然后能自强也。
Floor 21 Posted 2007-01-04 05:16 ·  中国 广西 南宁 联通
初级用户
Credits 54
Posts 25
Joined 2006-09-29 02:15
19-year member
UID 63991
Status Offline
Thanks to ccwan, the script provided this time is already very complete.

The function of the script is really powerful!

Thanks again to everyone.
Floor 22 Posted 2007-01-05 02:21 ·  中国 广西 南宁 联通
初级用户
Credits 54
Posts 25
Joined 2006-09-29 02:15
19-year member
UID 63991
Status Offline
How to solve this???

Extract text.txt
----------------------------------------------
ab.exe
2006.10.26
c:\my\
cde.com
2007.1.1
d:\
f.bat
2007.1.3
c:\
...
-----------------------------------------------

Output to list.txt
----------------------------------------------
c:\my\ab.exe 2006.10.26
d:\cde.com 2007.1.1
c:\f.bat 2007.1.3
...
-----------------------------------------------

Then extract the latest file list by comparing different list.txt
--------------------------------------------------------
c:\my\ab.exe 2006.10.26
d:\cde.com 2007.1.1
c:\f.bat 2007.1.3
...
--------------------------------------------------------

--------------------------------------------------------
c:\my\ab.exe 2007.10.26
d:\cde.com 2007.1.1
c:\f.bat 2007.1.3
...
--------------------------------------------------------


That is
--------------------------------------------------------
c:\my\ab.exe 2007.10.26
...
--------------------------------------------------------

[ Last edited by gyfhgyfh on 2007-1-5 at 02:38 AM ]
Floor 23 Posted 2007-01-05 03:00 ·  中国 辽宁 联通
银牌会员
★★★
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",1)
m=1
do while file.AtEndOfStream <> True
n=file.readline
if m=1 then
a=n
m=m+1
elseif m=2 then
a=a&" "&n
m=m+1
elseif m=3 then
a=n&a
m=1
s=s&a&vbcrlf
a=""
end if
loop
file.close
set file=fso.createtextfile("my.txt",true)
file.write s
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-10 07:27
Floor 24 Posted 2007-01-05 03:06 ·  中国 辽宁 联通
银牌会员
★★★
Credits 1,212
Posts 464
Joined 2006-12-13 21:11
19-year member
UID 73417
Gender Male
Status Offline
I didn't understand what you meant by "then extract the latest file list by comparing different list.txt".
Floor 25 Posted 2007-01-05 03:18 ·  中国 广东 电信
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
Brother jmz573515's code is quite good.

If you want to use batch processing to extract the content of test.txt, you can make slight modifications to the demonstration code on floor 16. Because considering that there may be special characters in the path, such as spaces, &,!, etc., when generating list.txt, the path is enclosed in quotes:

@echo off
cd.>list.txt
set num=0
for /f "delims=" %%i in (test.txt) do call :link "%%i"
start list.txt
goto :eof

:link
set str=%1
set "str=%str:~1,-1%"
set /a num+=1
if %num% equ 2 (
set "time_=%str%"
) else (
call set "str=%%str%%%var%"
set "var=%str%"
)
if %num% equ 3 (
echo "%str% %time_%">>list.txt
set num=0
set str=
set time_=
)
goto :eof


As for comparing the contents of two files, it is relatively complicated. I haven't thought of how to extract the latest record for the time being - if the records in the two list.txt are in one-to-one correspondence and only the time is different, then it is a bit easier to compare.

[ Last edited by namejm on 2007-1-4 at 02:19 PM ]
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
gyfhgyfh +2 2007-01-06 22:48
尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。
Floor 26 Posted 2007-01-05 03:33 ·  中国 广西 南宁 联通
初级用户
Credits 54
Posts 25
Joined 2006-09-29 02:15
19-year member
UID 63991
Status Offline
### Solution 1: Using batch script (for Windows environment)
1. First, write a batch script (for example, named `compare.bat`):
```batch
@echo off
fc A.txt B.txt > temp.txt
for /f "tokens=1,2 delims= " %%a in ('findstr "c:\\my\\ab.exe" temp.txt') do (
set "old_date=%%b"
)
for /f "tokens=1,2 delims= " %%a in ('findstr "c:\\my\\ab.exe" B.txt') do (
set "new_date=%%b"
)
if "%old_date%" LSS "%new_date%" (
echo c:\my\ab.exe %new_date%
) else (
echo c:\my\ab.exe %old_date%
)
del temp.txt
```
- Explanation:
- The `fc` command is used to compare two files, and the result is redirected to `temp.txt`.
- Then, use `findstr` to find the line containing `c:\my\ab.exe` in `temp.txt` to get the old date.
- Then, find the line containing `c:\my\ab.exe` in `B.txt` to get the new date.
- Compare the two dates, and output the one with the more recent date.

### Solution 2: Using PowerShell (for Windows environment with PowerShell installed)
1. Open PowerShell and run the following commands:
```powershell
$a = Get-Content A.txt | Where-Object {$_ -match 'c:\\my\\ab.exe'}
$b = Get-Content B.txt | Where-Object {$_ -match 'c:\\my\\ab.exe'}
$dateA = $a.Split(' ')[1]
$dateB = $b.Split(' ')[1]
$dateAObj = [DateTime]::ParseExact($dateA, 'yyyy.MM.dd', $null)
$dateBObj = [DateTime]::ParseExact($dateB, 'yyyy.MM.dd', $null)
if ($dateAObj -lt $dateBObj) {
Write-Host "c:\my\ab.exe $dateB"
} else {
Write-Host "c:\my\ab.exe $dateA"
}
```
- Explanation:
- First, use `Get-Content` to read the contents of `A.txt` and `B.txt`, and then use `Where-Object` to filter the line containing `c:\my\ab.exe`.
- Then, split the line to get the date part.
- Parse the date strings into `DateTime` objects.
- Compare the two `DateTime` objects, and output the line with the more recent date.
Floor 27 Posted 2007-01-05 03:54 ·  中国 广东 电信
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
Using FC to compare text will output a lot of useless distracting information, and it will be very tedious to extract useful information. It is not recommended to use it for comparison.

Still that question: In different list.txt, are each record one-to-one corresponding, just with different times? If so, then it is very easy.
尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。
Floor 28 Posted 2007-01-05 05:16 ·  中国 广西 南宁 联通
初级用户
Credits 54
Posts 25
Joined 2006-09-29 02:15
19-year member
UID 63991
Status Offline
Originally posted by namejm at 2007-1-5 03:54:
  Comparing text with FC will output a lot of useless interference information, and it will be very tedious to extract useful information. It is not recommended to use it for comparison.

  In different list.txt, are each record one-to-one corresponding, just with different times? If so, then it is very easy.



As you said,

In different list.txt, each record is one-to-one corresponding. Just the times are different.

How to solve it???


listA.txt
---------------------------------
c:\my\ab.exe 2006.10.26
d:\cde.com 2007.1.1
c:\f.bat 2007.1.3
---------------------------------



listB.txt
---------------------------------
c:\my\ab.exe 2007.10.26
d:\cde.com 2007.1.1
c:\f.bat 2007.1.3
---------------------------------





  

[ Last edited by gyfhgyfh on 2007-1-5 at 05:28 AM ]
Floor 29 Posted 2007-01-05 05:49 ·  中国 广东 电信
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
Here is given a demonstration code for displaying the different line contents of corresponding lines of two files, which is relatively inefficient:


@echo off
set num=0
for /f "delims=" %%i in ('findstr .* listA.txt') do call :comp_ "%%i"
pause
goto :eof

:comp_
for /f "delims=" %%i in ('more +%num% listB.txt') do (
if not "%%i"==%1 echo %1 与 "%%i" 不同
set /a num+=1
goto :eof
)
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
gyfhgyfh +1 2008-01-07 13:39
尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。
Floor 30 Posted 2007-01-05 06:07 ·  中国 广西 南宁 联通
初级用户
Credits 54
Posts 25
Joined 2006-09-29 02:15
19-year member
UID 63991
Status Offline
Thank you very much to namejm, jmz573515, ccwan for their help. Thanks.


@echo off
set num=0
for /f "delims=" %%i in ('findstr .* old.txt ') do call :comp_ "%%i"
goto :eof

:comp_
for /f "delims=" %%i in ('more +%num% new.txt ') do (
if not "%%i"==%1 echo %%i> list.txt
set /a num+=1
goto :eof
)



[ Last edited by gyfhgyfh on 2007-1-5 at 06:11 AM ]
Forum Jump: