比较两个文件夹中的文件数

时间:2014-01-13 10:08:07

标签: windows batch-file

我想验证两个文件夹的文件数是否相同。

例如,如果文件夹c:\Users\abc\INBOX中有5个文件,我想验证文件夹c:\Users\abc\OUTBOX中还有5个文件

我怎样才能做到这一点?

3 个答案:

答案 0 :(得分:0)

尝试这样的事情:

@echo off

set $Folder1="c:\Users\abc\INBOX"
set $folder2="c:\Users\abc\OUTBOX"
set $count=1

setlocal EnableDelayedExpansion

for %%x in (%$Folder1% %$Folder2%) do (
for /f "tokens=1 delims= " %%a in ('dir %%x ^| find /i "File(s)"') do (
set $Total!$Count!=%%a)
set /a $Count+=1)

If %$Total1% Equ %$Total2% (echo Same number of files) else (echo Different number of files)

如果您的系统不是英文版,则必须根据您的系统语言更改"File"(例如:法语中的“Fichier(s)”)

编辑:

将更多目录与第一个目录进行比较:

@echo off

set $Folder1="c:\Users\abc\INBOX"

set $folder2="c:\Users\abc\OUTBOX"
set $Folder3=c:\Users\abc\OUTBOX\PROC
set $Folder4=c:\Users\abc\OUTBOX\PEND
set $Count=0

setlocal EnableDelayedExpansion

for %%x in (%$Folder1% %$Folder2% %$Folder3% %$Folder4%) do (
     for /f "tokens=1 delims= " %%a in ('dir %%x /a-d ^| find /i "File(s)"') do (
               call:test %%x %%a
               if !$count! Equ 0 set $Ref=%%a
                                set $Count=1))
exit/b

:test

if !$count! Equ 1 (
    If "%$Ref%" Equ "%2" (echo %$Folder1% SAME %1) else (echo %$Folder1% DIFFERENT %1))

答案 1 :(得分:0)

@echo off

    setlocal enableextensions disabledelayedexpansion

    call :getNumberOfFilesInFolderList nINBOX "c:\Users\abc\INBOX"
    call :getNumberOfFilesInFolderList nFiles "c:\Users\abc\OUTBOX" "c:\Users\abc\OUTBOX\PROC" "c:\Users\abc\OUTBOX\PEND"

    if %nINBOX% EQU %nFiles% (
        echo SAME number of files
    ) else (
        echo DIFFERENT number of files
    )

    endlocal
    exit /b

:getNumberOfFilesInFolderList variable folder1 [[folder2] ... ]
    setlocal enableextensions disabledelayedexpansion
    set "variable="
    set /a "total=0"
    for %%a in (%*) do if not defined variable (set "variable=%%~a" ) else (
        for /f %%b in ('dir /a-d "%%~a" 2^>nul ^| findstr /r /c:"^[ ][ ][ ]*[0-9]"') do set /a "total+=%%b"
    )
    endlocal & set "%~1=%total%" & echo %total%
    goto :eof

答案 2 :(得分:0)

这应该比较两个文件夹。

@echo off
set aa=0&set bb=0
for %%a in ("c:\Users\abc\INBOX\*")  do set /a aa+=1
for %%a in ("c:\Users\abc\OUTBOX\*") do set /a bb+=1

if %aa% EQU %bb% (
     echo they have the same number of visible files.
  ) else (
     echo the file count is different
)