用于比较文件夹大小的批处理文件

时间:2015-07-22 06:16:44

标签: batch-file byte sizeof directory

硬盘驱动器现在很大,我想将所有图片存储在其上,有时检查我是否错误地添加或移动了任何文件。

这个是接近我需要的那个,但我不知道它给我看的所有文件夹,只是我选择的文件夹

How to list all folder with size via batch file

我需要一个简单的批处理文件,它可以进行字节计数并将其与应该回显的数字进行比较,好与否,然后在我选择的文件夹上进行相同的例程。 感谢您的帮助

set "folder=DVD-001 - 4,471,234,796" 

set "size=0

for /f "tokens=3,5" %%b in ('dir /-c /a /w /s "%%~fa\

@echo foldername  !size! Bytes

IF !size! EQU 4471234796 (@echo good) ELSE (@echo oops)

:next folder

set "folder=DVD-002 - 4,590,775,746"

set "size=0

for /f "tokens=3,5" %%b in ('dir /-c /a /w /s "%%~fa\

@echo foldername  !size! Bytes

IF !size! EQU 4590775746 (@echo good) ELSE (@echo oops)  

1 个答案:

答案 0 :(得分:0)

  • 与预定义的文件夹列表进行比较:

    @echo off
    setlocal enableDelayedExpansion
    
    for /f "delims=: tokens=1,2*" %%a in ('^
        echo DVD-001: 4471234796: f:\folder1 ^&
        echo DVD-002: 4590775746: d:\another folder ^&
        echo DVD-003: 1111111111: e:\folder 3 ^&
        echo DVD-004: 2222222222: c:\folder 4 ^
    ') do (
        set status=
    
        rem trim the path
        for /f "tokens=*" %%c in ("%%~c") do set folder=%%~c
        for /l %%c in (1,1,100) do if "!folder:~-1,1!"==" " set folder=!folder:~0,-1!
        for /l %%c in (1,1,100) do if "!folder:~-1,1!"=="\" set folder=!folder:~0,-1!
    
        rem trim dvd size
        for /f %%b in ("%%~b") do set dvdsize=%%~b
    
        rem get folder size
        set size=
        if exist "!folder!" (
            rem save penultimate line
            for /f "tokens=2,3*" %%s in ('dir "!folder!" /s /w /a 2^>^&1') do (
                set prev=!cur! & set cur=%%t
            )
            rem combine digit groups
            for /l %%s in (0,1,20) do (
                if "!prev:~%%s,1!" geq "0" if "!prev:~%%s,1!" leq "9" (
                    set size=!size!!prev:~%%s,1!
                )
            )
        ) else (
            set size=NOT FOUND
        )
        if !size!==!dvdsize! (
            echo GOOD %%a: !dvdsize! == !folder!
        ) else (
            if "!size!"=="" set size=ERROR
            echo OOPS %%a: !dvdsize! ^<^> !size! !folder!
        )
    )
    pause
    
  • 如果两个文件夹都存在,则仅列表模式中的双向robocopy会更可靠:

    @echo off
    set DIR1=d:\somefolder (no trailing slash!)
    set DIR2=e:\anotherfolder (no trailing slash!)
    robocopy /L /njh /njs /ndl /s "%DIR1%" "%DIR2%" | find "%DIR1%"
    if errorlevel 1 (
        robocopy /L /njh /njs /ndl /s "%DIR2%" "%DIR1%" | find "%DIR2%"
        if errorlevel 1 (echo GOOD & goto DONE)
    )
    echo OOPS
    :DONE
    pause
    

    它也会列出不同的文件。