Help on getting file size in proper manner

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
kalyanpuli
Posts: 3
Joined: 31 Jan 2017 09:45

Help on getting file size in proper manner

#1 Post by kalyanpuli » 31 Jan 2017 10:16

Hi,
I wrote a script which will get the file size and dump to a CSV. Below is the script:

Code: Select all

::get DB size
set targetDBSize=
for /F "tokens=1 " %%i in ('du -m \\servername\c$\Progra~1\Micros~1\MSSQL10_50.MSSQLSERVER\MSSQL\DATA\%targetSite%.mdf') do set targetDBSize=%%i

echo %targetServer%,%targetSite%,%targetAppFolderSize%,%targetDBSize% >> site_usage.csv


But, some size of ".MDF" are showing as negative values.

Can someone please help me. Below is the sample of the file:

Server Site App Folder(MB) Database (MB)
ABC123 test 372.8 139
ABC123 test1 250.82 -1697
ABC123 test3 1933.15 -210
ABC123 test4 0
ABC123 test5 191.75 222


-Kalyan

Squashman
Expert
Posts: 4465
Joined: 23 Dec 2011 13:59

Re: Help on getting file size in proper manner

#2 Post by Squashman » 31 Jan 2017 10:36

I am not sure what you are asking us to help you with?

Squashman
Expert
Posts: 4465
Joined: 23 Dec 2011 13:59

Re: Help on getting file size in proper manner

#3 Post by Squashman » 31 Jan 2017 10:50

Your FOR command is not even programmed correctly to parse the output of the DU command. Regardless of that if you need the size of a file it is already built-in to the FOR command.

Code: Select all

for %%G in ("\\servername\c$\Progra~1\Micros~1\MSSQL10_50.MSSQLSERVER\MSSQL\DATA\%targetSite%.mdf") do set targetDBSize=%%~zG

kalyanpuli
Posts: 3
Joined: 31 Jan 2017 09:45

Re: Help on getting file size in proper manner

#4 Post by kalyanpuli » 01 Feb 2017 08:32

Hi,
But if the file size is more than 2GB, it is not showing any result.

-Kalyan

Squashman
Expert
Posts: 4465
Joined: 23 Dec 2011 13:59

Re: Help on getting file size in proper manner

#5 Post by Squashman » 01 Feb 2017 09:01

kalyanpuli wrote:Hi,
But if the file size is more than 2GB, it is not showing any result.

-Kalyan

No problems on my end with file sizes greater than 2GB.

Code: Select all

J:\>for %G in (P001.txt) do @echo %~zG
2979626010

kalyanpuli
Posts: 3
Joined: 31 Jan 2017 09:45

Re: Help on getting file size in proper manner

#6 Post by kalyanpuli » 06 Feb 2017 01:45

Hi,
Got it, can I get the result in MB's?
It's giving me blank value using the below one.

Code: Select all

:next
::get DB size
set targetDBSize=
for %%i in ("\\SERVER_NAME\c$\Progra~1\Micros~1\MSSQL10_50.MSSQLSERVER\MSSQL\DATA\%targetSite%.mdf") do set /A targetDBSize=%%~zi/1024/1024

echo %targetDBSize% >> DB_usage.csv

Squashman
Expert
Posts: 4465
Joined: 23 Dec 2011 13:59

Re: Help on getting file size in proper manner

#7 Post by Squashman » 06 Feb 2017 07:33

kalyanpuli wrote:Hi,
Got it, can I get the result in MB's?
It's giving me blank value using the below one.

Code: Select all

:next
::get DB size
set targetDBSize=
for %%i in ("\\SERVER_NAME\c$\Progra~1\Micros~1\MSSQL10_50.MSSQLSERVER\MSSQL\DATA\%targetSite%.mdf") do set /A targetDBSize=%%~zi/1024/1024

echo %targetDBSize% >> DB_usage.csv


It is actually giving you an error.

Code: Select all

Invalid number.  Numbers are limited to 32-bits of precision.

pieh-ejdsch
Posts: 239
Joined: 04 Mar 2014 11:14
Location: germany

Re: Help on getting file size in proper manner

#8 Post by pieh-ejdsch » 06 Feb 2017 15:57

Oh a converter ...
So it should calculate and display.
From bytes over Mib; MB; Terabyte; PiB and EB, you can calculate everything here as you wish.

Code: Select all

@echo off 
setlocal

set /a oneMB=1024*1024*1024
for %%i in ( 1048576 1048575 1000000000 1024 1023 365 645378829 999999999999999999999 10234567888299 %oneMB%
 )Do call :ListSizes %%i

if :Sub==only (
:ListSizes
set "b=%~1"
set/ak=m=g=t=p=kib=mib=gib=tib=pib=0
call :diff %b%  1024 K bytes
if %k% neq 0 call :diff %K%  1024 M kiB
if %M% neq 0 call :diff %M%  1024 G miB
if %G% neq 0 call :diff %G%  1024 T giB
if %T% neq 0 call :diff %T%  1024 P tiB
if %P% neq 0 call :diff %P%  1024 E piB

echo(
echo results:
 echo in %b%  bytes
 echo  = %G% giB +%Mib% mib +%KiB% kiB +%bytes% bytes

if %p%==0 goto :P.
call :diff %p%000 1024 pb
set "pb=000%pb%"
echo  = %E%,%pb:~-3% EiB

:P.
if %t%==0 goto :T.
call :diff %T%000 1024 tb
set "tb=000%tb%"
echo  = %P%,%tb:~-3% PiB

:T.
if %g%==0 goto :G.
call :diff %G%000 1024 gb
set "gb=000%gb%"
echo  = %T%,%gb:~-3% TiB

:G.
call :diff %M%000 1024 mb
set "mb=000%mb%"
echo  = %G%,%mb:~-3% GiB

call :diff %K%000 1024 kb
set "kb=000%kb%"
echo  = %M%,%kb:~-3% MiB

call :diff %b%000 1024 byte
set "byte=000%byte%"
echo  = %k%,%byte:~-3% KiB

exit /b
if :==Sub * END :ListSizes * )


if :Sub==only (
 ****   Max Diff = 32bit /10   ****
:diff in# diff#  [VARout]  [VarRest]
set /a i=o=0 ,diff = %2 ||exit /b 1
for /f ^"eol^=^
%== LF > emtpy Line ==%
^" %%# in ('cmd /u /c"echo %~1"^|find /v ""')do (
  set /a i +=1%%# -10^
  ,n=i /diff ^
  ,R=i %%diff ^
  ,i=r *10
   rem set /a o=o *10 +n
  setlocal enabledelayedexpansion
  for /f "tokens=*delims=0" %%o in ("!o!!n!")do (
    endlocal
    if NOT :%%o==: set "o=%%o"
  )
)
if "%~3"=="" echo %o% R %r%
if NOT "%~3"=="" set "%~3=%o%"
if NOT "%~4"=="" set "%~4=%r%"
exit /b
if :==Sub * END :diff * )

rem END script
pause
exit /b


Phil

Aacini
Expert
Posts: 1885
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Help on getting file size in proper manner

#9 Post by Aacini » 06 Feb 2017 23:47

I copied the solution I posted at this answer, that convert an unlimited size number into a base 1024 number, and slightly modified it in order to show the result with the proper units.

Code: Select all

@echo off
setlocal EnableDelayedExpansion

rem Decimal to power-of-1024 base conversion of an unlimited size decimal number
rem Antonio Perez Ayala

rem Divide the number in 9-digits groups, eliminating left zeros in each group
set "number=%1"
set "groups=0"
:nextGroup
   set group=%number:~-9%
   for /L %%a in (1,1,8) do if "!group:~0,1!" equ "0" set "group=!group:~1!"
   set /A groups+=1
   set "group[%groups%]=%group%"
   set "number=%number:~0,-9%"
if defined number goto nextGroup

rem Convert the 9-digits groups to power-of-1024 values
set /A bitPos=0, power=0, power[0]=0
:nextBinaryDigit
   rem Divide the 9-digits groups by 2
   set carry=0
   for /L %%i in (%groups%,-1,1) do (
      set /A term=carry*1000000000+group[%%i], group[%%i]=term/2, carry=term%%2
   )
   rem Insert remainder in current PowerOf1024 value, in right-to-left order
   set /A "power[%power%]+=carry<<bitPos, bitPos+=1"
   rem If current PowerOf1024 value completed: pass to next one
   if %bitPos% equ 10 set /A bitPos=0, power+=1 & set power[!power!]=0
   rem If last (most significant) group was completely converted: eliminate it
   if !group[%groups%]! equ 0 set /A groups-=1
   rem And pass to convert the rest of 9-digits groups
if %groups% gtr 0 goto nextBinaryDigit
if !power[%power%]! equ 0 set "power[%power%]=" & set /A power-=1

rem Show the result with proper units
set i=0
for %%a in (B KB MB GB TB) do (
   set "unit[!i!]=%%a"
   set /A i+=1
)
set "result="
for /L %%i in (%power%,-1,0) do set "result=!result! + !power[%%i]! !unit[%%i]!"
echo %1  =  %result:~3%

Some output examples:

Code: Select all

2979626010  =  2 GB + 793 MB + 607 KB + 26 B
999999999999999  =  909 TB + 506 GB + 588 MB + 415 KB + 1023 B

Antonio

thefeduke
Posts: 211
Joined: 05 Apr 2015 13:06
Location: MA South Shore, USA

Re: Help on getting file size in proper manner

#10 Post by thefeduke » 09 Feb 2017 14:59

Aacini wrote:I ... slightly modified it in order to show the result with the proper units.
I liked the script so much that I added this for my own use:

Code: Select all

rem Show the result with proper units
set i=0
for %%a in (B KB MB GB TB PB EB ZB YB) do (
   set "unit[!i!]=%%a"
   set /A i+=1
)
set i=0
for %%a in (Bytes kiloBytes megaBytes gigaBytes teraBytes petaBytes exaBytes zettaBytes yottaBytes) do (
   set "units[!i!]=%%a"
   set /A i+=1
)
Thanks,
John A.

Post Reply