Pascal

Moderators: None (Apply to moderate this forum)
Number of threads: 4106
Number of posts: 14016

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Converting KB to MB for use with new systems Posted by Solo5300 on 1 Apr 2003 at 6:33 PM
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


Report
Re: Converting KB to MB for use with new systems Posted by Manning on 1 Apr 2003 at 7:20 PM
: 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.
Report
Re: Converting KB to MB for use with new systems Posted by Solo5300 on 1 Apr 2003 at 8:38 PM
: : 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
Report
Re: Converting KB to MB for use with new systems Posted by zibadian on 1 Apr 2003 at 9:29 PM
: : : 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.
Report
Re: Converting KB to MB for use with new systems Posted by Solo5300 on 1 Apr 2003 at 9:44 PM
: : : : 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

Report
Re: Converting KB to MB for use with new systems Posted by Phat Nat on 2 Apr 2003 at 12:09 AM
: : : : : 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:
{
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.


Phat Nat

Report
Re: Converting KB to MB for use with new systems Posted by Manning on 2 Apr 2003 at 2:59 PM
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.



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.