Output Results from Batch to HTML

Home Forums Scripting DOS Command Shell Output Results from Batch to HTML

This topic contains 2 replies, has 3 voices, and was last updated by Avatar khalid 5 years, 3 months ago.

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #164181

    Hi Petri Forum,

    Apologies for asking for help without so much as an introduction, but I am in need of batch file support and have found this forum to be an excellent resource in the past.

    I am trying to write a batch file which can report back the sizes of all folders in a directory and output the result to an HTML file which managers can view. In principle I know how to do this, but I am having trouble with my code producing duplicate results.

    Unfortunately I cannot use Powershell to accomplish this task as the script needs to run locally and the server in question is an old 2003 machine which I am not permitted to install anything on (I have tried to argue otherwise).

    I’m quite new to scripting in general and have more or less pieced together the script below but I’d greatly appreciate if someone could take a glance at the code and advise as to why it produces a duplicate set of results in the html file.

    [B]@echo off

    setlocal disabledelayedexpansion

    set opt=E:Sizes.html

    cd /d [location to be scanned]

    :Label1

    echo ^ > %opt%

    echo ^
    >> %opt%

    echo Folders Checked On %date% at %time% >> %opt%

    echo ^
    >> %opt%

    echo ================================================= >> %opt%

    echo ^
    >> %opt%

    call :GetSizes

    echo ^
    >> %opt%

    echo ================================================= >> %opt%

    echo ^
    >> %opt%

    echo ^ >> %opt%

    echo ^ >> %opt%

    :GetSizes

    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! ^
    >> %opt%

    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[/B][/CODE]

    Sincerely appreciate any help that anybody is able to provide on this.

    Cheers,
    A hopeless scripter[CODE]
    @echo off

    setlocal disabledelayedexpansion

    set opt=E:Sizes.html

    cd /d [location to be scanned]

    :Label1

    echo ^ > %opt%

    echo ^
    >> %opt%

    echo Folders Checked On %date% at %time% >> %opt%

    echo ^
    >> %opt%

    echo ================================================= >> %opt%

    echo ^
    >> %opt%

    call :GetSizes

    echo ^
    >> %opt%

    echo ================================================= >> %opt%

    echo ^
    >> %opt%

    echo ^ >> %opt%

    echo ^ >> %opt%

    :GetSizes

    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! ^
    >> %opt%

    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[/CODE]

    Sincerely appreciate any help that anybody is able to provide on this.

    Cheers,
    A hopeless scripter

    Avatar
    universal
    Member
    #388697

    Re: Output Results from Batch to HTML

    You call :GetSizes, but you don’t have a goto to jump to the end of the script before the :GetSizes label, so the code in that part of the script is run twice.

    Try sticking an “:end” label at the end of the script and put a “goto end” just before the :GetSizes label.

    Avatar
    khalid
    Member
    #390247

    Re: Output Results from Batch to HTML

    Ser Olmy;286059 wrote:
    You call :GetSizes, but you don’t have a goto to jump to the end of the script before the :GetSizes label, so the code in that part of the script is run twice.

    Try sticking an “:end” label at the end of the script and put a “goto end” just before the :GetSizes label.

    Thanks very much!

    I’m now able to output successfully.

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.