批处理文件返回文件夹大小并删除内容(如果大于50gb)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了批处理文件返回文件夹大小并删除内容(如果大于50gb)相关的知识,希望对你有一定的参考价值。

我正在尝试编写批处理脚本,它将首先返回多个文件夹位置的总和,如果这些位置大于50GB,则删除内容。

我在Google上搜索,仍然不知道如何返回文件夹的大小(以GB为单位)。

任何人都可以帮忙吗?

答案

文件夹的大小可以通过dir /S派生(请参阅输出底部的摘要,其中包含所有子项占用的总字节数)。这可以像这样提取:

set "PREVB="
for /F "skip=2 tokens=3" %%B in ('
    dir /S /-C "path	ofolder"
') do (
    call set "BYTES=%%PREVB%%"
    set "PREVB=%%B"
)

但要小心:不要将此数字视为整数(通过set /Aif EQU / GTR / ...),因为cmd仅支持带符号的32位整数,这很容易超出文件夹大小。

要检查50 GB,您可以拆分结果数字,例如:

rem // Split huge number into GB and ones:
set "BYTES_GIGA=%BYTES:~,-9%"
set "BYTES_ONES=%BYTES:~-9%"
rem // Ensure GB part to be non-empty:
if not defined BYTES_GIGA set "BYTES_GIGA=0"
rem // Remove leading zeroes from ones:
set /A "BYTES_ONES=1%BYTES_ONES%%%1000000000"

然后,如果您愿意,可以将GB编号向上舍入:

if %BYTES_ONES% GTR 0 set /A "BYTES_GIGA+=1"

最后,检查GB编号是否超出预定义的限制:

if %BYTES_GIGA% GTR 50 (
    rem // Do something, perhaps delete the entire folder:
    rmdir /S /Q "path	ofolder"
)
另一答案

在我看来,dir /s /-c返回最后一个块:

Total Files Listed:
         51 File(s)       88492332 bytes
         14 Dir(s)    112224022528 bytes free

而你只需解析该值(88492332)。

您能否提供更详细的详细信息,了解您拥有的代码以及无法使用的代码?


This works for me:

GetSize.bat

for /F "delims=:" %%a in ('dir /-c /s ^| findstr /N /C:"Total Files Listed"') do set SKIP=%%a
for /F "tokens=3" %%a in ('dir /-c /s ^| more +%SKIP%') do (set FOLDER_SIZE=%%a & goto :end)

:end
echo Final Folder Size is %FOLDER_SIZE%
另一答案

使用robocoby输出和擦除

代码重新编写。

@echo off
setlocal
prompt $g$s

 rem set size= 150.0 m
 rem usage -> see SUB :Fsize
set size= 50.0 g

call :createTMP
set /a _N=0

:Listing
 rem list Folder List size and delete
for /D %%F in (
  D:1?
  D:80
  D:2014-*
  D:a

) do (
  if exist %%F if /i NOT "%~0"==":Listing" (
    set /a _N+=1
    call mklink /j "%%empty.link.tmp%%\%%_N%%" "%%~F"
  ) else call :delall "%%~F"
)

 rem --- END SETTINGS ---


if :SUB==only (
 :createTMP
 set "empty.tmp=%temp%empty_%time::=%"
 set "empty.link.tmp=%temp%link_%time::=%"
 md "%empty.tmp%"
 md "%empty.link.tmp%"
 exit /b
)

if /i "%~0"==":Listing" (
  :deltmp
  for /d %%D in (
    "%empty.link.tmp%*"
    "%empty.link.tmp%"
    "%empty.tmp%" 
  ) do rd "%%~D"
  exit /b
)

:Listsize  
call :Fsize "%size%" size.max
for /f "skip=2tokens=6*" %%a in ('
   robocopy /L /purge /njh "%empty.tmp%" "%empty.link.tmp%" /nfl /ndl ^|find " 0 "
') do call :Fsize "%%b" size.out deci.mal pre
if NOT %size.out% lss %size.max% (
  echo Folders: %deci.mal% %pre% ^> %size%
  call :Listing
)else echo Folders %deci.mal% %pre% ^< %size% &call :deltmp
exit/b

:Fsize "512.0 k" [VAR1-floating point [VAR2-to display only [VAR3-prefix]]]
 rem to use with output of robocopy 
 rem set with decimal minimum 1/1     max b  =1024
 rem set with decimal minimum 1/10    max k  =.9    rounded + 102.3  bytes (+  10 %)
 rem          decimal minimum 1/100   max m  =.99   rounded + 10.23  KiB   (+   1 %)
 rem          decimal minimum 1/1000  max g  =.999  rounded + 1.023  MiB   (+ 0,1 %)
 rem 500.0 b        -> 500      bytes
 rem 200.5 k        -> 200,5    KiB
 rem 350.04 m       -> 350,04   MiB
 rem 1.001 g        -> 1,001    GiB
 rem VAR1 size.out  - Binary prefix and number to floating point
 rem VAR2 deci.mal  - Reverse the calculation part to display only
 rem VAR3 pre.fix   - Reverse the calculation to add prefix
set/a p=-1,TC=10000
for %%N in (b k m g t p)Do set /a "p+=1,%%N=p"
for /f "tokens=1-3delims=. " %%A in ("%~1 0 b")Do (
  set/a"pre.fix=TC*(%%C+1) + %%A"
  set "deci.mal=%%A.%%B"
  setlocal enabledelayedexpansion
  set "FS=!pre.fix!.%%B"
  for /f "tokens=1,2" %%S in  ("!FS! !deci.mal!")Do (
    endlocal
    if NOT "%~2"=="" (set "%~2=%%S")else set "size.out=%%S"
    if NOT "%~3"=="" set "%~3=%%T"
  )
)
set/ax=pre.fix/TC-1
for %%s in (b k m g t p)Do 2>nul set/a"N/=(%%s-x)"||^
 if NOT "%~4"=="" (set "%~4=%%s")else set "pre.fix=%%s"

exit /b

:delall
 rem be carfully - this will erase all Data !
 rem 1st with option /L only list this Files
 rem without /L will erase all Data 
:@echo off
setlocal
set "T=%temp%DF%time::=%"
md "%T%" ||exit /b 1
:Delete
if "%~1" equ "" goto :end
robocopy /L /fp /purge /ns /np /njh /njs "%T%" "%~dp1" "%~nx1"
shift
goto :Delete
:end
rd "%T%"
exit /b 0

以上是关于批处理文件返回文件夹大小并删除内容(如果大于50gb)的主要内容,如果未能解决你的问题,请参考以下文章

JAVA大数据处理题

面试常用算法

PHP x64 仍然返回错误的文件大小

十道海量数据处理面试题与十个方法大总结

linuxfind查找大于多少天的文件,并删除之

Wix:处理大于 2 GB 的文件