用于比较文件夹大小(以字节为单位)的批处理文件
batch file to compare Folder sizes in bytes
现在硬盘很大,我想把我所有的照片都放在上面,有时会检查我是否错误地添加或移动了任何文件。
这个是最接近我需要的那个,但我不知道它能给我显示所有文件夹,只显示我选择的文件夹
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)
与大小为的预定义文件夹列表相比:
@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
它也会列出不同的文件。
现在硬盘很大,我想把我所有的照片都放在上面,有时会检查我是否错误地添加或移动了任何文件。
这个是最接近我需要的那个,但我不知道它能给我显示所有文件夹,只显示我选择的文件夹
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)
与大小为的预定义文件夹列表相比:
@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
它也会列出不同的文件。