Solved batch file list shows size in bytes

June 9, 2011 at 08:17:39
Specs: Windows 7
Hi guys,

I need your help.

I tried this batch file to create a list of all folders, sub folders and files in my drive and it works just fine:
dir /s > list.txt
The problems is that it also shows the size of the files in the folders and files in bytes. Is there some kind of script i can add to show the size in megabytes rather than bytes?

Thanks,

freeloaderdono


See More: batch file list shows size in bytes

Report •

#1
June 10, 2011 at 17:06:04
Interesting and I'm sure someone will come in with a solution but first can you define how you want to display the filesize in mb if the file contains less than 1 mb?

Example: filesize = 5 bytes one mb = 1048576 bytes (or could be 1000000) so the filesize in mb is 0.00000476837...... which I guess is just about meaningless to most of us and would need to be converted back to bytes to be meaningful.


Please come back & tell us if your problem is resolved.


Report •

#2
June 10, 2011 at 18:07:19
Good point, i haven't thought about files under 1mb since all the files i have are 100mb+. I am talking about a dedicated hard drive for media storage.

Report •

#3
June 10, 2011 at 20:24:41
✔ Best Answer
Might work:

@echo off
SetLocal EnableDelayedExpansion


rem drive/directory start point
pushd "d:\"



for /r %%a in (.) do (
    for /f "tokens=1,3" %%b in ('dir /a/s/-c "%%~fa" ^| find /i "file(s)"') do (
        set dirbytes=%%c
        set dirfiles=%%b
    )
    call :BYTE_TO_MiB !dirbytes! MiB
    echo(
    echo(
    echo Directory %%~fa !dirfiles! File(s^) !MiB!MiB %%~ta
    echo(
    if not !dirfiles!==0 (
        for /f "tokens=*" %%d in ('dir /b "%%~a" ') do (
            for %%e in ("%%~a\%%~nxd") do (
                call :BYTE_TO_MiB %%~ze MiB 8
                echo:    %%~te  !MiB!MiB  %%~fe
            )
        )
    )
)
popd
pause
goto :eof




:BYTE_TO_MiB
if "%~1"=="" goto :eof
if "%~2"=="" goto :eof
set %~2=0
for /f "delims=1234567890." %%a in ("%~1") do goto :eof
set dplace=%~3
if not defined dplace set dplace=8
for /f "delims=1234567890." %%a in ("%~4") do set dplace=8
set input=0%~1
for /l %%a in (1 1 %dplace%) do set input=!input!0

set chunk=
set total=
set /a fpos=dplace + 1
:BTM_isfloat
if not "!input:.=!"=="!input!" (
    if not "!input:~-%fpos%,1!"=="." (
        set input=!input:~0,-1!
        goto BTM_isfloat
    ) else (
        set input=!input:.=!
    )
)

:BTM_split
if not "%input:~3%"=="" (
    set /a chunk+=1
    set input!chunk!=%input:~-3%
    set input=%input:~0,-3%
    if defined input goto BTM_split
) else (
    set /a chunk+=1
    set input!chunk!=%input%
)

:BTM_loop
if defined input%chunk% (
    if "!input%chunk%:~0,1!"=="0" (
        set input%chunk%=!input%chunk%:~1!
        goto BTM_loop
    )
) else (
    set input%chunk%=0
    goto BTM_pad
)
set chunkresult=0

:BTM_divide
If !input%chunk%! geq 1048576 (
    If !input%chunk%! geq 1048576000 (
        set /a input%chunk%-=1048576000
        set /a chunkresult+=1000
        goto BTM_divide
    ) else (
        If !input%chunk%! geq 104857600 (
            set /a input%chunk%-=104857600
            set /a chunkresult+=100
            goto BTM_divide
        ) else (
            If !input%chunk%! geq 10485760 (
                set /a input%chunk%-=10485760
                set /a chunkresult+=10
                goto BTM_divide
            ) else (
                set /a input%chunk%-=1048576
                set /a chunkresult+=1
                goto BTM_divide
            )
        )
    )
)
:BTM_pad
if "!chunkresult:~2,1!"=="" set chunkresult=0!chunkresult!
if "!chunkresult:~2,1!"=="" set chunkresult=0!chunkresult!
set total=%total%%chunkresult%
set chunkresult=0
if %chunk% gtr 0 (
    set /a chunk-=1
    if !input%chunk%! gtr 0 (
        set carry=!input%chunk%!
        for %%a in (!chunk!) do set input!chunk!=!carry!!input%%a!
    )
)
if %chunk% gtr 0 goto BTM_loop
if not defined total set total=0
if %dplace% gtr 0 set total=!total:~0^,-%dplace%!.!total:~-%dplace%!

:BTM_finish
if "%total:~0,1%"=="0" if not "%total:~1%"=="" set total=%total:~1%&&goto BTM_finish
if "%total:~0,1%"=="." set total=0%total%
set %~2=%total%
exit /b 0


Report •

Related Solutions

#4
June 11, 2011 at 00:35:11
Nice one Judago...


Please come back & tell us if your problem is resolved.


Report •

#5
June 11, 2011 at 08:37:36
Nice. Thanks!

The only thing missing though is the ability to output the data into .txt.


Report •

#6
June 11, 2011 at 14:28:40
The only thing missing though is the ability to output the data into .txt.

That's easily done:

call "thatscript.bat" > "somefile.txt"

@Wahine
Thanks!


Report •

Ask Question