中国DOS联盟

-- 联合DOS 推动DOS 发展DOS --

联盟域名:www.cn-dos.net 论坛域名:www.cn-dos.net/forum
DOS,代表着自由开放与发展,我们努力起来,学习FreeDOS和Linux的自由开放与GNU精神,共同创造和发展美好的自由与GNU GPL世界吧!

中国DOS联盟论坛
现在时间是 2026-08-12 01:39
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » [已结]文件内容比对 查看 3,047 回复 21
楼 主 [已结]文件内容比对 发表于 2010-09-02 14:35 ·  中国 江苏 苏州 电信
初级用户
★★
积分 71
发帖 70
注册 2010-07-19 11:51
16年会员
UID 170757
性别 男
状态 离线
请教个问题,比如A文件的内容是:

MinimumPasswordAge = 0
MaximumPasswordAge = 42

AuditSystemEvents = 3
AuditLogonEvents = 3
AuditObjectAccess = 2
AuditPrivilegeUse = 2
AuditPolicyChange = 3
AuditAccountManage = 3
B文件的内容是:

MinimumPasswordAge = 0
LSAAnonymousNameLookup = 0
EnableAdminAccount = 1
EnableGuestAccount = 0

AuditPolicyChange = 3
AuditAccountManage = 3
如果把A文件中的内容作为标准内容,在B中查找那些与A相同项目的后面赋值,如果不一样则提示总的不符的个数以及哪些内容一致。
批处理如何实现,谢谢!

[ Last edited by coldface on 2010-9-12 at 15:41 ]
2 发表于 2010-09-02 14:49 ·  中国 江苏 苏州 电信
初级用户
★★
积分 71
发帖 70
注册 2010-07-19 11:51
16年会员
UID 170757
性别 男
状态 离线
补充下,我的文件里内容很多,而且比较长,
FC的话是没法的,试过了。
内容具体如下:

