8

我有两个包含所有相同文件和子文件夹的文件夹,但每个文件中的内容可能已更改。我想编写一个批处理文件,它将搜索每个文件并查找任何差异。我想做的最好的工具是什么?

4

4 回答 4

12

不需要批处理文件。一个 FC 命令可以做你想做的事:

fc folder1\* folder2\*

如果需要,您可以更具体地指定第一个文件夹中的文件掩码。例如folder1\*.txt.

该命令将报告文件夹 1 中存在但文件夹 2 中缺少的文件。folder2 中的额外文件将被忽略。

FC 命令有许多选项。输入HELP FCFC /?从命令提示符获取更多信息。

编辑

扩展解决方案以支持子文件夹有点棘手。使用 FOR /R 很容易迭代给定根目录的文件夹层次结构。问题是获取相对路径,以便可以将层次结构应用于另一个根。

最简单的解决方案是改用 FORFILES,因为它直接支持相对路径。但 FORFILES 是......慢:/

此时,批处理文件是有意义的:

@echo off
setlocal
set "folder1=c:\path\To\Folder1\Root"
set "folder2=d:\path\To\Folder2\Root"
set "fileMask=*"

for /f "delims=" %%F in (
  'echo "."^&forfiles /s /p "%folder1%" /m "%fileMask%" /c "cmd /c if @isdir==TRUE echo @relpath"'
) do fc "%folder1%\%%~F\%fileMask%" "%folder2%\%%~F\*"
于 2013-07-11T19:51:41.033 回答
1

我对以下批处理文件有更好的运气。路径名称必须完全匹配;所以只有驱动器号不同。

映射共享网络文件夹可能会使这更容易。对于你的情况,我不能肯定地说。

'--- setlocal set "folder1=D:\User\Public" set "Drv2=E:" set "fileMask=*"

setlocal
 set "folder1=<DrvLttr>:\<Folder>\<SubFolder>"
 set "Drv2=<DrvLttr2>:"
 set "LogFile=<SystemDrv>\User\<UserName>\<LogFileName>"

ECHO "the '>' may OVER WRITE or make a ~NEW~ File for the results" > "%LogFile%"
ECHO " '>>' adds to the end of the file >> "%LogFile%"

FOR /R %folder1% %%A in ( *.* ) DO FC "%%A" "%Drv2%%%~pnxA" 1>> "%LogFile%" 2>&1

如果您想注意测试命令是什么,您可以尝试在 DO 之后插入 ECHO。您可以删除 1>>... 内容以在屏幕上查看结果,而不必打开输出文件。

于 2015-07-25T08:34:46.550 回答
1

我修改了我为 CD 进程编写的批处理文件,它应该可以满足您的需要。它

  • 取 2 个目录树并比较每棵树中的每个文件
  • 创建文件差异列表(在输出文件夹中命名为 File_differences.txt)
  • 并为每个不匹配的对象创建一个包含差异文件的文件夹

如果两个目录结构都在同一个父目录下,那么您需要做的就是更新脚本中的前 8 个变量。如果目录树有不同的父目录,则通读脚本注释。特别是第 14、38 和 46 行。

:: This script compares the contents of 2 trees
:: set a workspace location for the script outside of the trees being reviewed
set home=D:\path\to\batch_file_home
set Input=D:\path\to\batch_file_home\Input_Files
set Resource=D:\path\to\batch_file_home\Resource_Files
set Output=D:\path\to\where your want to view your\Output_Files

set environment=D:\path\to\parent directory containing the different tree structures (if they do not share a parent, then make this the drive)
:: the next 3 lines are only needed if you want to predefine multiple directories for comparison 
set Prod=production
set QA=test
set Dev=branches\dev
:: If you remove the 3 lines above, then you need to replace the 2 below variables with values
:: If the trees are not under the same parent, then include the full path for Tree A and B below
set Tree_A=%Prod%
set Tree_B=%QA%
:: if you already have an object list, place it in the Input folder and remove lines 24 through 35 of this script
set Object_List=Object_List_minus_Direcotries.txt
set Output_File=File_differences.txt

