Link to home
Start Free TrialLog in
Avatar of RJSoft
RJSoft

asked on

GetDiskFreeSpaceEx problems

Hello;

I am trying to create a function that will absorb all the free space that is available on a pc drive.

I am using GetDiskFreeSpaceEx.

The function like the one pasted below will nearly get all of the free space on my pc C: drive except 12.8 megs.

Also I turned off the option to automaticly call Disk Cleanup. As the Cleanup utility on Windows 98 automatically calls this up. But even with this option off it will still call up Disk Cleanup. Disk Cleanup utility gives the option to remove temporary internet files etc...

The sample below is a stripped down version of the actual code. The actual function is called repeatedly by a timer and creates multiple files up to 10 meg in size depending upon remaining disk space.

It works fine until it reaches 12.8 meg of free disk space left and quits.

The example below only creates one file but the code is basically the same.

Any comments or clues are apreciated.
RJ

int MyDialog::SqeezeDisk()
{
ULARGE_INTEGER fba, tnob;
GetDiskFreeSpaceEx(Drv,&fba,&tnob,NULL);

//Assign the free space available to this thread
unsigned _int64 Size = fba.QuadPart;

unsigned _int64 Wrote=0;
unsigned _int64 Sub=0;
char *ch;

MakeNewFileName(FileName);

FILE *fptr=NULL;
fptr=fopen(FileName,"wb");
if(fptr==NULL)
{
      MessageBox("ERROR failed to create file",FileName);
      return 1;
}//endif
//

////////////////////////WRITE FILE LOOP
unsigned _int64 x=0;

while(1)
{
   //Calculate largest size to write
      
      if(Size >= (unsigned _int64)10000)
            Sub=(unsigned _int64)10000;
      if(Size <  (unsigned _int64)10000 && Size >= (unsigned _int64)1000)
            Sub=(unsigned _int64)1000;
      if(Size <  (unsigned _int64)1000  && Size >= (unsigned _int64)100)
            Sub=(unsigned _int64)100;
      if(Size <  (unsigned _int64)100   && Size >= (unsigned _int64)10)
            Sub=(unsigned _int64)10;
      if(Size <  (unsigned _int64)10    && Size >= (unsigned _int64)1)
            Sub=(unsigned _int64)1;

      if(Size <=0)
      {
            flag=0;//not bad but done
            break; /////////ALL DISK SPACE IS USED UP SO BREAK!!!!
      }

      //create mem space to write
      ch = new char[Sub];

      //fill string with junk chars
      x=0;
      while(1)
      {
            ch[x]='z';
            x++;
            if(x==Sub)break;
      }//endwhile
      
      //write the string
      if(fwrite(ch,sizeof(char)*(int)Sub,1,fptr)<1) //was==-1
      {
            fclose(fptr);
            delete [] ch;
            return 1;
      }//endif

delete [] ch;
Wrote+=Sub;

//See if Wrote is same as or greater than free space
if((unsigned _int64)Wrote>=Size)break;

//recalculate free space
Size-=(unsigned _int64)Sub;

//check if done, no more space
if(Size<=(unsigned _int64)0)break;
}//endwhile
/////////////////////////END WRITE FILE LOOP

fclose(fptr);
//
return 0; //good
}//endfunc

Avatar of carribus
carribus

I'm not sure if this is the right answer, but the third parameter to GetDiskFreeSpaceEx() is an out parameter that will return ALL the free space on the drive, as opposed to parameter 1, which will return all space available on the drive available to the current user context.

Try and compare whether the two values are equal to the difference of 12.8 Mb you mentioned earlier...
Writing chunks of 10000 bytes will fail if there isn't contiguous space of 10 k or bigger. You could try to decrease chunk size _AFTER_ failing and close the file if a write with the smallest chunk size fails.

Or you could try to defrag the disk before running the prog.

Regards, Alex

BTW, why are you doing that? Do you want to convince your customers to buy larger hard disks?
Avatar of RJSoft

ASKER

Hello all;

>>Writing chunks of 10000 bytes will fail if there isn't contiguous space of 10 k or bigger.

Yeah that's what the if statements are supposedly covering. The size of the write is reduced continuously until it reaches 10 bytes and drops to 1 repeatedly. It is supposed to suck up all of the hard drive space.

>>You could try to decrease chunk size _AFTER_ failing and close the file if a write with the smallest chunk size fails.

Smallest chunk is 1 and the logic is supposed to cover it. But somehow not.

>>Or you could try to defrag the disk before running the prog.

Na.

>>Regards, Alex

>>BTW, why are you doing that? Do you want to convince your customers to buy larger hard disks?

LoL! Good one. Maybe hmmm....Hey I like it!
(only joking:))

RJ
Have you tried checking what the return value is for the third parameter of GetDiskFreeSpaceEx() ?
SOLUTION
Avatar of rhodgson
rhodgson

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of RJSoft

ASKER

carribus.

Yes, I was hoping things would add up as you suspected. But not.

(At least not as I am doing the addition, using values reported from GetDiskFreeSpaceEx and comparing with a Dos prompt dir command.)

Also, I assume that I am only allowed to create files in the permission status of my main thread. So if I continue on in my current direction I may need to devise a way to chgmod to the super user in Windows (If there is such a thing). In short how do I change my priority level?

jamie; Thanks as usual. Good examples.

rhodgson; Exactly it!

You see I have a scraper type program that downloads any type of media file on any subject. But I have found that it can sometime download *wrong files and *very very wrong files.

BEGIN RANT>>>>>>>

The users of my application can have the best of intentions, just that there is so much trashy stuff on the internet. Good or acceptable stuff has links leads to other suff. So sometimes my app produces "good" results and sometimes bad. Stuff that is just plain wrong. If you get my drift.

Take for example if I where to start a search for jpegs of "nude women". I set the depth level (recursive searches (links leading to links) ) to 5. For each link that is found from my search engine query tool is parsed for links and files and is re-searched to the level of 5 deep. The depth level keeps the recursion from going on forever.

I am almost guaranteed to get some files that I consider hideous allong with many good ones. To think that I have spent all this time to produce a product that can even sometimes produce these kind of results sickens me to no end.

Some things are so gross and so wrong, that they should never be seen. Not even an accidental glimpse. I am sure you can understand.

But what am I to do?

I know that I am not responsible for what is on the internet. So the most I figure that I can do is add the functionally of a disk washer type utility. Which is basically what SDelete does.

This is a good solution and I thank you for it. But it does not sooth my ethical/moral delema.

I find that it is pointless to filter keywords during the search process as ordinary words lead to the un-wanted links. Un-wanted links are usualy not advertised with keywords to help specify thier content so they slip in via the norm. So negative keywords are pointless.

Truthfully, I dont know what to think. On one hand I would like to provide a file washer/ disk washer to help good people from being associated with bad stuff. But on the other hand I dont want to provide a method for bad people to do wrong stuff.

Sometimes I just want to kill the whole project. Wash my hands of it. But I have spent so much time on it. So much sacrifice.

Wondering what any of you think?

END RANT>>>>>>

Thanks
RJ
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of RJSoft

ASKER

carribus
Thanks!
RJ
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of RJSoft

ASKER

Alex; Thanks I will give it a try.
RJ