简体   繁体   English

在Windows命令提示符下比较

[英]Compare in windows command prompt

Description: 描述:
I want to use a syntax like COMP I:\\folder1 Z:\\folder2 to compare all files in my I drive with the content of my z drive. 我想使用COMP I:\\ folder1 Z:\\ folder2这样的语法来比较I驱动器中的所有文件和z驱动器的内容。 I only need to compare their names to see if one exsists in the other. 我只需要比较他们的名字,看看一个人是否存在。 I need to recurse into the subdirectories because there are many located in both drives, I understand I need to use a batch script using a FOR loop and the PUSHD and POPD command. 我需要递归到子目录,因为两个驱动器中都有许多子目录,我了解我需要使用一个使用FOR循环以及PUSHD和POPD命令的批处理脚本。

QUESTION: 题:
How do I do this? 我该怎么做呢?

From the output from FOR /? 从FOR /的输出

FOR /R [[drive:]path] %variable IN (set) DO command [command-parameters]

    Walks the directory tree rooted at [drive:]path, executing the FOR
    statement in each directory of the tree.  If no directory
    specification is specified after /R then the current directory is
    assumed.  If set is just a single period (.) character then it
    will just enumerate the directory tree.

So you'd do something like 所以你会做类似的事情

@echo off
setlocal enabledelayedexpansion ENABLEEXTENSIONS

for /R P:\ %%F in (*.*) DO (
set fileFull=%%~fF
set filePath=%%~pF
set fileDrive=%%~dF
set fileName=%%~nF
set fileExtension=%%~xF
call :checker "!filePath!" "!fileName!" "!fileExtension!"
) 
goto :eof

:checker
set fileTarget="c:%~1%~2%~3"
if not exist %fileTarget% echo %fileTarget% not found
goto :eof

In this case, the script is getting all the filenames in P:\\ and its subdirectories and telling me if the file doesn't exist on the same path in C: 在这种情况下,脚本将获取P:\\及其子目录中的所有文件名,并告诉我该文件是否不存在于C中的同一路径中:

List folder differences over a tree: 列出一棵树上的文件夹差异:

This uses robocopy to create the list - don't remove /L as this switch makes robocopy only list the information. 这使用robocopy创建列表- 不要删除 /L因为此开关使robocopy仅列出信息。

robocopy "d:\source\folder one" "c:\target\folder two" /L /fp /njs /njh /ndl /ns /np /mir

If you really need to avoid third party utilities: 如果您确实需要避免使用第三方实用程序:

@ECHO OFF
SET FOLDER1=I:\Folder1\
SET FOLDER2=Z:\Folder2\
ECHO SET FNAME=%%1 ^& @ECHO %%FNAME:%FOLDER1%=%%^>^>FILES1.TXT>FILES1.BAT
ECHO SET FNAME=%%1 ^& @ECHO %%FNAME:%FOLDER2%=%%^>^>FILES2.TXT>FILES2.BAT
IF EXIST FILES1.TXT DEL FILES1.TXT
IF EXIST FILES2.TXT DEL FILES2.TXT   
FOR /F "tokens=*" %%* IN ('DIR /S /B /ON "%FOLDER1%"') DO CALL FILES1.BAT "%%*"
FOR /F "tokens=*" %%* IN ('DIR /S /B /ON "%FOLDER2%"') DO CALL FILES2.BAT "%%*"
ECHO Files from "%FOLDER1%" which are not found in "%FOLDER2%"
FOR /F "tokens=*" %%* IN (FILES1.TXT) DO (FIND %%* FILES2.TXT >NUL || ECHO %%*)
ECHO Files from "%FOLDER2%" which are not found in "%FOLDER1%"
FOR /F "tokens=*" %%* IN (FILES2.TXT) DO (FIND %%* FILES1.TXT >NUL || ECHO %%*)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM