Coming Soon: GET-IT: Endpoint Management 1-Day Conference on September 28th at 9:30 AM ET Coming Soon: GET-IT: Endpoint Management 1-Day Conference on September 28th at 9:30 AM ET

Output Results from Batch to HTML

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

Viewing 1 post (of 1 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

Viewing 1 post (of 1 total)

You must be logged in to reply to this topic.

Live Webinar: Active Directory Security: What Needs Immediate Priority!Live on Tuesday, October 12th at 1 PM ET

Attacks on Active Directory are at an all-time high. Companies that are not taking heed are being punished, both monetarily and with loss of production.

In this webinar, you will learn:

  • How to prioritize vulnerability management
  • What attackers are leveraging to breach organizations
  • Where Active Directory security needs immediate attention
  • Overall strategy to secure your environment and keep it secured

Sponsored by: