![]() |
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-11-10 19:17 |
47,815 topics / 349,930 posts / today 0 new / 48,291 members |
| DOS批处理 & 脚本技术(批处理室) » How to read a text file in the following format with a script? [Solved, thanks to everyone.] |
| Printable Version 6,088 / 33 |
| Floor16 namejm | Posted 2007-01-04 00:07 |
| 荣誉版主 Posts 1,737 Credits 5,226 From 成都 | |
|
To let you see the effect, make a demo code:
|
|
| Floor17 jmz573515 | Posted 2007-01-04 00:22 |
| 银牌会员 Posts 464 Credits 1,212 | |
|
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,"操作完成" |
|
| Floor18 gyfhgyfh | Posted 2007-01-04 00:40 |
| 初级用户 Posts 25 Credits 54 | |
|
Thanks everyone, finally understand.
Wish everyone a happy new year!!! |
|
| Floor19 ccwan | Posted 2007-01-04 04:36 |
| 金牌会员 Posts 1,160 Credits 2,725 From 河北廊坊 | |
|
### test.txt content is as follows:
### my.bat code is as follows: |
|
| Floor20 ccwan | Posted 2007-01-04 04:39 |
| 金牌会员 Posts 1,160 Credits 2,725 From 河北廊坊 | |
|
Running result
The content of my.txt is as follows: |
|
| Floor21 gyfhgyfh | Posted 2007-01-04 05:16 |
| 初级用户 Posts 25 Credits 54 | |
|
Thanks to ccwan, the script provided this time is already very complete.
The function of the script is really powerful! Thanks again to everyone. |
|
| Floor22 gyfhgyfh | Posted 2007-01-05 02:21 |
| 初级用户 Posts 25 Credits 54 | |
|
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 ] |
|
| Floor23 jmz573515 | Posted 2007-01-05 03:00 |
| 银牌会员 Posts 464 Credits 1,212 | |
|
```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" ``` |
|
| Floor24 jmz573515 | Posted 2007-01-05 03:06 |
| 银牌会员 Posts 464 Credits 1,212 | |
|
I didn't understand what you meant by "then extract the latest file list by comparing different list.txt".
|
|
| Floor25 namejm | Posted 2007-01-05 03:18 |
| 荣誉版主 Posts 1,737 Credits 5,226 From 成都 | |
|
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: 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 ] |
|
| Floor26 gyfhgyfh | Posted 2007-01-05 03:33 |
| 初级用户 Posts 25 Credits 54 | |
|
### 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. |
|
| Floor27 namejm | Posted 2007-01-05 03:54 |
| 荣誉版主 Posts 1,737 Credits 5,226 From 成都 | |
|
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. |
|
| Floor28 gyfhgyfh | Posted 2007-01-05 05:16 |
| 初级用户 Posts 25 Credits 54 | |
Originally posted by namejm at 2007-1-5 03:54: 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 ] |
|
| Floor29 namejm | Posted 2007-01-05 05:49 |
| 荣誉版主 Posts 1,737 Credits 5,226 From 成都 | |
|
Here is given a demonstration code for displaying the different line contents of corresponding lines of two files, which is relatively inefficient:
|
|
| Floor30 gyfhgyfh | Posted 2007-01-05 06:07 |
| 初级用户 Posts 25 Credits 54 | |
|
Thank you very much to namejm, jmz573515, ccwan for their help. Thanks.
[ Last edited by gyfhgyfh on 2007-1-5 at 06:11 AM ] |
|
| Prev 1 2 3 Next |
|
[ Contact the Union admin team -
中国DOS联盟 -
Standard version ] Sponsored by ifanr Inc | © 2001–2023 |