在Windows命令提示符下比较

时间:2014-03-05 16:37:54

标签: windows batch-file cmd

描述:
我想使用像COMP I:\ folder1 Z:\ folder2这样的语法来比较我的驱动器中的所有文件和我的z驱动器的内容。我只需要比较他们的名字,看看是否有一个人在另一个人中存在。我需要递归到子目录中,因为两个驱动器中都有很多,我知道我需要使用一个使用FOR循环和PUSHD和POPD命令的批处理脚本。

问题:
我该怎么做?

3 个答案:

答案 0 :(得分:0)

来自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.

所以你会做类似

的事情
@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

在这种情况下,脚本将获取P:\及其子目录中的所有文件名,并告诉我该文件在C中的同一路径上是否不存在:

答案 1 :(得分:0)

列出树上的文件夹差异:

这使用robocopy创建列表 - 不要删除 /L,因为此切换使robocopy仅列出信息。

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

答案 2 :(得分:0)

如果您确实需要避免第三方实用程序:

@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 %%*)