if exist %Output%\%Output_File% del %Output%\%Output_File%
if exist %Output%\Differences\*.txt del %Output%\Differences\*.txt
if exist %Resource%\* del /q %Resource%\*

:: since you state the objects in both trees are always the same, I have not included a comparison to verify the 2 trees match
:: this section identifies the contents of Tree A
cd %environment%\%Tree_A%
dir /b /s > %Resource%\Complete_Tree_A_Object_List.txt

:: Next, remove the objects that are directories
setlocal enabledelayedexpansion
for /f "tokens=*" %%p in (%Resource%\Complete_Tree_A_Object_List.txt) do (
  dir /a:d /b %%p 2>nul >nul && (set object_type=folder) || (set object_type=file)
  echo !object_type!
  if !object_type!==file echo %%p >> %Resource%\%Object_List%
  )

:: in the object list, remove the parent tree from the path
:: if the Trees are not under the same parent, then comment out the below 6 lines
powershell -command "(Get-Content %Resource%\%Object_List%) -replace '\\','/' | set-content %Resource%\%Object_List%"
powershell -command "(get-content %Resource%\%Object_List%) | Foreach {$_.TrimEnd()} | Set-Content %Resource%\%Object_List%"
set remove_parent_prefix=%environment%\%Tree_A%
set remove_parent_prefix=%remove_parent_prefix:\=/%
powershell -command "(Get-Content %Resource%\%Object_List%) -replace '%remove_parent_prefix%/','' | set-content %Resource%\%Object_List%"
powershell -command "(Get-Content %Resource%\%Object_List%) -replace '/','\' | set-content %Resource%\%Object_List%"

:: the below loop assumes both Trees are under the same parent.  If this is not the case, replace the cd %environment% line with cd %home%
:: when the Trees are not under the same parent, set home to the root location, example cd D:\ 
setlocal enabledelayedexpansion
for /f "tokens=*" %%x in (%Resource%\%Object_List%) do (
  set Diff_File=%%x
  set Diff_File=!Diff_File:\=-!
  cd %environment%
  fc %Tree_A%\%%x %Tree_B%\%%x > "%Output%\Differences\!Diff_File!-%Output_File%"
  for %%a in ("%Output%\Differences\!Diff_File!-%Output_File%") do for /f %%b in ('find /c /v "" ^< "%%a" ') do if %%b LSS 3 del "%%a"  
  for %%R in ("%Output%\Differences\!Diff_File!-%Output_File%") do if not %%~zR lss 1 (
   echo %%x >> %Output%\%Output_File%
  )
  for %%R in ("%Output%\Differences\!Diff_File!-%Output_File%") do if %%~zR lss 1 (
     del "%Output%\Differences\!Diff_File!-%Output_File%"
  )
)
endlocal

:: Clean up Resources.  If you want to review the temp files used to create the report, comment out the below line
if exist %Resource%\* del /q %Resource%\*
于 2017-12-05T21:59:43.940 回答
1

这是完成任务的另一种方法。将变量Folder1Folder2设置为要比较并运行批处理文件的文件夹的完整路径。

输出:

Dup - 该文件存在于两个文件夹中并且是相同的。

Dif - 文件存在于两个文件夹中,但文件的内容不同。

新建– 文件存在于一个文件夹中,但不存在于另一个文件夹中。

@Echo Off
SetLocal EnableDelayedExpansion

Set "Folder1=%UserProfile%\Desktop\Test Folder1"
Set "Folder2=%UserProfile%\Desktop\Test Folder2"

For /R "%Folder1%" %%x In (*.*) Do (

    Set "FullPath=%%x"
    Set "RelPath=!FullPath:%Folder1%=!"

    If Exist "%Folder2%!RelPath!" (
      >Nul 2>&1 FC /b "%Folder1%!RelPath!" "%Folder2%!RelPath!" && (
       Echo Dup - %%x
      )||(
       Echo Dif - %%x
      )
    ) Else (
      Echo New - %%x
    )
)

For /R "%Folder2%" %%x In (*.*) Do (

    Set "FullPath=%%x"
    Set "RelPath=!FullPath:%Folder2%=!"

    If Not Exist "%Folder1%!RelPath!" (
      Echo New - %%x
    )
)
于 2020-02-27T09:39:01.753 回答