[Unicode]
Unicode=yes
[System Access]
MinimumPasswordAge = 0
MaximumPasswordAge = 42
MinimumPasswordLength = 0
PasswordComplexity = 0
PasswordHistorySize = 0
LockoutBadCount = 0
ResetLockoutCount = 0
LockoutDuration = 0
RequireLogonToChangePassword = 0
ForceLogoffWhenHourExpire = 0
ClearTextPassword = 0
LSAAnonymousNameLookup = 0
EnableAdminAccount = 1
EnableGuestAccount = 0
[Event Audit]
AuditSystemEvents = 3
AuditLogonEvents = 3
AuditObjectAccess = 2
AuditPrivilegeUse = 2
AuditPolicyChange = 3
AuditAccountManage = 3
AuditProcessTracking = 0
AuditDSAccess = 2
AuditAccountLogon = 3
[Registry Values]
MACHINE\Software\Microsoft\Driver Signing\Policy=3,1
MACHINE\Software\Microsoft\Windows NT\CurrentVersion\Setup\RecoveryConsole\SecurityLevel=4,0
MACHINE\Software\Microsoft\Windows NT\CurrentVersion\Setup\RecoveryConsole\SetCommand=4,0
MACHINE\Software\Microsoft\Windows NT\CurrentVersion\Winlogon\AllocateCDRoms=1,"0"
MACHINE\Software\Microsoft\Windows NT\CurrentVersion\Winlogon\AllocateDASD=1,"0"
MACHINE\Software\Microsoft\Windows NT\CurrentVersion\Winlogon\AllocateFloppies=1,"0"
MACHINE\Software\Microsoft\Windows NT\CurrentVersion\Winlogon\CachedLogonsCount=1,"10"
MACHINE\Software\Microsoft\Windows NT\CurrentVersion\Winlogon\ForceUnlockLogon=4,0
MACHINE\Software\Microsoft\Windows NT\CurrentVersion\Winlogon\PasswordExpiryWarning=4,14
MACHINE\Software\Microsoft\Windows NT\CurrentVersion\Winlogon\ScRemoveOption=1,"0"
MACHINE\Software\Microsoft\Windows\CurrentVersion\Policies\System\DisableCAD=4,1
MACHINE\Software\Microsoft\Windows\CurrentVersion\Policies\System\DontDisplayLastUserName=4,1
MACHINE\Software\Microsoft\Windows\CurrentVersion\Policies\System\LegalNoticeText=7,
MACHINE\Software\Microsoft\Windows\CurrentVersion\Policies\System\ScForceOption=4,0
MACHINE\Software\Microsoft\Windows\CurrentVersion\Policies\System\ShutdownWithoutLogon=4,0
MACHINE\Software\Microsoft\Windows\CurrentVersion\Policies\System\UndockWithoutLogon=4,1
MACHINE\Software\Policies\Microsoft\Windows\Safer\CodeIdentifiers\AuthenticodeEnabled=4,0
MACHINE\System\CurrentControlSet\Control\Lsa\AuditBaseObjects=4,0
MACHINE\System\CurrentControlSet\Control\Lsa\CrashOnAuditFail=4,0
MACHINE\System\CurrentControlSet\Control\Lsa\DisableDomainCreds=4,1
MACHINE\System\CurrentControlSet\Control\Lsa\EveryoneIncludesAnonymous=4,0
MACHINE\System\CurrentControlSet\Control\Lsa\FIPSAlgorithmPolicy=4,0
MACHINE\System\CurrentControlSet\Control\Lsa\ForceGuest=4,0
MACHINE\System\CurrentControlSet\Control\Lsa\FullPrivilegeAuditing=3,0
MACHINE\System\CurrentControlSet\Control\Lsa\LimitBlankPasswordUse=4,1
MACHINE\System\CurrentControlSet\Control\Lsa\LmCompatibilityLevel=4,2
MACHINE\System\CurrentControlSet\Control\Lsa\MSV1_0\NTLMMinClientSec=4,0
MACHINE\System\CurrentControlSet\Control\Lsa\MSV1_0\NTLMMinServerSec=4,0
MACHINE\System\CurrentControlSet\Control\Lsa\NoDefaultAdminOwner=4,0
MACHINE\System\CurrentControlSet\Control\Lsa\NoLMHash=4,1
MACHINE\System\CurrentControlSet\Control\Lsa\RestrictAnonymous=4,1
MACHINE\System\CurrentControlSet\Control\Lsa\RestrictAnonymousSAM=4,1
MACHINE\System\CurrentControlSet\Control\Print\Providers\LanMan Print Services\Servers\AddPrinterDrivers=4,1
MACHINE\System\CurrentControlSet\Control\SecurePipeServers\Winreg\AllowedExactPaths\Machine=7,
MACHINE\System\CurrentControlSet\Control\SecurePipeServers\Winreg\AllowedPaths\Machine=7,
MACHINE\System\CurrentControlSet\Control\Session Manager\Kernel\ObCaseInsensitive=4,1
MACHINE\System\CurrentControlSet\Control\Session Manager\Memory Management\ClearPageFileAtShutdown=4,0
MACHINE\System\CurrentControlSet\Control\Session Manager\ProtectionMode=4,1
MACHINE\System\CurrentControlSet\Control\Session Manager\SubSystems\optional=7,Posix
MACHINE\System\CurrentControlSet\Services\LanManServer\Parameters\NullSessionPipes=7,
MACHINE\System\CurrentControlSet\Services\LanManServer\Parameters\NullSessionShares=7,
MACHINE\System\CurrentControlSet\Services\LanmanWorkstation\Parameters\EnablePlainTextPassword=4,0
MACHINE\System\CurrentControlSet\Services\LanmanWorkstation\Parameters\EnableSecuritySignature=4,1
MACHINE\System\CurrentControlSet\Services\LanmanWorkstation\Parameters\RequireSecuritySignature=4,0
MACHINE\System\CurrentControlSet\Services\LDAP\LDAPClientIntegrity=4,1
MACHINE\System\CurrentControlSet\Services\Netlogon\Parameters\DisablePasswordChange=4,0
MACHINE\System\CurrentControlSet\Services\Netlogon\Parameters\MaximumPasswordAge=4,30
MACHINE\System\CurrentControlSet\Services\Netlogon\Parameters\RequireSignOrSeal=4,1
MACHINE\System\CurrentControlSet\Services\Netlogon\Parameters\RequireStrongKey=4,0
MACHINE\System\CurrentControlSet\Services\Netlogon\Parameters\SealSecureChannel=4,1
MACHINE\System\CurrentControlSet\Services\Netlogon\Parameters\SignSecureChannel=4,1
[Version]
signature="$CHICAGO$"
Revision=1
3 发表于 2010-09-07 10:16 ·  中国 江苏 苏州 电信
初级用户
★★
积分 71
发帖 70
注册 2010-07-19 11:51
16年会员
UID 170757
性别 男
状态 离线
这个还没想到方法,谁帮助下
4 发表于 2010-09-07 11:48 ·  中国 四川 南充 联通
高级用户
★★★
积分 609
发帖 374
注册 2006-08-02 22:38
20年会员
UID 59720
状态 离线
把你的真实文件上传
5 发表于 2010-09-07 12:53 ·  中国 江苏 苏州 电信
初级用户
★★
积分 71
发帖 70
注册 2010-07-19 11:51
16年会员
UID 170757
性别 男
状态 离线
上面那个就是文件里的原始内容了
6 发表于 2010-09-07 13:24 ·  中国 北京 263网络通信电信数据中心
铂金会员
★★★★
积分 5,212
发帖 2,478
注册 2007-02-08 23:39
19年会员
UID 79003
性别 男
状态 离线
for一遍全set 前导字符%%i

再for另一个文件 delims==
if %%j==!前导字符%%i! ... else ...
S smile 微笑,L love 爱,O optimism 乐观,R relax 放松,E enthusiasm 热情...Slore
7 发表于 2010-09-07 14:11 ·  中国 江苏 苏州 电信
初级用户
★★
积分 71
发帖 70
注册 2010-07-19 11:51
16年会员
UID 170757
性别 男
状态 离线
延伸下,首先比对=前面的字符串,如果一样则比对=后面的数值是否一致。
如果=前面的字符串不一样则把B的此行写入一个文件C。

按照slore的提示,我写前面的字符串比对:

@echo off&setlocal ENABLEDELAYEDEXPANSION
for /f %%i in (A.txt) do (
set str=%%i
for /f "tokens=1 delims==" %%k in (B.txt) do (
if "%%k"=="!str:~1!" echo "It is the same" else echo %%k
)
)
但是运行没结果,说明在字符串比较方面有问题啊?

[ Last edited by coldface on 2010-9-7 at 14:54 ]
8 发表于 2010-09-07 16:48 ·  中国 北京 263网络通信电信数据中心
铂金会员
★★★★
积分 5,212
发帖 2,478
注册 2007-02-08 23:39
19年会员
UID 79003
性别 男
状态 离线
两个循环不是嵌套的。。。是顺序的。
S smile 微笑,L love 爱,O optimism 乐观,R relax 放松,E enthusiasm 热情...Slore
9 发表于 2010-09-07 17:47 ·  中国 江苏 苏州 电信
初级用户
★★
积分 71
发帖 70
注册 2010-07-19 11:51
16年会员
UID 170757
性别 男
状态 离线
那这个不知道怎么写了
@echo off&setlocal ENABLEDELAYEDEXPANSION
for /f %%i in (3.txt) do set str1=%%i
for /f "tokens=1* delims==" %%k in (4.txt) do
(
if "%%k"=="!str1!"....
10 发表于 2010-09-07 20:58 ·  中国 江苏 苏州 电信
初级用户
★★
积分 71
发帖 70
注册 2010-07-19 11:51
16年会员
UID 170757
性别 男
状态 离线
帮忙下
谢谢
11 发表于 2010-09-07 22:57 ·  中国 陕西 西安 联通
铂金会员
★★★★
积分 5,212
发帖 2,478
注册 2007-02-08 23:39
19年会员
UID 79003
性别 男
状态 离线
@echo off&setlocal ENABLEDELAYEDEXPANSION
for /f "eol=[ delims=" %%i in (1.txt) do set "OLD_%%i"
for /f "eol=[ tokens=1,2 delims==" %%i in (2.txt) do (
if "!OLD_%%i!"=="" (
echo %%i 为新属性
) else (
if not "%%j"=="!OLD_%%i!" (
echo %%i=%%j 不同与 !OLD_%%i!
)
)
)
pause


大致如这般,你自己看着改吧。
S smile 微笑,L love 爱,O optimism 乐观,R relax 放松,E enthusiasm 热情...Slore
12 发表于 2010-09-07 22:58 ·  中国 陕西 西安 联通
铂金会员
★★★★
积分 5,212
发帖 2,478
注册 2007-02-08 23:39
19年会员
UID 79003
性别 男
状态 离线
建议用比较专业的比较工具,比如Beyond Compare什么的,带命令行也是很方便很强大的
S smile 微笑,L love 爱,O optimism 乐观,R relax 放松,E enthusiasm 热情...Slore
13 发表于 2010-09-08 11:44 ·  中国 江苏 苏州 电信
初级用户
★★
积分 71
发帖 70
注册 2010-07-19 11:51
16年会员
UID 170757
性别 男
状态 离线
恩 ,谢谢,你那段如果是检查短的字符的话没有问题。
但是文件里包含一些比较长的内容:

[Registry Values]
MACHINE\Software\Microsoft\Windows\CurrentVersion\Policies\System\DisableCAD=4,1
MACHINE\Software\Microsoft\Windows\CurrentVersion\Policies\System\DontDisplayLastUserName=4,1
MACHINE\Software\Microsoft\Windows\CurrentVersion\Policies\System\LegalNoticeText=7,1
这样的话就无法比较了。
关于用BC或者UC,这些关键对平台有一定的依赖性,不能很好的做“移植”,其实我做这个的初衷是对每台主机的本地安全策略与标准的安全策略做比较的
14 发表于 2010-09-08 12:00 ·  中国 北京 263网络通信电信数据中心
铂金会员
★★★★
积分 5,212
发帖 2,478
注册 2007-02-08 23:39
19年会员
UID 79003
性别 男
状态 离线
长的可以。。。

我估计是你文件太长了?CMD变量可能有个数限制。。

你复制你上面13L的内容保存为1.txt
复制一份为2.txt
然后随便改下值。。。

你运行BC12.BAT你看下结果。
S smile 微笑,L love 爱,O optimism 乐观,R relax 放松,E enthusiasm 热情...Slore
15 发表于 2010-09-08 14:04 ·  中国 江苏 苏州 电信
初级用户
★★
积分 71
发帖 70
注册 2010-07-19 11:51
16年会员
UID 170757
性别 男
状态 离线
恭喜你答对,是我文件太长,我那会是截取了一部分做的比对。
变量个数的最多数量可以修改么?
论坛跳转: