Page 1 of 1

Pre-occupy file space

Posted: Wed May 01, 2024 9:54 am
by jacdelad
Hi,
this may be a stupid question: Is there a special function to preoccupy size on the disk when writing a file? I mean, like the Windows Explorer does on copying (when I copy a big file the remaining disk size already reduces by the file's size when the explorer opens the file). I know I can open the file and fill it with zeros, then fill the real content. But this way it is written twice, so I am sure there's an API for that or something.

Re: Pre-occupy file space

Posted: Wed May 01, 2024 10:52 am
by PBJim
It's a perfectly reasonable question — the answer is yes, you can create a sparse file, a provision which exists in NTFS and also widely used in Unix.

It is necessary to first create the file and then call DeviceIoControl http://msdn.microsoft.com/en-us/library ... 85%29.aspx Set the option FSCTL_SET_SPARSE IOCTL https://learn.microsoft.com/en-us/windo ... dfrom=MSDN

Re: Pre-occupy file space

Posted: Wed May 01, 2024 4:31 pm
by jacdelad
Thanks, I'll look into that.

Re: Pre-occupy file space

Posted: Fri May 03, 2024 5:02 pm
by miskox
FSUTIL can do this. So there must be an API or something... I am not good at finding it.

https://learn.microsoft.com/en-us/windo ... sutil-file

Code: Select all

fsutil file createnew 'file_name' 'length'
Saso

Re: Pre-occupy file space

Posted: Sat May 04, 2024 4:18 am
by BarryG
From my archives:

Code: Select all

Procedure CreateEmptyFile(file$,bytes.q)
  f=CreateFile(#PB_Any,file$)
  If f
    SetFilePointer_(FileID(f),bytes,0,#FILE_BEGIN)
    ok=SetEndOfFile_(FileID(f))
    CloseFile(f)
  EndIf
  ProcedureReturn ok
EndProcedure

Debug CreateEmptyFile("D:\1-MB-File.txt",1048576)
It gets filled with 0 bytes.

Re: Pre-occupy file space

Posted: Sat May 04, 2024 12:53 pm
by blueb
Reminds me of a time when PureBasic was so small that users were forced to do this... :mrgreen:

Code: Select all

;==================================================================
;
; Author:   Rings      
; Date:     September 9th, 2009
;           
; Typical Usage: 
;      If you want to appear competitive in the eyes of a user who might have concluded 
;      that a 30Mb download must be really something special compared to 100kb download. 
;      Then...Bloat away!
;==================================================================
Macro BloatMe(_LONGS_)
! jmp SR

!    rept _LONGS_ counter
!     {
;!        byte#counter db counter
!       dword#counter dd counter
!     }
!SR:

EndMacro

BloatMe(100000) ; Add Bloat - (adds nearly 400 kb)

MessageRequester("BLOATING", "Created a file (this is nearly 400k in size)",0)