Computing.Net > Forums > Programming > Show size of HDD in Mb using batch

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Show size of HDD in Mb using batch

Reply to Message Icon

Name: CR
Date: December 13, 2008 at 16:17:56 Pacific
OS: Windows XP Pro SP3
CPU/Ram: AMD XP 2500+ 1GB
Product: Custom / CUSTOM BUILD
Comment:

Hello again, after having a search around for this I am still non-the wiser... I have a script that gathers the total size of the PC's %systemdrive%. However, it shows It's output in bytes.

My question is, how can I convert bytes to Mb or Gb etc...

As an example, if my HDD is 180Gb in size I need it to show:

Total size of C: is 180Gb

This is what I have:

@ECHO off
for /f "tokens=1" %%T in ('chkdsk "%systemdrive%"^|find /i "total disk"') do (
set HDD=%%T )
ECHO Total size of %systemdrive% is %HDD%bytes
pause



Sponsored Link
Ads by Google

Response Number 1
Name: BatchFreak
Date: December 13, 2008 at 17:04:32 Pacific
Reply:

Heres a good idea :)

SET b=%systemdrive%
SET /a mb=b/1024
SET /a gb=mb/1024
CLS
ECHO Total size is %gb% GB

I only Batch if possible, 2000 more lines of code, oh well.


0

Response Number 2
Name: CR
Date: December 13, 2008 at 17:12:39 Pacific
Reply:

Hey thanks for a quick answer :) Looking at you code I thought, how simple is that and also, why didn't I think of doing it this way too hehe.

Anyway, using your code (with a pause at the end) all I got was: Total size is 0 GB?

Any idea why it shows as 0 GB? and also, what if I want to show Total size is xx GB (zz MB)?


0

Response Number 3
Name: BatchFreak
Date: December 13, 2008 at 18:51:48 Pacific
Reply:

You better appreciate this :)


fsutil volume diskfree C:\>temp.txt
FOR /F "Tokens=* skip=1 delims= " %%A IN (temp.txt) DO echo %%A>>temp2.txt
SORT /+32 temp2.txt /O temp3.txt
FOR /F "tokens=5 Delims=: " %%A IN (temp3.txt) DO ECHO %%A>temp4.txt
FOR /f "tokens=1 Delims= " %%A IN (temp4.txt) DO SET a=%%A
set /a b=%a:~0,-10%
set /a c=b*1024
PAUSE
DEL temp.txt
DEL temp1.txt
DEL temp2.txt
DEL temp3.txt
DEL temp4.txt
CLS
ECHO %b% GB (%c% MB)
PAUSE

I only Batch if possible, 2000 more lines of code, oh well.


0

Response Number 4
Name: Wahine
Date: December 14, 2008 at 00:20:48 Pacific
Reply:

You bet we do. Thanks.


0

Response Number 5
Name: BatchFreak
Date: December 14, 2008 at 00:59:31 Pacific
Reply:

Why do you appreciate it?

I only Batch if possible, 2000 more lines of code, oh well.


0

Related Posts

See More



Response Number 6
Name: Judago
Date: December 14, 2008 at 02:18:10 Pacific
Reply:

I'm not sure if this will be appreciated or not......


For /f "tokens=5 delims=: " %%a in ('fsutil volume diskfree c:^|find /i "of bytes"') do set a=%%a


0

Response Number 7
Name: Judago
Date: December 14, 2008 at 02:52:21 Pacific
Reply:

[edit: small tweek]

Scratch that, this one definitely won't be.


@ECHO OFF
setlocal
set part2=0
for /f "tokens=1 delims= " %%a in ('defrag -a %systemdrive%^|find /i "total"') do (
set sgb=%%a
for /f "tokens=1,2 delims=." %%b in ("%%a") do (
set part1=%%b
set part2=%%c
)
)
set /a part1*=1024
set /a smb=%part2%*10+%part1%
if %part2% gtr 75 set /a smb+=6
if %part2% gtr 50 set /a smb+=6
if %part2% gtr 25 set /a smb+=6
if %part2% gtr 0 set /a smb+=6
ECHO %sgb% GB (Approximately %smb% MB)
endlocal
pause

The only reason I post it is because the numbers are closer(piggy backing on defrag's work)


0

Response Number 8
Name: CR
Date: December 14, 2008 at 15:47:32 Pacific
Reply:

That is fab, thanks you sooo much :) One issue tho, It shows my 180Gb HDD as 200Gb... And if I try and check another HDD I have (a 300Gb one) It doesn't show... Just says Missing operand. then GB (0MB)


0

Response Number 9
Name: BatchFreak
Date: December 14, 2008 at 16:14:04 Pacific
Reply:

Mine is very inacurate go with Judago's I had it working... but it said that SET /a 79998760064\1000000000=2 So I had to find a cheap way of doing it.

I only Batch if possible, 2000 more lines of code, oh well.


0

Response Number 10
Name: Judago
Date: December 14, 2008 at 16:47:53 Pacific
Reply:

Cr,

Is the problem your mentoning with my script or batchfreak's?

Batchfreak,

Set /a is restrained to 32 or (I assume) 64 bit numbers depending on the os, hence your problem.


0

Response Number 11
Name: Judago
Date: December 14, 2008 at 18:11:03 Pacific
Reply:

Ok I went back looking for even more accuracy, and found it. As far as I can tell the only inaccuracy is introduced but defrag rounding to two decimal places and is minimal....


@ECHO OFF
setlocal
set part2=
for /f "tokens=1 delims= " %%a in ('defrag -a %systemdrive%^|find /i "total"') do (
set sgb=%%a
for /f "tokens=1,2 delims=." %%b in ("%%a") do (
set part1=%%b
set part2=%%c
)
)
if not defined part2 set part2=0
set /a part1*=1024
set /a part2*=1024
if not %part2%==0 set /a smb=%part1%+%part2:~0,-2%
if not %part2%==0 (set smb=%smb%.%part2:~-2%) else set smb=%part1%
ECHO %sgb% GB (%smb% MB) (Approximate but very close)
endlocal
pause


0

Response Number 12
Name: CR
Date: December 15, 2008 at 03:37:00 Pacific
Reply:

Hi both and thanks you sooo much for your help with this :)

It was BatchFreaks' script that didn't show the correct size. However, Judagos' did the job nicely :):):)

Who would have thought it so hard to see the size of a HDD...

What would be nice is:

Viewing %systemdrive% drive
Total size: xx GB (xx MB)
Total used: xx GB (xx MB)
Total free: xx GB (xx MB)

I'll have a play but it would seem that you guys are the talent on this subject ;)


0

Response Number 13
Name: Judago
Date: December 15, 2008 at 04:55:44 Pacific
Reply:

Here we go, I think I'm starting to get good at this...


@ECHO OFF
setlocal
set part2=
set part4=
for /f "tokens=1,4 delims= " %%a in ('defrag -a %systemdrive%^|find /i "total"') do (
set sgbt=%%a
set sgbf=%%b
for /f "tokens=1,2 delims=." %%c in ("%%a") do (
set part1=%%c
set part2=%%d
)
for /f "tokens=1,2 delims=." %%e in ("%%b") do (
set part3=%%e
set part4=%%f
)
)
if not defined part4 set part4=0
if not defined part2 set part2=0
set /a part5=part1-part3
set /a part6=1%part2%-1%part4%
if %part6% lss 0 set /a part5-=1&&set /a part6+=100
set sgbu=%part5%.%part6%
for /l %%z in (1,1,6) do set /a part%%z*=1024
if not %part6%==0 set /a smbu=%part5%+%part6:~0,-2%
if not %part6%==0 (set smbu=%smbu%.%part6:~-2%) else set smbu=%part5%
if not %part4%==0 set /a smbf=%part3%+%part4:~0,-2%
if not %part4%==0 (set smbf=%smbf%.%part4:~-2%) else set smbf=%part3%
if not %part2%==0 set /a smbt=%part1%+%part2:~0,-2%
if not %part2%==0 (set smbt=%smbt%.%part2:~-2%) else set smbt=%part1%
echo Values very close but not exact.
echo Viewing %systemdrive% drive
echo Total size: %sgbt% GB (%smbt% MB)
echo Total used: %sgbu% GB (%smbu% MB)
echo Total free: %sgbf% GB (%smbf% MB)
endlocal
pause


