|
namejm
荣誉版主
       batch fan
积分 5226
发帖 1737
注册 2006-3-10 来自 成都
状态 离线
|
『第 16 楼』:
使用 LLM 解释/回答一下
为了让你看到效果,做个演示代码:
@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
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
|

尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。 |
|
2007-1-4 00:07 |
|
|
jmz573515
银牌会员
    
积分 1212
发帖 464
注册 2006-12-13
状态 离线
|
『第 17 楼』:
使用 LLM 解释/回答一下
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,"操作完成"
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,"操作完成"
|
|
2007-1-4 00:22 |
|
|
gyfhgyfh
初级用户
 
积分 54
发帖 25
注册 2006-9-29
状态 离线
|
『第 18 楼』:
使用 LLM 解释/回答一下
多谢各位,终于明白了。
祝大家新年快乐!!!
Thanks everyone, finally understand.
Wish everyone a happy new year!!!
|
|
2007-1-4 00:40 |
|
|
ccwan
金牌会员
     
积分 2725
发帖 1160
注册 2006-9-23 来自 河北廊坊
状态 离线
|
『第 19 楼』:
使用 LLM 解释/回答一下
test.txt内容如下:
XXXXX...=***...
XXXX...=.../
XXX...=...
...
XXX...=...
FileName=123
XXX...=...
Path=c:\
...
...
FileName=456
...
Path=d:\
FileName=789
...
Path=e:\
...
my.bat代码如下:
@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="
### 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="
|

三人行,必有吾师焉。 学然后知不足,教然后知困,然后能自强也。 |
|
2007-1-4 04:36 |
|
|
ccwan
金牌会员
     
积分 2725
发帖 1160
注册 2006-9-23 来自 河北廊坊
状态 离线
|
『第 20 楼』:
使用 LLM 解释/回答一下
运行结果
my.txt内容如下:
c:\123
d:\456
e:\789
Running result
The content of my.txt is as follows:
c:\123
d:\456
e:\789
|

三人行,必有吾师焉。 学然后知不足,教然后知困,然后能自强也。 |
|
2007-1-4 04:39 |
|
|
gyfhgyfh
初级用户
 
积分 54
发帖 25
注册 2006-9-29
状态 离线
|
『第 21 楼』:
使用 LLM 解释/回答一下
多谢 ccwan ,这次给出的脚本已经非常完善了。
脚本的功能真是强大!
再次感谢各位。
Thanks to ccwan, the script provided this time is already very complete.
The function of the script is really powerful!
Thanks again to everyone.
|
|
2007-1-4 05:16 |
|
|
gyfhgyfh
初级用户
 
积分 54
发帖 25
注册 2006-9-29
状态 离线
|
『第 22 楼』:
使用 LLM 解释/回答一下
这个该如何解决???
提取 text.txt
----------------------------------------------
ab.exe
2006.10.26
c:\my\
cde.com
2007.1.1
d:\
f.bat
2007.1.3
c:\
...
-----------------------------------------------
输出为 list.txt
----------------------------------------------
c:\my\ab.exe 2006.10.26
d:\cde.com 2007.1.1
c:\f.bat 2007.1.3
...
-----------------------------------------------
然后通过比较不同的 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
...
--------------------------------------------------------
即
--------------------------------------------------------
c:\my\ab.exe 2007.10.26
...
--------------------------------------------------------
Last edited by gyfhgyfh on 2007-1-5 at 02:38 AM ]
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 ]
|
|
2007-1-5 02:21 |
|
|
jmz573515
银牌会员
    
积分 1212
发帖 464
注册 2006-12-13
状态 离线
|
『第 23 楼』:
使用 LLM 解释/回答一下
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 "提取内容成功!请查看my.txt文件。",48+4096,"操作完成"
```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"
```
|
|
2007-1-5 03:00 |
|
|
jmz573515
银牌会员
    
积分 1212
发帖 464
注册 2006-12-13
状态 离线
|
『第 24 楼』:
使用 LLM 解释/回答一下
没有明白你说的“然后通过比较不同的 list.txt 提取出最新的文件列表”是什么意思?
I didn't understand what you meant by "then extract the latest file list by comparing different list.txt".
|
|
2007-1-5 03:06 |
|
|
namejm
荣誉版主
       batch fan
积分 5226
发帖 1737
注册 2006-3-10 来自 成都
状态 离线
|
『第 25 楼』:
使用 LLM 解释/回答一下
jmz573515 兄的代码不错。
若要用批处理来提取test.txt的内容,在16楼的演示代码上稍作修改即可。因为考虑到路径中有可能含有特殊字符,比如空格、&、!等符号,所以生成 list.txt 的时候,把路径用引号括起来了:
@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
至于比较两个文件的内容,则比较复杂,暂时还没想到怎样才能提取到最新的记录——如果两个list.txt中的记录是一一对应的、只是时间有所不同,则比较好办一点。
Last edited by namejm on 2007-1-4 at 02:19 PM ]
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 ]
|

尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。 |
|
2007-1-5 03:18 |
|
|
gyfhgyfh
初级用户
 
积分 54
发帖 25
注册 2006-9-29
状态 离线
|
『第 26 楼』:
使用 LLM 解释/回答一下
A.txt
---------------------------------
c:\my\ab.exe 2006.10.26
d:\cde.com 2007.1.1
c:\f.bat 2007.1.3
---------------------------------
B.txt
---------------------------------
c:\my\ab.exe 2007.10.26
d:\cde.com 2007.1.1
c:\f.bat 2007.1.3
---------------------------------
fc A.txt B.txt > my.txt
---------------------------------
正在比较文件 A.txt 和 B.TXT
***** A.txt
c:\my\ab.exe 2006.10.26
d:\cde.com 2007.1.1
***** B.TXT
c:\my\ab.exe 2007.10.26
d:\cde.com 2007.1.1
*****
---------------------------------
如何只提取时间最近的
c:\my\ab.exe 2007.10.26
### 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(' ')
$dateB = $b.Split(' ')
$dateAObj = ::ParseExact($dateA, 'yyyy.MM.dd', $null)
$dateBObj = ::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.
|
|
2007-1-5 03:33 |
|
|
namejm
荣誉版主
       batch fan
积分 5226
发帖 1737
注册 2006-3-10 来自 成都
状态 离线
|
『第 27 楼』:
使用 LLM 解释/回答一下
用 FC 来比较文本,会输出很多无用的干扰信息,要想提取到有用的信息将十分烦琐,不推荐用它来做比较。
还是那个问题:不同的list.txt中,每条记录都是一一对应的、只是时间有所不同吗?如果是这样的话,那就十分好办了。
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没商量。
考虑问题复杂化,解决问题简洁化。 |
|
2007-1-5 03:54 |
|
|
gyfhgyfh
初级用户
 
积分 54
发帖 25
注册 2006-9-29
状态 离线
|
『第 28 楼』:
使用 LLM 解释/回答一下
Originally posted by namejm at 2007-1-5 03:54:
用 FC 来比较文本,会输出很多无用的干扰信息,要想提取到有用的信息将十分烦琐,不推荐用它来做比较。
不同的list.txt中,每条记录都是一一对应的、只是时间有所不同吗?如果是这样的话,那就十分好办了。
正如您所说,
不同的list.txt中,每条记录都是一一对应的.只是时间有所不同。
该如何解决???
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 ]
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 ]
|
|
2007-1-5 05:16 |
|
|
namejm
荣誉版主
       batch fan
积分 5226
发帖 1737
注册 2006-3-10 来自 成都
状态 离线
|
『第 29 楼』:
使用 LLM 解释/回答一下
给你一段演示代码,用于显示两个文件的对应行不同的行内容,效率比较低下:
@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
)
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
)
|

尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。 |
|
2007-1-5 05:49 |
|
|
gyfhgyfh
初级用户
 
积分 54
发帖 25
注册 2006-9-29
状态 离线
|
『第 30 楼』:
使用 LLM 解释/回答一下
非常感谢 namejm jmz573515 ccwan 的帮助。谢谢。
@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 ]
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 ]
|
|
2007-1-5 06:07 |
|