通过批处理文件列出所有文件大小

时间:2015-04-03 17:19:30

标签: windows batch-file size directory dir

从以下帖子中找到此工作代码:HERE

用于列出具有大小的文件夹非常棒,但是在此脚本中需要更改哪些内容才能仅列出大小不是文件夹的文件?

脚本输出示例:

  

文件夹 - 105,4 GB

     

folder1 - 6,7 GB

     

folder2 - 15,6 GB

@echo off
setlocal disabledelayedexpansion

set "folder=%~1"
  if not defined folder set "folder=%cd%"

    for /d %%a in ("%folder%\*") do (
        set "size=0"
        for /f "tokens=3,5" %%b in ('dir /-c /a /w /s "%%~fa\*" 2^>nul ^| findstr /b /c:"  "') do if "%%~c"=="" set "size=%%~b"
        setlocal enabledelayedexpansion
        call :GetUnit !size! unit
        call :ConvertBytes !size! !unit! newsize
        echo(%%~nxa - !newsize! !unit!
        endlocal
    )

endlocal
exit /b

:ConvertBytes bytes unit ret
setlocal
if "%~2" EQU "KB" set val=/1024
if "%~2" EQU "MB" set val=/1024/1024
if "%~2" EQU "GB" set val=/1024/1024/1024
if "%~2" EQU "TB" set val=/1024/1024/1024/1024
> %temp%\tmp.vbs echo wsh.echo FormatNumber(eval(%~1%val%),1)
for /f "delims=" %%a in ( 
  'cscript //nologo %temp%\tmp.vbs' 
) do endlocal & set %~3=%%a
del %temp%\tmp.vbs
exit /b


:GetUnit bytes return
set byt=00000000000%1X
set TB=000000000001099511627776X
if %1 LEQ 1024 set "unit=Bytes"
if %1 GTR 1024   set "unit=KB"
if %1 GTR 1048576  set "unit=MB"
if %1 GTR 1073741824  set "unit=GB"
if %byt:~-14% GTR %TB:~-14% set "unit=TB"
endlocal & set %~2=%unit%
exit /b

1 个答案:

答案 0 :(得分:0)

下一个代码段将仅列出大小的文件(不是文件夹)。附加:GetUnit:ConvertBytes子程序不变。

ECHO OFF >NUL
SETLOCAL enableextensions disabledelayedexpansion
  set "folder=%~1"
  if not defined folder set "folder=%cd%"
  if not exist "%folder%" set "folder=%cd%"
    for /F "delims=" %%a in ('dir /B /S /A:-D "%folder%\"') do (
        set "size=%%~za"
        setlocal enabledelayedexpansion
          call :GetUnit !size! unit
          call :ConvertBytes !size! !unit! newsize
          echo(%%~a - !size! - !newsize! !unit!   
        endlocal
    )
ENDLOCAL
goto :eof

Echo编辑!size!!newsize!仅用于调试目的......

标题行中包含文件夹名称的另一种方法:

@ECHO OFF >NUL
SETLOCAL enableextensions disabledelayedexpansion
  set "folder=%~1"
  if not defined folder set "folder=%cd%"
  if not exist "%folder%" set "folder=%cd%"

  set "subfolder=%folder%"
  Call :listfiles

  pushd "%folder%"
  rem for /D /R %%G in (*) do (
  rem the option `/D /R` is undocumented
  for /F "delims=" %%G in ('dir /B /A:D *.* 2^>nul') do (
      set "subfolder=%%~G"
      Call :listfiles
  )
  popd
ENDLOCAL
goto :eof

:listfiles
  pushd "%subfolder%"
  echo(
  echo     folder %cd%
  for /F "delims=" %%a in ('dir /B /A:-D *.* 2^>nul') do (
      set "size=%%~za"
      setlocal enabledelayedexpansion
        call :GetUnit !size! unit
        call :ConvertBytes !size! !unit! newsize
        echo(%%~nxa - !size! - !newsize! !unit!   
      endlocal
  )
  popd
goto :eof