比较两个文件夹及其子文件夹批处理文件

比较两个文件夹及其子文件夹批处理文件

本文介绍了比较两个文件夹及其子文件夹批处理文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have two folders that contain all the same files and subfolders, but the conents inside each file may have changed. I want to write a batch file that will search through each file and look for any differences. What's the best tool for what I want to do?

推荐答案

无需批处理文件.单个FC命令可以执行您想要的操作:

No need for a batch file. A single FC command can do what you want:

fc folder1\* folder2\*

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

You can be more specific for the file mask in the first folder if you want. For example folder1\*.txt.

该命令将报告在folder1中存在但在folder2中丢失的文件.文件夹2中的多余文件将被忽略.

The command will report on files that exist in folder1 but are missing in folder2. Extra files in folder2 are simply ignored.

FC命令有许多选项.在命令提示符下输入HELP FCFC /?以获取更多信息.

There are a number of options to the FC command. Enter HELP FC or FC /? from the command prompt to get more information.

编辑

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

Extending the solution to support subfolders is a bit tricky. It is easy to iterate the folder hierarchy for a given root using FOR /R. The problem is getting the relative paths so that the hierarchy can be applied to another root.

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

The simplest solution is to use FORFILES instead, since it directly supports relative paths. but FORFILES is... S L O W :/

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

At this point, a batch file makes sense:

@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\*"

这篇关于比较两个文件夹及其子文件夹批处理文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 22:38