如何做二进制文件比较文件递归在2个文件夹路径使用cmd? [英] How to do Binary comparison of files recursively within 2 folder paths using cmd?
问题描述
我需要编写一个脚本来递归地对2个文件夹内的所有文件进行二进制比较。 2个文件夹是安装文件夹,包含相同的文件,但它们是不同版本的安装文件夹。我需要找到哪些文件( .dll
, .lib
, .exe
)相对于以前的版本已经改变。
I need to write a script for binary comparison of all files within 2 folders recursively. The 2 folders are installation folders and contain same files, but they are installation folders of different versions. I need to find which files (.dll
, .lib
, .exe
) have changed with regard to previous versions.
我已尝试使用 fc
命令
fc /b %1\* %2\* > result.txt
但是,它仅比较指定文件夹中的文件。我需要递归地比较所有文件夹中的所有文件。
But, it compares files only inside the stated folder. I need to recursively compare all files within all folders.
我认为这可以通过为
循环来实现。
I thought this can be achieved by for
loop.
For /r C:\test\%%f in (*) do @fc /b %%f C:\test\%2\%%~nxf > result.txt
这里的问题是 %%〜nxf
它只给出文件名而不是相对路径。
The problem here is %%~nxf
which only gives the file name not the relative path.
我尝试使用 forfiles
命令:
forfiles /s /p C:\test\%1 /m * /c "cmd /c @fc /b %1\@relpath %2\@relpath"
@relpath
介绍。 \
在路径的中间,这是搞乱我的完整路径。
@relpath
introduces .\
in the middle of the path, which is messing up my complete path. Any pointers on this one will be highly appreciated.
请帮助!
推荐答案
解决方法可能是使用subst将路径与驱动器号关联,并比较两者。例如:
A workaround could be to use subst to associate the path with a drive letter, and compare both. For example:
subst Y: "C:\test 1"
subst Z: "C:\test 2"
for /r Y: %%f in (*) do fc /b "%%f" "Z:%%~pnxf"
subst Y: /d
subst Z: /d
希望这有助于
EDIT:处理fc命令输出(long):
Handle the fc command output (long):
setlocal enabledelayedexpansion
subst Y: /d
subst Z: /d
pause
subst Y: "C:\test 1"
subst Z: "C:\test 2"
for /r Y: %%f in (*) do (
set diff=1&for /f "tokens=1,2,3,4 delims= " %%i in ('fc /b "%%f" "Z:%%~pnxf"') do (
if "%%i"=="Comparing" (echo.) else (
if "%%k"=="longer" (echo LONGER : %%j) else (
if "%%i"=="FC:" echo NO DIFF : %%f
if "%%l"=="" (if "!diff!"=="1" (
echo DIFFERENT: %%f
echo %%i %%j %%k
set diff=2
))
)
)
)
)
subst Y: /d
subst Z: /d
这篇关于如何做二进制文件比较文件递归在2个文件夹路径使用cmd?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!