0

Response Number 14
Name: CR
Date: December 15, 2008 at 08:32:55 Pacific
Reply:

Wow, that is fab! I just wish I knew half the stuff you guys did!

Ok, how about this - is it at all poss to scan the PC for all fixed hard drives from a batch file and then pass each drive letter to the above batch so the HDD information can be shown for all detected?

If so, then that would be fantastic as it could then run from a USB pen drive and work for any computer configuration as oppossed to being fixed to the default %systemdrive%.


0

Response Number 15
Name: Judago
Date: December 15, 2008 at 14:19:28 Pacific
Reply:

Here we go, there is one thing I should point out my script actually finds volumes not physical disks. For example one physical disk can be partitioned with two volumes(say c: and d:), they would both be picked up separately. It should generally make no difference, just thought I would mention it.


@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
for /f "delims=" %%a in ('fsutil fsinfo drives^|find ":"') do (
set disk=%%a
set disk=!disk:Drives: =!
for /f %%b in ('fsutil fsinfo drivetype !disk!^|find /i "fixed drive"') do (
call :sizer !disk!
)
)
pause
endlocal
goto :eof

:sizer
for /l %%c in (1,1,6) do set part%%c=
for %%d in (sgbt sgbf sgbu smbu smbt smbf) do set %%d=
for /f "tokens=1,4 delims= " %%e in ('defrag -a %1^|find /i "total"') do (
set sgbt=%%e
set sgbf=%%f
for /f "tokens=1,2 delims=." %%g in ("%%e") do (
set part1=%%g
set part2=%%h
)
for /f "tokens=1,2 delims=." %%i in ("%%f") do (
set part3=%%i
set part4=%%j
)
)
if not defined part4 set part4=0
if not defined part2 set part2=0
set /a part5=part1-part3
set /a part6=1%part2%-1%part4%
if %part6% lss 0 set /a part5-=1&&set /a part6+=100
set sgbu=%part5%.%part6%
for /l %%k in (1,1,6) do set /a part%%k*=1024
if not %part6%==0 set /a smbu=%part5%+%part6:~0,-2%
if not %part6%==0 (set smbu=%smbu%.%part6:~-2%) else set smbu=%part5%
if not %part4%==0 set /a smbf=%part3%+%part4:~0,-2%
if not %part4%==0 (set smbf=%smbf%.%part4:~-2%) else set smbf=%part3%
if not %part2%==0 set /a smbt=%part1%+%part2:~0,-2%
if not %part2%==0 (set smbt=%smbt%.%part2:~-2%) else set smbt=%part1%
echo Values very close but not exact.
echo Viewing %1 drive
echo Total size: %sgbt% GB (%smbt% MB)
echo Total used: %sgbu% GB (%smbu% MB)
echo Total free: %sgbf% GB (%smbf% MB)
echo.
goto :eof



0

Response Number 16
Name: CR
Date: December 15, 2008 at 16:21:37 Pacific
Reply:

I'm astounded! That is one fantastic bit of batch scripting. Well it is from my point of view and I am learning so much from your code - thank you so much for your help with this.

I have just ran this on my PC with the following results:

Viewing C:\ drive
Total size: 186 GB (190464 MB)
Total used: 45.0 GB (46080 MB)
Total free: 141 GB (144384 MB)

Viewing D:\ drive
Total size: 298 GB (305152 MB)
Total used: 242.-63 GB (247163.12 MB)
Total free: 55.73 GB (57067.52 MB)

Press any key to continue . . .

I know you say this is not exact but according to each of the above drives properties I actually have:

C:\ drive
Total size: 186 GB
Total used: 45.6 GB
Total free: 140 GB

D:\ drive
Total size: 298 GB
Total used: 242 GB
Total free: 55.7 GB

S aso you can see it is not that far off at all! One thing is the total used on my D:/ drive showing as 242.-63 GB... What is the - (minus) all about any idea?


0

Response Number 17
Name: Judago
Date: December 15, 2008 at 16:44:05 Pacific
Reply:

I didn't think about that problem! Try this slightly modified version.


@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
for /f "delims=" %%a in ('fsutil fsinfo drives^|find ":"') do (
set disk=%%a
set disk=!disk:Drives: =!
for /f %%b in ('fsutil fsinfo drivetype !disk!^|find /i "fixed drive"') do (
call :sizer !disk!
)
)
pause
endlocal
goto :eof

:sizer
for /l %%c in (1,1,6) do set part%%c=
for %%d in (sgbt sgbf sgbu smbu smbt smbf) do set %%d=
for /f "tokens=1,4 delims= " %%e in ('defrag -a %1^|find /i "total"') do (
set sgbt=%%e
set sgbf=%%f
for /f "tokens=1,2 delims=." %%g in ("%%e") do (
set part1=%%g
set part2=%%h
)
for /f "tokens=1,2 delims=." %%i in ("%%f") do (
set part3=%%i
set part4=%%j
)
)
if not defined part4 set part4=00
if not defined part2 set part2=00
set /a part5=part1-part3
set /a part6=1%part2%-1%part4%
if %part6% lss 0 set /a part5-=1&&set /a part6+=100
if %part6% equ 0 (set sgbu=%part5%) else set sgbu=%part5%.%part6%
for /l %%k in (1,1,6) do set /a part%%k*=1024
if not %part6% equ 0 set /a smbu=%part5%+%part6:~0,-2%
if not %part6% equ 0 (set smbu=%smbu%.%part6:~-2%) else set smbu=%part5%
if not %part4% equ 0 set /a smbf=%part3%+%part4:~0,-2%
if not %part4% equ 0 (set smbf=%smbf%.%part4:~-2%) else set smbf=%part3%
if not %part2% equ 0 set /a smbt=%part1%+%part2:~0,-2%
if not %part2% equ 0 (set smbt=%smbt%.%part2:~-2%) else set smbt=%part1%
echo Values very close but not exact.
echo Viewing %1 drive
echo Total size: %sgbt% GB (%smbt% MB)
echo Total used: %sgbu% GB (%smbu% MB)
echo Total free: %sgbf% GB (%smbf% MB)
echo.
goto :eof


The problem was being caused buy a trick I was using with set /a. Set /a doesn't do floating point math and interprets numbers with leading zeros as octal. I broke the numbers up and added 1 in front of the decimal numbers, to avoid leading zeros, then added 100 to the result and took 1 from the gbs if it was negitive. This was fine except I set part4 and part2 to just zero if defrag didn't give any part gb's. (10-173)+100 = -63. Setting part2 and part4 to 00 if undefined and changing the if operator to equ instead or == was also necessary to test the value instead of a string comparison.

When I say it's not exact it may be off by a mb or x hunderd kb, this is from the rounding defrag uses; if your drive happens to be exactly what defrag reports it should be exact.


0

Response Number 18
Name: CR
Date: December 15, 2008 at 17:24:16 Pacific
Reply:

That sorted it - now showing as:

Viewing C:\ drive
Total size: 186 GB (190464 MB)
Total used: 45.0 GB (46080 MB)
Total free: 141 GB (144384 MB)

Viewing D:\ drive
Total size: 298 GB (305152 MB)
Total used: 242.27 GB (248084.48 MB)
Total free: 55.73 GB (57067.52 MB)

I have added to the above batch so that it records the results to a log file that is then moved to the users Desktop once finished.

On it's own this information is not really that useful for the client to know. However, coupled with the rest of the report that is generated from my main PC Clean-Up batch file this fantastic chunk of code now allows me to also see at a glance the users HDD properties and make suggestions on clearing them up by removing old data etc...

Outstanding scripting :)

This is what I have (including the report generating code):

REM Remove old log file if exists
IF EXIST "Test Report*.*" DEL /Q /F /S "Test Report*.*" > NUL
IF EXIST "%systemdrive%\Documents and Settings\%USERNAME%\Desktop\Test Report*.*" DEL /Q /F /S "%systemdrive%\Documents and Settings\%USERNAME%\Desktop\Test Report*.*" > NUL

REM Set name of log file and add todays date
FOR /F "TOKENS=1, 2, 3, 4 DELIMS=-/. " %%j IN ('Date /T') DO SET FILENAME=Test Report %%j-%%k-%%l.txt

SETLOCAL ENABLEDELAYEDEXPANSION
for /f "delims=" %%a in ('fsutil fsinfo drives^|find ":"') do (
set disk=%%a
set disk=!disk:Drives: =!
for /f %%b in ('fsutil fsinfo drivetype !disk!^|find /i "fixed drive"') do (
call :sizer !disk!
)
)
endlocal
goto END_HDD_DETECTION

:sizer
for /l %%c in (1,1,6) do set part%%c=
for %%d in (sgbt sgbf sgbu smbu smbt smbf) do set %%d=
for /f "tokens=1,4 delims= " %%e in ('defrag -a %1^|find /i "total"') do (
set sgbt=%%e
set sgbf=%%f
for /f "tokens=1,2 delims=." %%g in ("%%e") do (
set part1=%%g
set part2=%%h
)
for /f "tokens=1,2 delims=." %%i in ("%%f") do (
set part3=%%i
set part4=%%j
)
)
if not defined part4 set part4=00
if not defined part2 set part2=00
set /a part5=part1-part3
set /a part6=1%part2%-1%part4%
if %part6% lss 0 set /a part5-=1&&set /a part6+=100
set sgbu=%part5%.%part6%
for /l %%k in (1,1,6) do set /a part%%k*=1024
if not %part6% equ 0 set /a smbu=%part5%+%part6:~0,-2%
if not %part6% equ 0 (set smbu=%smbu%.%part6:~-2%) else set smbu=%part5%
if not %part4% equ 0 set /a smbf=%part3%+%part4:~0,-2%
if not %part4% equ 0 (set smbf=%smbf%.%part4:~-2%) else set smbf=%part3%
if not %part2% equ 0 set /a smbt=%part1%+%part2:~0,-2%
if not %part2% equ 0 (set smbt=%smbt%.%part2:~-2%) else set smbt=%part1%

echo Viewing %1 drive >> "%FILENAME%"
echo Total size: %sgbt% GB (%smbt% MB) >> "%FILENAME%"
echo Total used: %sgbu% GB (%smbu% MB) >> "%FILENAME%"
echo Total free: %sgbf% GB (%smbf% MB) >> "%FILENAME%"
echo. >> "%FILENAME%"
goto :eof

:END_HDD_DETECTION
echo NOTE: >> "%FILENAME%"
echo If you have less than 15 percent free space then you may think about >> "%FILENAME%"
echo either removing/reorganising your files to free up some HDD space or >> "%FILENAME%"
echo buying a larger HDD. >> "%FILENAME%"
echo. >> "%FILENAME%"


IF EXIST "%FILENAME%" MOVE /Y "%FILENAME%" "%systemdrive%\Documents and Settings\%USERNAME%\Desktop\"

So, the final part to this would be to maybe add a percentage value if poss...


0

Response Number 19
Name: Judago
Date: December 15, 2008 at 17:35:34 Pacific
Reply:

Good to see it's working properly. I made a trivial edit to the script above if your interested, it should remove the tailing .0 from the gb used.


0

Response Number 20
Name: CR
Date: December 15, 2008 at 17:39:46 Pacific
Reply:

That would be cool if you wouldn't mind posting ;)

Do you also think it would be hard to show a percentage for used and free space too and maybe add a , in the results so we would see 1,000 MB rather than 1000 MB?


0

Response Number 21
Name: Judago
Date: December 15, 2008 at 18:01:39 Pacific
Reply:

What next?? :)


@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
for /f "delims=" %%a in ('fsutil fsinfo drives^|find ":"') do (
set disk=%%a
set disk=!disk:Drives: =!
for /f %%b in ('fsutil fsinfo drivetype !disk!^|find /i "fixed drive"') do (
call :sizer !disk!
)
)
pause
endlocal
goto :eof

:sizer
for /l %%c in (1,1,6) do set part%%c=
for %%d in (sgbt sgbf sgbu smbu smbt smbf) do set %%d=
for /f "tokens=1,4,6 delims= " %%e in ('defrag -a %1 2^>^&1^|find /i "total"') do (
set sgbt=%%e
set sgbf=%%f
set pcfree=%%g
for /f "tokens=1,2 delims=." %%h in ("%%e") do (
set part1=%%h
set part2=%%i
)
for /f "tokens=1,2 delims=." %%j in ("%%f") do (
set part3=%%j
set part4=%%k
)
)
if not defined sgbt exit /b
if not defined part4 set part4=00
if not defined part2 set part2=00
set pcfree=%pcfree:~1,-2%
set /a pcused=100-pcfree
set /a part5=part1-part3
set /a part6=1%part2%-1%part4%
if %part6% lss 0 set /a part5-=1&&set /a part6+=100
if %part6% equ 0 (set sgbu=%part5%) else set sgbu=%part5%.%part6%
for /l %%l in (1,1,6) do set /a part%%l*=1024
if not %part6% equ 0 set /a smbu=%part5%+%part6:~0,-2%
if not %part6% equ 0 (set smbu=%smbu%.%part6:~-2%) else set smbu=%part5%
if not %part4% equ 0 set /a smbf=%part3%+%part4:~0,-2%
if not %part4% equ 0 (set smbf=%smbf%.%part4:~-2%) else set smbf=%part3%
if not %part2% equ 0 set /a smbt=%part1%+%part2:~0,-2%
if not %part2% equ 0 (set smbt=%smbt%.%part2:~-2%) else set smbt=%part1%
echo Values very close but not exact.
echo Viewing %1 drive
echo Total size: %sgbt% GB (%smbt% MB)
echo Total used: %sgbu% GB (%smbu% MB) %pcused%%%
echo Total free: %sgbf% GB (%smbf% MB) %pcfree%%%
echo.
goto :eof

The trivial edit was already in the post above, but because the percentage was so easy as defrag reports percentage free, I thought I may as well post it.


0

Response Number 22
Name: CR
Date: December 15, 2008 at 18:16:54 Pacific
Reply:

Hehe never thought I would geek out over a batch script :) Ok, how about maybe add a , in the results so we would see 1,000 MB rather than 1000 MB?


0

Response Number 23
Name: Judago
Date: December 15, 2008 at 18:55:52 Pacific
Reply:

I've patched in and old script of mined and it's ready to go, but this is getting very complicated.



@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
for /f "delims=" %%a in ('fsutil fsinfo drives^|find ":"') do (
set disk=%%a
set disk=!disk:Drives: =!
for /f %%b in ('fsutil fsinfo drivetype !disk!^|find /i "fixed drive"') do (
call :sizer !disk!
)
)
pause
endlocal
goto :eof

:sizer
for /l %%c in (1,1,6) do set part%%c=
for %%d in (sgbt sgbf sgbu smbu smbt smbf) do set %%d=
for /f "tokens=1,4,6 delims= " %%e in ('defrag -a %1 2^>^&1^|find /i "total"') do (
set sgbt=%%e
set sgbf=%%f
set pcfree=%%g
for /f "tokens=1,2 delims=." %%h in ("%%e") do (
set part1=%%h
set part2=%%i
)
for /f "tokens=1,2 delims=." %%j in ("%%f") do (
set part3=%%j
set part4=%%k
)
)
if not defined sgbt exit /b
if not defined part4 set part4=00
if not defined part2 set part2=00
set pcfree=%pcfree:~1,-2%
set /a pcused=100-pcfree
set /a part5=part1-part3
set /a part6=1%part2%-1%part4%
if %part6% lss 0 set /a part5-=1&&set /a part6+=100
if %part6% equ 0 (set sgbu=%part5%) else set sgbu=%part5%.%part6%
for /l %%l in (1,1,6) do set /a part%%l*=1024
if not %part6% equ 0 set /a smbu=%part5%+%part6:~0,-2%
if not %part6% equ 0 (set smbu=%smbu%.%part6:~-2%) else set smbu=%part5%
if not %part4% equ 0 set /a smbf=%part3%+%part4:~0,-2%
if not %part4% equ 0 (set smbf=%smbf%.%part4:~-2%) else set smbf=%part3%
if not %part2% equ 0 set /a smbt=%part1%+%part2:~0,-2%
if not %part2% equ 0 (set smbt=%smbt%.%part2:~-2%) else set smbt=%part1%
for %%m in (u f t) do call :comm smb%%m
echo Values very close but not exact.
echo Viewing %1 drive
echo Total size: %sgbt% GB (%smbt% MB)
echo Total used: %sgbu% GB (%smbu% MB) %pcused%%%
echo Total free: %sgbf% GB (%smbf% MB) %pcfree%%%
echo.
goto :eof


:comm
@echo off
set outputvar=
SET COUNT=0
if "!%1:.=!"=="!%1!" (set commavar=!%1!) else SET COMMAVAR=!%1:~0,-3!
SET LNGTH=%COMMAVAR%
SET EDITVAR=%COMMAVAR%
:LENGTH
IF NOT DEFINED LNGTH (
if %count% leq 3 goto :eof
GOTO splitter
)
SET /A COUNT+=1
SET LNGTH=%LNGTH:~1%
GOTO LENGTH
:SPLITTER
IF %COUNT% Leq 3 GOTO OUTPUT
SET OUTPUTVAR=%EDITVAR:~-3%,%OUTPUTVAR%
SET EDITVAR=%EDITVAR:~0,-3%
SET /A COUNT-=3
GOTO SPLITTER
:OUTPUT
IF DEFINED EDITVAR SET EDITVAR=%EDITVAR%,
if "!%1:.=!"=="!%1!" (set %1=%EDITVAR%%OUTPUTVAR:~0,-1%) else set %1=%EDITVAR%%OUTPUTVAR:~0,-1%!%1:~-3!
goto :eof

Brain num yet?

You will really want to consider making this batch standalone and calling it (ie call hdd.bat)


0

Response Number 24
Name: CR
Date: December 15, 2008 at 18:58:31 Pacific
Reply:

My brain is very numb with this lol - why is it best to call this batch from my main batch when I can just include it? Is there a limit on file size etc...?


0

Response Number 25
Name: Judago
Date: December 15, 2008 at 19:09:26 Pacific
Reply:

It really doesn't matter, it's more a case of ease of reading your main batch and the larger a batch is the longer it take goto jumps to find labels. Another advantage is that because I setlocal and endlocal the possibility of conflicts between variables is lessened, because when my batch ends all of the variable I used are wiped out. But it really comes down to personal preference so long as it works.


0

Response Number 26
Name: CR
Date: December 15, 2008 at 19:13:57 Pacific
Reply:

Why use setlocal and endlocal in the first place - is it just good batch coding practice? or is it there for a reason other than to clear vars?

P.S.
The script works like a charm :) and much faster than my original that used chkdsk %systemdrive%^|find /i "total disk"...


0

Response Number 27
Name: Judago
Date: December 15, 2008 at 19:26:59 Pacific
Reply:

To answer both your questions it is good practice to clear vars, especially if the script is called, setlocal and endlocal make this easy. Setlocal enabledelayedexpansion has other functionality in addition to the normal setlocal command but that is another story.

There was a major problem with using chkdsk like that any how - "Errors found. CHKDSK cannot continue in read-only mode." this gives no total and since this is to run on clients machines, this would probably happen quite often.


0

Response Number 28
Name: CR
Date: December 15, 2008 at 19:43:44 Pacific
Reply:

Hmmm very valid points. Well once my batch has finished and it exits all vars are cleared anyway so no real big issue in this case ;) but I see what you mean and will try to clean up my code I think...

Well I think we have about covered everything we can with this and what started out as a small useless script has now evolved into something that I for one will find very very useful - thank you so very much for all your time and effort with this!

It is so nice to actually find people these days that actually like helping others out. I have learnt (and am still learning) a lot from your code.

I still do not know what enabledelayedexpansion actually does lol

Anyway thank you again - have a great Yule ;)


0

Response Number 29
Name: CR
Date: December 15, 2008 at 19:44:59 Pacific
Reply:

P.S.

Will this work in Vista too do you know?


0

Response Number 30
Name: Judago
Date: December 15, 2008 at 20:34:18 Pacific
Reply:

It Should work in vista but I have no means of testing at the moment. Perhaps someone on vista can pipe in and say weather it works for them or not??

Setlocal enabledelayedexpansion basically makes variables that would otherwise be converted to text before a for loop, and thus be static for the entirety of the loop, dynamic within a for loop. Variables intended for dynamic use are marked with exclamation marks rather than percentage signs, !var! instead of %var%. It can also be useful with characters that have special meanings in batch, essentially escaping them.


0

Response Number 31
Name: BatchFreak
Date: December 15, 2008 at 21:12:58 Pacific
Reply:

I have a question that builds on this :P Sorry Judago, but you are the most patient UBER-Helper on here.

Inside of a for loop How do I set/ Call variables?
I tried !variable!
I tried %variable%
I tried %%variable%%

None of them work

I only Batch if possible, 2000 more lines of code, oh well.


0

Response Number 32
Name: Judago
Date: December 15, 2008 at 22:43:02 Pacific
Reply:

Try !%variable%!, sometimes with the nature of the for loop it may be necessary to call a label to accomplish this as variables will be updated whilst in this call.


0

Response Number 33
Name: BatchFreak
Date: December 15, 2008 at 23:21:43 Pacific
Reply:

For loops just got a whole lot better :) I remember when I joined here and made a post asking what a FOR loop was :) It seems so long ago and yet... it was a month or so :p

I only Batch if possible, 2000 more lines of code, oh well.


0

Response Number 34
Name: CR
Date: December 16, 2008 at 08:24:24 Pacific
Reply:

Hi peeps, just like to report back that none of the above scripts actually work on Vista at all - just show no output.


0

Response Number 35
Name: Judago
Date: December 16, 2008 at 13:23:56 Pacific
Reply:

That's a disapointment, my guess is that the have changed the output of defrag. When when you say no output do you mean absolutely nothing?

Perhaps someone can post the output in vista. I looked it up and apparently it is *supposed* to be included.

Is this a admin or limited user account? Perhaps certain things are being restricted for security reasons?


0

Response Number 36
Name: CR
Date: December 16, 2008 at 14:39:29 Pacific
Reply:

I didn't get to check what type of account I was on but I would hazard a guess at an admin account as I could use the UAC to run my batch as the Admin so...

As for the output, well my script isn't set to report anything to the screen, but log the output to a log file which just gave the following:

Viewing C:\ drive

NOTE:
If you have less than 15% free space on any drive you should think about either removing any redundant files/applications or reorganising your files to free up some space. However, if this is not an option you should seriously think about purchasing either a new or a larger hard drive.

:(


0

Response Number 37
Name: Judago
Date: December 16, 2008 at 22:40:57 Pacific
Reply:

I'm starting to think using defrag was a bad idea I'm going to go back to the drawing board and try to find a new solution if I can.....


0

Response Number 38
Name: CR
Date: December 17, 2008 at 00:20:48 Pacific
Reply:

A multi platform version.... oooh :)


0

Response Number 39
Name: Judago
Date: December 17, 2008 at 02:53:40 Pacific
Reply:

Ok I'm probably going to be busy for the next few days (have to make my eyes round again). This as far as I have got, a script that divides numbers by 1024 that circumvents the limitations of 32bit numbers. It doesn't even attempt floating point so no remainder is given. Be aware I think I found a bug when writing this script in substring expansion (this line "set svp!ccnt!=%sv:~-4%" was only expanding to 3 chars in the first iteration even though the string was longer). It seems to work fine stand alone, but had problems being called as a label.

It is very difficult to write a script for an os I can't test with, so it looks like this may not come togther....



@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
:div1024
set ccnt=
set sv=%1
set total=
:split
if not defined sv goto loop
if %sv% gtr 999 (
set /a ccnt+=1
set svp!ccnt!=%sv:~-4%
set sv=%sv:~0,-4%
goto split
) else (
set /a ccnt+=1
set svp!ccnt!=%sv%
)
:loop
if not defined svp%ccnt% set svp%ccnt%=0&&goto pad
if "!svp%ccnt%:~0,1!"=="0" set svp%ccnt%=!svp%ccnt%:~1!&&goto loop
set vcnt=0
:devide
if not defined svp%ccnt% set svp%ccnt%=0
If !svp%ccnt%! geq 1024 (
set /a svp%ccnt%-=1024
set /a vcnt+=1
goto devide
)
:pad
if 10000 gtr 1%vcnt% set vcnt=0%vcnt%
if 1000 gtr 1%vcnt% set vcnt=0%vcnt%
if 100 gtr 1%vcnt% set vcnt=0%vcnt%
set total=%total%%vcnt%
set tmpcnt=%ccnt%
call set /a ccnt-=1
if !svp%tmpcnt%! gtr 0 (
set carry=!svp%tmpcnt%!
set svp!ccnt!=!carry!!svp%ccnt%!
)
if %ccnt% equ 0 goto finish
goto loop
:finish
if not defined total set total=0&&goto output
if "%total:~0,1%" == "0" set total=%total:~1%&&goto finish
:output
echo %total%


0

Response Number 40
Name: Judago
Date: December 18, 2008 at 01:01:54 Pacific
Reply:

[edit minor bug]

Would you believe it I'm back already! Here is the snail pace version, it works on xp and hopefully they haven't changed the output of fsutil, if they have I might use dir instead, but that will add the the wait... The numbers should be almost exactly the same except the percentages, I'm calculating them in sort of a rough way so they may vary up to +-2%. It should also work without administrative privileges. Be aware it is quite resource intensive.

I'm not sure if it will work properly on vista...


@ECHO Off
SETLOCAL ENABLEDELAYEDEXPANSION
for /f "delims=" %%a in ('fsutil fsinfo drives^|find ":"') do (
set disk=%%a
set disk=!disk:Drives: =!
for /f %%b in ('fsutil fsinfo drivetype !disk!^|find /i "fixed drive"') do (
for /f "tokens=2 delims=:" %%c in ('fsutil volume diskfree !disk!^|find /i "of bytes"') do (
set smbt=%%c
set smbt=!smbt: =!
set smbt=!smbt:~0,-1!
)
for /f "tokens=2 delims=:" %%d in ('fsutil volume diskfree !disk!^|find /i "avail"') do (
set smbf=%%d
set smbf=!smbf: =!
set smbf=!smbf:~0,-1!
)
call :div1024 smbt
call :div1024 smbt
call :div1024 smbf
call :div1024 smbf
set sgbt=!smbt!
set sgbf=!smbf!
set /a smbu=!smbt:.=!-!smbf:.=!
set smbu=!smbu:~0,-2!.!smbu:~-2!
set sgbu=!smbu!
call :div1024 sgbt
call :div1024 sgbf
call :div1024 sgbu
set /a pcu=!smbu:~0,-3!00/!smbt:~0,-3!
set /a pcf=100-pcu
for %%m in (u f t) do call :comm smb%%m
echo Values close but not exact.
echo Viewing !disk! drive
echo Total size: !sgbt! GB ^(!smbt! MB^)
echo Total used: !sgbu! GB ^(!smbu! MB^) !pcu!%%
echo Total free: !sgbf! GB ^(!smbf! MB^) !pcf!%%
echo.
)
)
pause
goto :eof

:div1024
set ccnt=
set sv=!%1!
set sv=%sv%00
set total=
:float2
if not "!sv:.=!"=="!sv!" (
if not "!sv:~-3,1!"=="." (
set sv=!sv:~0,-1!
goto float2
) else (
set sv=!sv:.=!
)
)
:split
if not defined sv goto loop
if %sv% gtr 999 (
set /a ccnt+=1
set svp!ccnt!=%sv:~-4%
set sv=%sv:~0,-4%
goto split
) else (
set /a ccnt+=1
set svp!ccnt!=%sv%
)
:loop
if not defined svp%ccnt% set svp%ccnt%=0&&goto pad
if "!svp%ccnt%:~0,1!"=="0" set svp%ccnt%=!svp%ccnt%:~1!&&goto loop
set vcnt=0
:devide
if not defined svp%ccnt% set svp%ccnt%=0
If !svp%ccnt%! geq 1024 (
set /a svp%ccnt%-=1024
set /a vcnt+=1
goto devide
)

:pad
if "%vcnt:~3,1%"=="" set vcnt=0%vcnt%
if "%vcnt:~3,1%"=="" set vcnt=0%vcnt%
if "%vcnt:~3,1%"=="" set vcnt=0%vcnt%
set total=%total%%vcnt%
set tmpcnt=%ccnt%
call set /a ccnt-=1
if !svp%tmpcnt%! gtr 0 (
set carry=!svp%tmpcnt%!
set svp!ccnt!=!carry!!svp%ccnt%!
)
if %ccnt% equ 0 goto finish
goto loop
:finish
if not defined total set total=0&&goto output
if "!total:.=!"=="!total!" set total=!total:~0,-2!.!total:~-2!
if "%total:~0,1%" == "0" set total=%total:~1%&&goto finish
:output
if "!total:~0,1!"=="." (set %1=0!total!) else set %1=!total!
goto :eof

:comm
set outputvar=
SET COUNT=0
if "!%1:.=!"=="!%1!" (set commavar=!%1!) else SET COMMAVAR=!%1:~0,-3!
SET LNGTH=%COMMAVAR%
SET EDITVAR=%COMMAVAR%
:LENGTH
IF NOT DEFINED LNGTH (
if %count% leq 3 goto :eof
GOTO splitter
)
SET /A COUNT+=1
SET LNGTH=%LNGTH:~1%
GOTO LENGTH
:SPLITTER
IF %COUNT% Leq 3 GOTO OUTPUTcomm
SET OUTPUTVAR=%EDITVAR:~-3%,%OUTPUTVAR%
SET EDITVAR=%EDITVAR:~0,-3%
SET /A COUNT-=3
GOTO SPLITTER
:OUTPUTcomm
IF DEFINED EDITVAR SET EDITVAR=%EDITVAR%,
if "!%1:.=!"=="!%1!" (set %1=%EDITVAR%%OUTPUTVAR:~0,-1%) else set %1=%EDITVAR%%OUTPUTVAR:~0,-1%!%1:~-3!
goto :eof



0

Response Number 41
Name: CR
Date: December 18, 2008 at 01:46:30 Pacific
Reply:

OMG! You have been busy, thank you :) I'm feeling like I've opened a right can of Vista flavour worms hehe

I can confirm this works just as the old script did under XP (although much slower). As for testing it on Vista, well that will have to wait until I can get some feedback from one of my friends that has Vista installed...

I'm still gob-smacked that you sat and coded this from scratch! Supurb, thanks you so much for taking the time and effort to help with my random questions ;)


0

Response Number 42
Name: Judago
Date: December 18, 2008 at 02:03:00 Pacific
Reply:

No worries, some times I like a bit of a challenge. Some of this stuff will definitely go into my little code repository, so I'm not making a complete loss on my time.

If it doesn't work because of something they have done to fsutil it can be modified easily enough, after all it takes it's input in bytes, even dir can give that info.

Be sure you have the latest version, as I edited out a small bug in the post above(you may have copied it before I fixed it).


0

Response Number 43
Name: CR
Date: December 18, 2008 at 03:53:57 Pacific
Reply:

I'll test the latest version as soon as I can and post back to let you know how it fairs :)

One question though, I understand that the output has poss changed with Vista compaired to XP so your having to do this another way but is this as fast as it can gather the results?


0

Response Number 44
Name: Judago
Date: December 18, 2008 at 04:11:20 Pacific
Reply:

Arggh! I keep posting this script over and over and over.... I decide to optimise and found quite a speed increase....


@ECHO Off
SETLOCAL ENABLEDELAYEDEXPANSION
for /f "delims=" %%a in ('fsutil fsinfo drives^|find ":"') do (
set disk=%%a
set disk=!disk:Drives: =!
for /f %%b in ('fsutil fsinfo drivetype !disk!^|find /i "fixed drive"') do (
for /f "tokens=2 delims=:" %%c in ('fsutil volume diskfree !disk!^|find /i "of bytes"') do (
set smbt=%%c
set smbt=!smbt: =!
set smbt=!smbt:~0,-1!
)
for /f "tokens=2 delims=:" %%d in ('fsutil volume diskfree !disk!^|find /i "avail"') do (
set smbf=%%d
set smbf=!smbf: =!
set smbf=!smbf:~0,-1!
)
call :div1024 smbt
call :div1024 smbt
call :div1024 smbf
call :div1024 smbf
set sgbt=!smbt!
set sgbf=!smbf!
set /a smbu=!smbt:.=!-!smbf:.=!
set smbu=!smbu:~0,-2!.!smbu:~-2!
set sgbu=!smbu!
call :div1024 sgbt
call :div1024 sgbf
call :div1024 sgbu
set /a pcu=!smbu:~0,-3!00/!smbt:~0,-3!
set /a pcu+=1
if !pcu! gtr 100 set pcu=100
set /a pcf=100-pcu
for %%m in (u f t) do call :comm smb%%m
echo Values close but not exact.
echo Viewing !disk! drive
echo Total size: !sgbt! GB ^(!smbt! MB^)
echo Total used: !sgbu! GB ^(!smbu! MB^) !pcu!%%
echo Total free: !sgbf! GB ^(!smbf! MB^) !pcf!%%
echo.
)
)
pause
goto :eof

:div1024
set ccnt=
set sv=!%1!
set sv=%sv%00
set total=
:float2
if not "!sv:.=!"=="!sv!" (
if not "!sv:~-3,1!"=="." (
set sv=!sv:~0,-1!
goto float2
) else (
set sv=!sv:.=!
)
)
:split
if not defined sv goto loop
if %sv% gtr 999 (
set /a ccnt+=1
set svp!ccnt!=%sv:~-4%
set sv=%sv:~0,-4%
goto split
) else (
set /a ccnt+=1
set svp!ccnt!=%sv%
)
:loop
if defined svp%ccnt% (
if "!svp%ccnt%:~0,1!"=="0" (
set svp%ccnt%=!svp%ccnt%:~1!
goto loop
)
) else (
set svp%ccnt%=0
goto pad
)
set vcnt=0
:devide
If !svp%ccnt%! geq 1024 (
If !svp%ccnt%! geq 10240 (
set /a svp%ccnt%-=10240
set /a vcnt+=10
goto devide
) else (
set /a svp%ccnt%-=1024
set /a vcnt+=1
goto devide
)
)
:pad
if "%vcnt:~3,1%"=="" set vcnt=0%vcnt%
if "%vcnt:~3,1%"=="" set vcnt=0%vcnt%
if "%vcnt:~3,1%"=="" set vcnt=0%vcnt%
set total=%total%%vcnt%
set tmpcnt=%ccnt%
set /a ccnt-=1
if !svp%tmpcnt%! gtr 0 (
set carry=!svp%tmpcnt%!
set svp!ccnt!=!carry!!svp%ccnt%!
)
if %ccnt% equ 0 goto finish
goto loop
:finish
if not defined total set total=0&&goto output
if "!total:.=!"=="!total!" set total=!total:~0,-2!.!total:~-2!
if "%total:~0,1%" == "0" set total=%total:~1%&&goto finish
:output
if "!total:~0,1!"=="." (set %1=0!total!) else set %1=!total!
goto :eof

:comm
set outputvar=
set count=
if "!%1:.=!"=="!%1!" (set commavar=!%1!) else set commavar=!%1:~0,-3!
set lngth=%commavar%
set editvar=%commavar%
:length
if defined lngth (
set /a count+=1
set lngth=!lngth:~1!
goto length
)
if %count% leq 3 goto :eof
:splitter
if %count% leq 3 (
if defined editvar set editvar=%editvar%,
if "!%1:.=!"=="!%1!" (set %1=!editvar!!outputvar:~0,-1!) else set %1=!editvar!!outputvar:~0,-1!!%1:~-3!
goto :eof
)
set outputvar=!editvar:~-3!,!outputvar!
set editvar=!editvar:~0,-3!
set /a count-=3
goto splitter



0

Response Number 45
Name: Judago
Date: December 18, 2008 at 04:25:56 Pacific
Reply:

Hey I got to making it faster before I read your post(esp??)! To be honest I realised defrag was a bad idea because it needed to be run as a admin. Everything else reports bytes so I found a way to convert despite the limitations of 32bit numbers. Defrag could work in vista but without seeing it's output I don't know what info it has and what to set my tokens to.


0

Response Number 46
Name: Judago
Date: December 18, 2008 at 15:00:43 Pacific
Reply:

Did I mention Grrrr?? This the absolute last performance tweek, it should rival the speed defrag gave(almost), even on enormous drives.....



@ECHO Off
SETLOCAL ENABLEDELAYEDEXPANSION
for /f "delims=" %%a in ('fsutil fsinfo drives^|find ":"') do (
set disk=%%a
set disk=!disk:Drives: =!
for /f %%b in ('fsutil fsinfo drivetype !disk!^|find /i "fixed drive"') do (
for /f "tokens=2 delims=:" %%c in ('fsutil volume diskfree !disk!^|find /i "of bytes"') do (
set smbt=%%c
set smbt=!smbt: =!
set smbt=!smbt:~0,-1!
)
for /f "tokens=2 delims=:" %%d in ('fsutil volume diskfree !disk!^|find /i "avail"') do (
set smbf=%%d
set smbf=!smbf: =!
set smbf=!smbf:~0,-1!
)
call :div1024 smbt
call :div1024 smbt
call :div1024 smbf
call :div1024 smbf
set sgbt=!smbt!
set sgbf=!smbf!
set /a smbu=!smbt:.=!-!smbf:.=!
set smbu=!smbu:~0,-2!.!smbu:~-2!
set sgbu=!smbu!
call :div1024 sgbt
call :div1024 sgbf
call :div1024 sgbu
set /a pcu=!smbu:~0,-3!00/!smbt:~0,-3!
set /a pcu+=1
if !pcu! gtr 100 set pcu=100
set /a pcf=100-pcu
for %%m in (u f t) do call :comm smb%%m
echo Values close but not exact.
echo Viewing !disk! drive
echo Total size: !sgbt! GB ^(!smbt! MB^)
echo Total used: !sgbu! GB ^(!smbu! MB^) !pcu!%%
echo Total free: !sgbf! GB ^(!smbf! MB^) !pcf!%%
echo.
)
)
pause
goto :eof

