Announcement

Collapse
No announcement yet.

Output Results from Batch to HTML

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Output Results from Batch to HTML

    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.


    Code:
    @echo off
    
    setlocal disabledelayedexpansion
    
    set opt=E:\Sizes.html
    
    cd /d [location to be scanned]
    
    
    :Label1
    
    echo ^<html^> > %opt%
    
    echo ^<br^> >> %opt%
    
    echo Folders Checked On %date% at %time% >> %opt%
    
    echo ^<br^> >> %opt%
    
    echo ================================================= >> %opt%
    
    echo ^<br^> >> %opt%
    
    call :GetSizes
    
    echo ^<br^> >> %opt%
    
    echo ================================================= >> %opt%
    
    echo ^<br^> >> %opt%
    
    echo ^</font^> >> %opt%
    
    echo ^</html^> >> %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! ^<br^> >> %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


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

    Cheers,
    A hopeless scripter
    Last edited by Rems; 30th August 2014, 03:12.

  • #2
    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.

    Comment


    • #3
      Re: Output Results from Batch to HTML

      Originally posted by Ser Olmy View Post
      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.

      Comment

      Working...
      X