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-20 08:02
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » How to compare the differences between two text contents View 2,852 Replies 11
Original Poster Posted 2006-08-31 02:07 ·  中国 广东 佛山 电信
初级用户
Credits 74
Posts 31
Joined 2006-08-26 03:38
19-year member
UID 61291
Status Offline
If the content in 1.txt is the same as that in 2.txt, replace it with blank.

1.txt
c:\windows
c:\windows\12.txt

2.txt
c:\windows
c:\windows\21.txt
c:\windows\123.txt
c:\my doc

The output is 3.txt
c:\windows\123.txt
c:\my doc

[ Last edited by namejm on 2007-2-5 at 10:24 AM ]
Floor 2 Posted 2006-08-31 02:38 ·  中国 四川 成都 鹏博士宽带
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
There was a similar discussion post before, but I can't find it at once. So I simply post the previously sent complete code for your reference:

@echo off
:: Compare the differences in file names between folder A and folder B (there are no sub - directories under both folder A and folder B)
:: If there are differences, write the different ones in folder A to a.txt and the different ones in folder B to b.txt, and write the same part of file names to c.txt
cd.>a.txt
cd.>b.txt
cd.>c.txt
dir /a-d /b "Path of folder A">list_a.txt
dir /a-d /b "Path of folder B">list_b.txt
for /f "delims=" %%i in (list_a.txt) do (
find "%%i" list_b.txt>nul 2>nul && echo %%i>>c.txt || echo %%i>>a.txt
)
for /f "delims=" %%i in (list_b.txt) do (
find "%%i" list_a.txt>nul 2>nul || echo %%i>>b.txt
)
del /q list_a.txt>nul 2>nul
del /q list_b.txt>nul 2>nul
start a.txt
start b.txt
start c.txt
尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。
Floor 3 Posted 2006-08-31 02:45 ·  中国 四川 成都 鹏博士宽带
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
Considering that you are a newbie and it's hard for you to understand the complete code in building 2, here is a code customized for you:

@echo off
cd.>3.txt
for /f "tokens=*" %%i in (2.txt) do (
find "%%i" 1.txt || >>3.txt echo %%i
)
start 3.txt
尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。
Floor 4 Posted 2006-08-31 08:32 ·  中国 四川 成都 鹏博士宽带
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
Today I had nothing to do, so I pulled out the code from the third floor to play with. The result surprised me a lot: the content of 3.txt is
c:\windows\21.txt
c:\windows\123.txt
c:\my doc

instead of what the original poster said
c:\windows\123.txt
c:\my doc
.

Oh my god, I'm having another setback. Alas, the original posters are really hard to deal with now.
尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。
Floor 5 Posted 2006-08-31 15:36 ·  中国 广东 江门 电信
初级用户
Credits 74
Posts 31
Joined 2006-08-26 03:38
19-year member
UID 61291
Status Offline
Thanks everyone, but I tried this method and some can't be replaced. For example, the last name is LNK,

for /f "delims==" %%i in (1.txt) do findstr /l /i /x "%%i" "2.txt" ||if %errorlevel% == 0 echo %%i >>3.txt
Floor 6 Posted 2006-08-31 15:39 ·  中国 甘肃 张掖 电信
金牌会员
★★★★
Credits 4,103
Posts 1,744
Joined 2006-01-20 13:00
20-year member
UID 49241
Gender Male
From 甘肃.临泽
Status Offline
>>3.txt echo %%i

The one above can also be written in reverse, why?
Floor 7 Posted 2006-09-04 13:09 ·  中国 广东 江门 鹤山市 电信
初级用户
Credits 74
Posts 31
Joined 2006-08-26 03:38
19-year member
UID 61291
Status Offline
So far, no one has been able to solve it.
Floor 8 Posted 2006-09-04 13:31 ·  中国 四川 成都 鹏博士宽带
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
Originally posted by hhh246 at 2006-9-4 13:09:
There is still no one who can solve it yet,


  Really?

  Have you tested the above code by yourself? Can you ensure that there are no errors in your statement in the top post?
尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。
Floor 9 Posted 2006-09-08 08:52 ·  中国 广东 江门 蓬江区 电信
初级用户
Credits 74
Posts 31
Joined 2006-08-26 03:38
19-year member
UID 61291
Status Offline
for /f "delims==" %%i in (1.log) do (
find /i "%%i" "2.log" || echo %%i>>b.txt
)

It works, but it's quite slow. Can anyone write a multi-threaded one?
Floor 10 Posted 2006-09-08 11:24 ·  中国 广东 佛山 广东睿江科技有限公司
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
Originally posted by hhh246 at 2006-9-8 08:52:
for /f "delims==" %%i in (1.log) do (
find /i "%%i" "2.log" || echo %%i>>b.txt
)

It works, but the speed is quite slow. Can anyone write a multithreaded one?


  Is the speed slow? Unless you are dealing with tens of thousands of lines of text.

  Multithreaded? Hehe, CMD is in single-task mode, so it's impossible to implement multithreading.

[ Last edited by namejm on 2006-9-8 at 11:26 ]
尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。
Floor 11 Posted 2006-09-11 00:08 ·  中国 湖北 荆门 电信
荣誉版主
★★★
Credits 2,013
Posts 718
Joined 2006-02-18 07:07
20-year member
UID 50550
Status Offline
findstr /v /x /g:1.txt 2.txt
Floor 12 Posted 2006-09-18 21:19 ·  中国 广东 佛山 广东睿江科技有限公司
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
The code of moderator 3742668 is extremely concise and efficient, PF.
尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。
Forum Jump: