Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Converting KB to MB for use with new systems

Solo5300Solo5300 Posts: 3Member
I am new to this fourm as well to programming. I was just starting to dig thru my old software and found my old Turbo Pascal 7 that I got back in 93. Well to make a long story short I am now understanding the very basics after about 3 days and from examples and thoughts I know what I want to do but not sure how to get there. I would like to use a disk check utility that will display MB instead of KB I have tried everything from changing the order of the code to rewriting until now this is what I have.
{ Date: 04/01/03 }
{ Description: Gives total Drive Capacity & Total Free capacity on a list of drives. }
{ For Windows: }
{ uses WinDos, WinCrt; }

uses Dos, crt;

begin
clrscr;
{Basic calculation for conversion reasons.}
Writeln('1 Kilobyte KB = 0.000977 Megabyte MB');
Writeln('Drive A & B are not counted as a drive due to being removable.');
Writeln('For use with system above Pentium 1 class Processors due to the');
Writeln('software limitations.');
Writeln('');

{Drive C:}
Writeln(DiskSize(3) div 1024, ' Kbytes total capacity on drive c:');
Writeln(DiskFree(3) div 1024, ' Kbytes free capacity on drive c:');
Writeln(' ');

{Drive D:}
Writeln(DiskSize(4) div 1024, ' Kbytes total capacity on drive d:');
Writeln(DiskFree(4) div 1024, ' Kbytes free capacity on drive d:');
Writeln(' ');

{Drive E:}
Writeln(DiskSize(5) div 1024, ' Kbytes total capacity on drive e:');
Writeln(DiskFree(5) div 1024, ' Kbytes free capacity on drive e:');
Writeln(' ');
end.

Well at this point I am stupified and don't know where to go from here. Output looks great until one gets into the high capacity drives.
Exp. 999934 Kbytes total capacity on drive c: <-- I don't like Kbytes as an answer.

I know that this has been drawn out and a long Question But any Ideas would be great. I know someone out there has done this before but I am just starting out this way.

Thanks,
Solo5300


Comments

  • ManningManning Posts: 1,621Member
    : Well at this point I am stupified and don't know where to go from here. Output looks great until one gets into the high capacity drives.
    : Exp. 999934 Kbytes total capacity on drive c: <-- I don't like Kbytes as an answer.

    If you look at the value of the various measurements you'll see a pattern:

    1 kilobyte = 1024 bytes
    1 megabyte = 1024 kilobytes
    1 gigabyte = 1024 megabytes
    1 terrabyte = 1024 gigabytes

    So basically, whenever you want to move up to the next unit of measurement you divide by 1024. In this case, the number of kilobytes / 1024 will give you the number of megabytes.
  • Solo5300Solo5300 Posts: 3Member
    : : Well at this point I am stupified and don't know where to go from here. Output looks great until one gets into the high capacity drives.
    : : Exp. 999934 Kbytes total capacity on drive c: <-- I don't like Kbytes as an answer.
    :
    : If you look at the value of the various measurements you'll see a pattern:
    :
    : 1 kilobyte = 1024 bytes
    : 1 megabyte = 1024 kilobytes
    : 1 gigabyte = 1024 megabytes
    : 1 terrabyte = 1024 gigabytes
    :
    : So basically, whenever you want to move up to the next unit of measurement you divide by 1024. In this case, the number of kilobytes / 1024 will give you the number of megabytes.
    :

    Okay I get the basic idea but the math is what I am not able to solve. I have tried divide by all types of #'s and the answer I have gotten so far is 9.764e+2. This is nowhere correct due to the drive I am working with is software measured 4.87GB. A little more of a hint would be great but without totally giving away the farm if you know what I mean. Thanks again Solo5300
  • zibadianzibadian Posts: 6,349Member
    : : : Well at this point I am stupified and don't know where to go from here. Output looks great until one gets into the high capacity drives.
    : : : Exp. 999934 Kbytes total capacity on drive c: <-- I don't like Kbytes as an answer.
    : :
    : : If you look at the value of the various measurements you'll see a pattern:
    : :
    : : 1 kilobyte = 1024 bytes
    : : 1 megabyte = 1024 kilobytes
    : : 1 gigabyte = 1024 megabytes
    : : 1 terrabyte = 1024 gigabytes
    : :
    : : So basically, whenever you want to move up to the next unit of measurement you divide by 1024. In this case, the number of kilobytes / 1024 will give you the number of megabytes.
    : :
    :
    : Okay I get the basic idea but the math is what I am not able to solve. I have tried divide by all types of #'s and the answer I have gotten so far is 9.764e+2. This is nowhere correct due to the drive I am working with is software measured 4.87GB. A little more of a hint would be great but without totally giving away the farm if you know what I mean. Thanks again Solo5300
    :
    This is a limitation set by the size of the integer type. Because a longint is a signed 32 bit number, it means that the largest number you'll get is 2^31-1, which is about 2 GB. If the number is larger than that the variable will overflow and start at the lowest value (-2GB) and count upwards again. So a 4.87 GB drive will show as a about a 0.87 GB drive or about 900 MB.
    Unfortunately there is no way of knowing how often a variable has overflown its maximum, thus a 900 MB drive could be 900 MB, 4.87 GB, 8.87 GB, etc. There is a very slow way to determine the size of any drive, as long as the free space is less than 2 GB: Add together the sizes of all the files and the free space.
  • Solo5300Solo5300 Posts: 3Member
    : : : : Well at this point I am stupified and don't know where to go from here. Output looks great until one gets into the high capacity drives.
    : : : : Exp. 999934 Kbytes total capacity on drive c: <-- I don't like Kbytes as an answer.
    : : :
    : : : If you look at the value of the various measurements you'll see a pattern:
    : : :
    : : : 1 kilobyte = 1024 bytes
    : : : 1 megabyte = 1024 kilobytes
    : : : 1 gigabyte = 1024 megabytes
    : : : 1 terrabyte = 1024 gigabytes
    : : :
    : : : So basically, whenever you want to move up to the next unit of measurement you divide by 1024. In this case, the number of kilobytes / 1024 will give you the number of megabytes.
    : : :
    : :
    : : Okay I get the basic idea but the math is what I am not able to solve. I have tried divide by all types of #'s and the answer I have gotten so far is 9.764e+2. This is nowhere correct due to the drive I am working with is software measured 4.87GB. A little more of a hint would be great but without totally giving away the farm if you know what I mean. Thanks again Solo5300
    : :
    : This is a limitation set by the size of the integer type. Because a longint is a signed 32 bit number, it means that the largest number you'll get is 2^31-1, which is about 2 GB. If the number is larger than that the variable will overflow and start at the lowest value (-2GB) and count upwards again. So a 4.87 GB drive will show as a about a 0.87 GB drive or about 900 MB.
    : Unfortunately there is no way of knowing how often a variable has overflown its maximum, thus a 900 MB drive could be 900 MB, 4.87 GB, 8.87 GB, etc. There is a very slow way to determine the size of any drive, as long as the free space is less than 2 GB: Add together the sizes of all the files and the free space.
    :

    Thats why what ever I seem to do is rejected and basicly gives the same answer or an extended view of the same answer. Great that brings me a step closer to trying to figure out what I am doing...

    Thanks Again
    Solo5300

  • Phat NatPhat Nat Posts: 757Member
    : : : : : Well at this point I am stupified and don't know where to go from here. Output looks great until one gets into the high capacity drives.
    : : : : : Exp. 999934 Kbytes total capacity on drive c: <-- I don't like Kbytes as an answer.
    : : : :
    : : : : If you look at the value of the various measurements you'll see a pattern:
    : : : :
    : : : : 1 kilobyte = 1024 bytes
    : : : : 1 megabyte = 1024 kilobytes
    : : : : 1 gigabyte = 1024 megabytes
    : : : : 1 terrabyte = 1024 gigabytes
    : : : :
    : : : : So basically, whenever you want to move up to the next unit of measurement you divide by 1024. In this case, the number of kilobytes / 1024 will give you the number of megabytes.
    : : : :
    : : :
    : : : Okay I get the basic idea but the math is what I am not able to solve. I have tried divide by all types of #'s and the answer I have gotten so far is 9.764e+2. This is nowhere correct due to the drive I am working with is software measured 4.87GB. A little more of a hint would be great but without totally giving away the farm if you know what I mean. Thanks again Solo5300
    : : :
    : : This is a limitation set by the size of the integer type. Because a longint is a signed 32 bit number, it means that the largest number you'll get is 2^31-1, which is about 2 GB. If the number is larger than that the variable will overflow and start at the lowest value (-2GB) and count upwards again. So a 4.87 GB drive will show as a about a 0.87 GB drive or about 900 MB.
    : : Unfortunately there is no way of knowing how often a variable has overflown its maximum, thus a 900 MB drive could be 900 MB, 4.87 GB, 8.87 GB, etc. There is a very slow way to determine the size of any drive, as long as the free space is less than 2 GB: Add together the sizes of all the files and the free space.
    : :
    :
    : Thats why what ever I seem to do is rejected and basicly gives the same answer or an extended view of the same answer. Great that brings me a step closer to trying to figure out what I am doing...
    :
    : Thanks Again
    : Solo5300

    Well, this isn't perfect either, but it will get you farther. This code will handle up to 2gig drives, part of the problem being that Pascal's largest variable, LongInt, will only go up to 2,147,483,647 (or 2.1 gig)
    To go larger than this, you would have to create your own variable and I don't think this function will get higher values anyways, HOWEVER...
    With this, if the drive size is > 2.1gig then it returns a size of 1 byte (since you will never find a 1 byte drive). If it returned 1, then you could implement zibadian's idea.

    Anyways, here's the code:
    [code]
    {
    BO BENDTSEN

    Many people don't think about it, but DOS is limited to report more than
    1 gigabyte. I have a 1.3 and a 1.0 gig, and made these routines for my
    programs for knowing if the drive size is more than 1 gig. Using the normal
    DiskSize and DiskFree could get you strange result, sometimes it could report
    maybe 100MB when it is really 1 gig.

    If the size of free space is 1 you can assume that the drive is more than 1
    gigabyte.
    }
    USES Dos;

    VAR
    X : Byte;

    Function DriveSize(d : byte) : Longint; { -1 not found, 1=>1 Giga }
    Var
    R : Registers;
    Begin
    With R Do
    Begin
    ah := $36;
    dl := d;
    Intr($21, R);
    If AX = $FFFF Then
    DriveSize := -1 { Drive not found }
    Else
    If (DX = $FFFF) or
    (Longint(ax) * cx * dx = 1073725440) Then
    DriveSize := 1
    Else
    DriveSize := Longint(ax) * cx * dx;
    End;
    End;

    Function DriveFree(d : byte) : Longint; { -1 not found, 1=>1 Giga }
    Var
    R : Registers;
    Begin
    With R Do
    }
    USES Dos;

    VAR
    X : Byte;

    Function DriveSize(d : byte) : Longint; { -1 not found, 1=>1 Giga }
    Var
    R : Registers;
    Begin
    With R Do
    Begin
    ah := $36;
    dl := d;
    Intr($21, R);
    If AX = $FFFF Then
    DriveSize := -1 { Drive not found }
    Else
    If (DX = $FFFF) or
    (Longint(ax) * cx * dx = 1073725440) Then
    DriveSize := 1
    Else
    DriveSize := Longint(ax) * cx * dx;
    End;
    End;

    Function DriveFree(d : byte) : Longint; { -1 not found, 1=>1 Giga }
    Var
    R : Registers;
    Begin
    With R Do
    Begin
    ah := $36;
    dl := d;
    Intr($21, R);
    If AX = $FFFF Then
    DriveFree := -1 { Drive not found }
    Else
    If (BX = $FFFF) or (Longint(ax) * bx * cx = 1073725440) Then
    DriveFree := 1
    Else
    DriveFree := Longint(ax) * bx * cx;
    End;
    End;

    Begin
    For X := 3 to 26 Do
    If DriveSize(X) > 0 Then
    Begin
    WriteLn('Drive ',Chr(X+64),':');
    WriteLn(' ',DriveSize(X),' bytes');
    WriteLn(' ',DriveFree(X),' bytes');
    End;
    End.
    [/code]

    Phat Nat

  • ManningManning Posts: 1,621Member
    As others pointed out, your problem is the 32bit variable. If you are using a compiler that can call WinAPI functions, GetDiskFreeSpaceEx() uses LARGE_INTEGER, which is 64bit, so can easily handle the size of drives we use today. So if you have the ability, switch to using that function instead. If you are using a DOS compiler I guess that's not an option though.
Sign In or Register to comment.