0

Found this working code from this post: HERE

Works awesome for listing folders with size, but what needs to be changed in this script to list only files with size not folders?

Script output example:

folder - 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
Community
  • 1
  • 1
Sunint
  • 33
  • 1
  • 4
  • stackoverflow is not a code writing service. Show what you have tried so far and we can probably help you if you're stuck. – cel Apr 03 '15 at 17:26
  • for show just file you should change `dir /A:-D ` – Soheil Apr 03 '15 at 17:49
  • Try `for /F "delims=" %G in ('dir /b /s /a:-D "%cd%"') do @echo %G %~zG` from command line window. Use `%%G` and `%%~zG` in a batch file – JosefZ Apr 03 '15 at 19:08
  • JosefZ the problem with your script is that it outputs non human readable size. – Sunint Apr 03 '15 at 20:59
  • Script I found does exatly what I want. Problem is it only lists folders, but I need script to list only files with human readable size. – Sunint Apr 03 '15 at 21:17
  • :) my comment does not pretend to be considered as an answer; you could divide `%%~zG` value by 1024 sequentially while greater than 1024 (and add appropriate unit)... – JosefZ Apr 03 '15 at 23:56

1 Answers1

0

Next code snippet will list only files (not folders) with size. Append both :GetUnit and :ConvertBytes subroutines unchanged.

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

Echoed both !size! and !newsize! merely for debugging purposes...

Another approach with folder name in a header line:

@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
JosefZ
  • 28,460
  • 5
  • 44
  • 83