VBS Simple Script Help

Joined
25 Sep 2011
Posts
3,859
Hey Guys,

Been years since i've done any vb stuff and google isnt helping much.

I have the following:

strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colDisks = objWMIService.ExecQuery _
("Select * from Win32_LogicalDisk Where DeviceID = 'C:'")
For Each objDisk in colDisks
intFreeSpace = objDisk.FreeSpace
intTotalSpace = objDisk.Size
pctFreeSpace = intTotalSpace / 100 * 35
Wscript.Echo pctFreeSpace
Next

This outputs the value of 35% of the free disk space on C.

However it outputs it as "36846225203.2". I need this output as just the first 5 digits (36846) and I cannot figure out how.

Any help would be much appreciated.
 
Associate
Joined
16 Apr 2007
Posts
2,208
Would you not just divide
pctFreeSpaceInBytes = intTotalSpace / 100 * 35
pctFreeSpaceInKB = pctFreeSpaceInBytes /1024
pctFreeSpaceInMB = pctFreeSpaceInKB/1024
Wscript.Echo pctFreeSpaceInMB


or similar.
 
Joined
25 Sep 2011
Posts
3,859
If you need a smaller number, why not calculate a smaller percentage?

I need that specific percentage (just part of a bigger script).


Yeah I did see that link in my googling but couldn't figure it out.

I'm now a very noob scripter having forgotten almost everything I use to know!

Anyway, it's now working as I need with Six's suggestion.

Thanks for the help.
 
Soldato
Joined
28 Oct 2006
Posts
12,456
Location
Sufferlandria
I need that specific percentage (just part of a bigger script).

But you're not taking that specific percentage?

Using the first 5 digits of a number is not the same as rounding the number.

Consider these free space values:
36846225203 (the current free space indicated by your calculation)
399954532 (a future amount of free space after you've filled up the drive a bit)

Your code is going to indicate that the second number has more free space than the first when you take only the first 5 digits of each.
 
Last edited:
Joined
25 Sep 2011
Posts
3,859
Yes I see what you mean touch - and you are correct.

However for the purpose of this, it shouldn't actually matter..

The number outputted here is then taken and inputted in to set the cache size for the sccm client.

The client itself works on mb so just taking the first 5 digits will be fine as this does not need to be an exact or specific figure, just a rough percentage.

Have now tested on numerous machines its working as expected.
 
Soldato
Joined
28 Oct 2006
Posts
12,456
Location
Sufferlandria
The client itself works on mb so just taking the first 5 digits will be fine as this does not need to be an exact or specific figure, just a rough percentage.

That's kind of what i meant with my first suggestion of using a different percentage, I just didnt explain it very well.

1 byte is 0.000001 megabytes

so, rather than calculating 35% and getting an output in bytes, you could calculate 0.0000035% and get the output in megabytes.

(alternatively, you could take the 35% figure and divide it by 100000 to get it in MB)
 
Back
Top Bottom