:div1024
set ccnt=
set sv=!%1!
set sv=%sv%00
set total=
:float2
if not "!sv:.=!"=="!sv!" (
if not "!sv:~-3,1!"=="." (
set sv=!sv:~0,-1!
goto float2
) else (
set sv=!sv:.=!
)
)
:split
if not defined sv goto loop
if %sv% gtr 999 (
set /a ccnt+=1
set svp!ccnt!=%sv:~-4%
set sv=%sv:~0,-4%
goto split
) else (
set /a ccnt+=1
set svp!ccnt!=%sv%
)
:loop
if defined svp%ccnt% (
if "!svp%ccnt%:~0,1!"=="0" (
set svp%ccnt%=!svp%ccnt%:~1!
goto loop
)
) else (
set svp%ccnt%=0
goto pad
)
set vcnt=0
:devide
If !svp%ccnt%! geq 1024 (
If !svp%ccnt%! geq 1024000 (
set /a svp%ccnt%-=1024000
set /a vcnt+=1000
goto devide
) else (
If !svp%ccnt%! geq 102400 (
set /a svp%ccnt%-=102400
set /a vcnt+=100
goto devide
) else (
If !svp%ccnt%! geq 10240 (
set /a svp%ccnt%-=10240
set /a vcnt+=10
goto devide
) else (
set /a svp%ccnt%-=1024
set /a vcnt+=1
goto devide
)
)
)
)
:pad
if "!vcnt:~3,1!"=="" set vcnt=0!vcnt!
if "!vcnt:~3,1!"=="" set vcnt=0!vcnt!
if "!vcnt:~3,1!"=="" set vcnt=0!vcnt!
set total=%total%%vcnt%
set vcnt=0
set tmpcnt=%ccnt%
set /a ccnt-=1
if !svp%tmpcnt%! gtr 0 (
set carry=!svp%tmpcnt%!
set svp!ccnt!=!carry!!svp%ccnt%!
)
if not %ccnt% equ 0 goto loop
:finish
if not defined total set total=0.00&&goto output
if "!total:.=!"=="!total!" set total=!total:~0,-2!.!total:~-2!
if "%total:~0,1%" == "0" set total=%total:~1%&&goto finish
:output
if "!total:~0,1!"=="." (set %1=0!total!) else set %1=!total!
goto :eof

:comm
set outputvar=
set count=
if "!%1:.=!"=="!%1!" (set commavar=!%1!) else set commavar=!%1:~0,-3!
set lngth=%commavar%
set editvar=%commavar%
:length
if defined lngth (
set /a count+=1
set lngth=!lngth:~1!
goto length
)
if %count% leq 3 goto :eof
:splitter
if %count% leq 3 (
if defined editvar set editvar=%editvar%,
if "!%1:.=!"=="!%1!" (set %1=!editvar!!outputvar:~0,-1!) else set %1=!editvar!!outputvar:~0,-1!!%1:~-3!
goto :eof
)
set outputvar=!editvar:~-3!,!outputvar!
set editvar=!editvar:~0,-3!
set /a count-=3
goto splitter



0

Response Number 47
Name: CR
Date: December 18, 2008 at 18:27:48 Pacific
Reply:

Wow that is fast! -- and it works flawlessly under WinXP too :) Still can't test under Vista at the moment though but as stated before, I will post back with results when I can (unless someone else with Vista tests and posts back before me).

I am still gob-smacked at the sheer size of this script though to say what it does... You would have thought Microsoft would have provided an easier way to be fair!

Anyway, job well done I think :) Thank you for all your time with this and I am sorry for asking for such a huge job -- if only I had known how hard this was going to be initially I might have skipped actually asking the question hehe.


0

Response Number 48
Name: Judago
Date: December 18, 2008 at 19:12:04 Pacific
Reply:

I didn't have to answer or write the script if I choose not to, so don't worry about it :) I think I may have got a little obsessive over this one.

If they had of put heaps more effort into set /a I'm sure this would never have come up, when it comes down to it I get the idea that Microsoft probably want to do away with batch script, so no real surprise if we never see any great improvements to it.

I'm done with this script for now so no more messages filling up your inbox like spam:


You have received 4 new messages:

Your computing.net message has received a reply
Your computing.net message has received a reply
Your computing.net message has received a reply
Your computing.net message has received a reply



0

Response Number 49
Name: CR
Date: December 18, 2008 at 19:28:39 Pacific
Reply:

Hehe, to be honest I have really enjoyed this thread and looked forward to getting new emails about it :)

As for getting rid of batch files well I doubt it - just think of all the network admins out there, what would they do if they ever had to learn a 'proper' language :P

Anyway, it has been a good journey hehe and I like to think we all learnt something from it ;)

Thanks again matey, much appreciated :)


0

Response Number 50
Name: CR
Date: December 19, 2008 at 06:32:17 Pacific
Reply:

This is the part of the thread where you all go sigh :)

I found that I can export not only the current fixed disk setup of a PC but also show a full hardware specification too using psinfo.

You can get a hold of the above tool from http://technet.microsoft.com/en-us/...

psinfo -d > "%USERNAME%'s Hardware Summary.txt"

However, regardless of this little gem I have still learnt a massive amount from this thread and will be keeping the last incarnation of the batch for future projects as it is always good to look over and refresh on how stuff is done ;)

P.S.
What BBCode do I use to post code on this forum like you guys as opposed to simply using bold all the time?


0

Response Number 51
Name: Judago
Date: December 19, 2008 at 15:13:57 Pacific
Reply:

*sigh*

Third party executables always seem to make things so much easier and easy can often be better, especially if it's for work. No way like learning the hard way ;) As least we learnt something and, if nothing else, the script is academic in respect to the title of this thread.

I know I will be keeping the "divide massive numbers by 1024" part of the script, it seems to work into the many billions I even got something really unexpected(with the slow version):


--------------------
Data Execution Prevention - Microsoft Windows
--------------------

To help protect your computer, windows has closed this program.

Name: Windows Command Processor
Publisher: Microsoft Corporation

-------------------
Close message
-------------------


This was with the same number to about 1000 places, but still.

The code on these forums is mixture of plain text and html here so what I am basically using above is <b><pre><font color="#7f4000">. And of course closing the tags where desired.


0

Response Number 52
Name: Judago
Date: December 19, 2008 at 15:31:26 Pacific
Reply:

One thing I forgot to add, the line below may make the process slightly less annoying if psinfo is to be used constantly on other machines.


reg add HKEY_CURRENT_USER\Software\Sysinternals\PsInfo /v EulaAccepted /t reg_dword /d 00000001

This will delete the key when your finished:

reg delete HKEY_CURRENT_USER\Software\Sysinternals\PsInfo /f


0

Response Number 53
Name: CR
Date: December 19, 2008 at 15:34:51 Pacific
Reply:

Oooh, remember that comment about ESP! Well that was to be my next question - what a star :)


0

Response Number 54
Name: CR
Date: December 19, 2008 at 15:45:21 Pacific
Reply:

One thing, you know the following:

reg add HKEY_CURRENT_USER\Software\Sysinternals\PsInfo /v EulaAccepted /t reg_dword /d 00000001 

Well, if it already exists is there a way to have it over write silently without prompting the user to select Y/N without actuall making PsInfo_ADD.reg and PsInfo_DEL.reg files and calling them with REGEDIT /S?


0

Response Number 55
Name: Judago
Date: December 19, 2008 at 16:13:31 Pacific
Reply:

Try:


reg add HKEY_CURRENT_USER\Software\Sysinternals\PsInfo /v EulaAccepted /t reg_dword /d 00000001<nul

You can optionally output to >nul if you don't want anything displayed.


reg add HKEY_CURRENT_USER\Software\Sysinternals\PsInfo /v EulaAccepted /t reg_dword /d 00000001<nul>nul

[edit]

I just realised the above is equivalent to hitting no, which may be fine in some cases, the below will acutally overwrite:


echo y|reg add HKEY_CURRENT_USER\Software\Sysinternals\PsInfo /v EulaAccepted /t reg_dword /d 00000001

The y doesn't seem to cause any trouble if the key doesn't exist so this seems to be the one.


0

Response Number 56
Name: CR
Date: December 19, 2008 at 18:33:52 Pacific
Reply:

Nice one, that last line did the job nicely :)


0

Response Number 57
Name: BatchFreak
Date: December 20, 2008 at 22:54:03 Pacific
Reply:

Which part of thissscript divides by massive numbers?

I only Batch if possible, 2000 more lines of code, oh well.


0

Response Number 58
Name: Judago
Date: December 21, 2008 at 04:08:19 Pacific
Reply:

No part divides by massive numbers, everything from :div1024 to the following goto :eof divides massive numbers by 1024.

The script still relies on set /a, stepping around 32/64bit numbers by breaking the large number into chunks of 4 digits using variables. These chunks are then divided by subtraction, any remainder is tacted onto the start of the next chunk. The result for each chunk is then padded to 4 digits, with leading zeros, to uphold place value. This result is combined with the results from other chunks.

Although it does not perform proper floating point math it does give a result to two decimal places, this is acheived by multipling the input number by ten, by adding two zeros to the end if whole, else the input is padded or stripped to two decimal places, the decimal point is then removed.

When the division is finished a decimal point is added two places before the end, essentially dividing the result by ten. Due to this nature the result is not rounded, 1.079 and 1.071 would both be returned as 1.07.

The concept, and indeed most of the script, could be used to divide large number by numbers of a similar size to that I used above. My opinion is that numbers from a few hundred to about ten thousand should be practical divisors, anything above that may infringe on the limits of 32 bit numbers.

Additional decimal places could also be added to improve the accuracy of the end result.

If you want to continue this discussion I suggest starting a new thread as cr will continue to receive emails for this thread despite not being involved in the discussion. Who like scrolling down this far for both the listing and reply form anyway?


0

Response Number 59
Name: CR
Date: December 28, 2008 at 22:23:29 Pacific
Reply:

Hi, I am using the following to detect and show all fixed disk drives:

echo ^<u^>Fixed Disk Configuration^</u^> ^<br^> >> "%FILENAME%"
SETLOCAL ENABLEDELAYEDEXPANSION
for /f "delims=" %%a in ('fsutil fsinfo drives^|find ":"') do (
set disk=%%a
set disk=!disk:Drives: =!
for /f %%b in ('fsutil fsinfo drivetype !disk!^|find /i "fixed drive"') do (
call :sizer !disk!
)
)
endlocal
goto END_HDD_DETECTION

:sizer
for /l %%c in (1,1,6) do set part%%c=
for %%d in (sgbt sgbf sgbu smbu smbt smbf) do set %%d=
for /f "tokens=1,4,6 delims= " %%e in ('defrag -a %1 2^>^&1^|find /i "total"') do (
set sgbt=%%e
set sgbf=%%f
set pcfree=%%g
for /f "tokens=1,2 delims=." %%h in ("%%e") do (
set part1=%%h
set part2=%%i
)
for /f "tokens=1,2 delims=." %%j in ("%%f") do (
set part3=%%j
set part4=%%k
)
)
if not defined sgbt exit /b
if not defined part4 set part4=00
if not defined part2 set part2=00
set pcfree=%pcfree:~1,-2%
set /a pcused=100-pcfree
set /a part5=part1-part3
set /a part6=1%part2%-1%part4%
if %part6% lss 0 set /a part5-=1&&set /a part6+=100
if %part6% equ 0 (set sgbu=%part5%) else set sgbu=%part5%.%part6%
for /l %%l in (1,1,6) do set /a part%%l*=1024
if not %part6% equ 0 set /a smbu=%part5%+%part6:~0,-2%
if not %part6% equ 0 (set smbu=%smbu%.%part6:~-2%) else set smbu=%part5%
if not %part4% equ 0 set /a smbf=%part3%+%part4:~0,-2%
if not %part4% equ 0 (set smbf=%smbf%.%part4:~-2%) else set smbf=%part3%
if not %part2% equ 0 set /a smbt=%part1%+%part2:~0,-2%
if not %part2% equ 0 (set smbt=%smbt%.%part2:~-2%) else set smbt=%part1%
for %%m in (u f t) do call :comm smb%%m

echo %1 Drive^<br^> >> "%FILENAME%"
echo Total size: !sgbt! GB (!smbt! MB) ^<br^> >> "%FILENAME%"
echo Total used: !sgbu! GB (!smbu! MB) !pcused!%% ^<br^> >> "%FILENAME%"
echo Total free: !sgbf! GB (!smbf! MB) !pcfree!%% ^<br^> >> "%FILENAME%"
echo ^<br^> >> "%FILENAME%"
goto :eof

:comm
@echo off
set outputvar=
SET COUNT=0
if "!%1:.=!"=="!%1!" (set commavar=!%1!) else SET COMMAVAR=!%1:~0,-3!
SET LNGTH=%COMMAVAR%
SET EDITVAR=%COMMAVAR%
goto length

:LENGTH
IF NOT DEFINED LNGTH (
if %count% leq 3 goto :eof
GOTO splitter
)
SET /A COUNT+=1
SET LNGTH=%LNGTH:~1%
GOTO LENGTH

:SPLITTER
IF %COUNT% Leq 3 GOTO OUTPUT
SET OUTPUTVAR=%EDITVAR:~-3%,%OUTPUTVAR%
SET EDITVAR=%EDITVAR:~0,-3%
SET /A COUNT-=3
GOTO SPLITTER

:OUTPUT
IF DEFINED EDITVAR SET EDITVAR=%EDITVAR%,
if "!%1:.=!"=="!%1!" (set %1=%EDITVAR%%OUTPUTVAR:~0,-1%) else set %1=%EDITVAR%%OUTPUTVAR:~0,-1%!%1:~-3!
goto :eof

:END_HDD_DETECTION

However, I also need to show all removable drives too - I have been using the following but this only shows the drive letter, as opposed to the drive letter, total size, total used, and total free.

:: Flash Drives
echo ^<u^>Also detected the following removable drives:^</u^> ^<br^> >> "%FILENAME%"
for /f "tokens=1,2 delims=\ " %%a in ('fsutil fsinfo drives^|more/e/t0') do (
if "%%b"=="" (set usb=%%a) else (set usb=%%b)
fsutil fsinfo drivetype !usb!|find "Removable Drive">nul 2>&0 &&echo !usb!\ Drive^<br^> >> "%FILENAME%"
)

My question is, can the 2nd chunk of code be added to the first lot to also show the extra information as it does for all fixed drives? - like so:

Fixed Disk Configuration
C:\ Drive
Total size: 186 GB (190,464 MB)
Total used: 58 GB (59,392 MB) 32%
Total free: 128 GB (131,072 MB) 68%

D:\ Drive
Total size: 298 GB (305,152 MB)
Total used: 240.6 GB (245,821.44 MB) 81%
Total free: 57.94 GB (59,330.56 MB) 19%

as opposed to:

Also detected the following removable drives:
I:\ Drive
J:\ Drive

PS
The html tags are needed as I am outputting to a .HTML file ;)


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Show size of HDD in Mb using batch

size of array in VB www.computing.net/answers/programming/size-of-array-in-vb/10262.html

Rename multiple iles in folder using Batch www.computing.net/answers/programming/rename-multiple-iles-in-folder-using-batch/19638.html

Show hardware list using batch www.computing.net/answers/programming/show-hardware-list-using-batch/17412.html