Pre-occupy file space

Everything else that doesn't fall into one of the other PB categories.
User avatar
jacdelad
Addict
Addict
Posts: 1992
Joined: Wed Feb 03, 2021 12:46 pm
Location: Riesa

Pre-occupy file space

Post 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.
Good morning, that's a nice tnetennba!

PureBasic 6.21/Windows 11 x64/Ryzen 7900X/32GB RAM/3TB SSD
Synology DS1821+/DX517, 130.9TB+50.8TB+2TB SSD
PBJim
Enthusiast
Enthusiast
Posts: 294
Joined: Fri Jan 19, 2024 11:56 pm

Re: Pre-occupy file space

Post 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
User avatar
jacdelad
Addict
Addict
Posts: 1992
Joined: Wed Feb 03, 2021 12:46 pm
Location: Riesa

Re: Pre-occupy file space

Post by jacdelad »

Thanks, I'll look into that.
Good morning, that's a nice tnetennba!

PureBasic 6.21/Windows 11 x64/Ryzen 7900X/32GB RAM/3TB SSD
Synology DS1821+/DX517, 130.9TB+50.8TB+2TB SSD
miskox
Enthusiast
Enthusiast
Posts: 107
Joined: Sun Aug 27, 2017 7:37 pm
Location: Slovenia

Re: Pre-occupy file space

Post 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
BarryG
Addict
Addict
Posts: 4127
Joined: Thu Apr 18, 2019 8:17 am

Re: Pre-occupy file space

Post 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.
User avatar
blueb
Addict
Addict
Posts: 1111
Joined: Sat Apr 26, 2003 2:15 pm
Location: Cuernavaca, Mexico

Re: Pre-occupy file space

Post 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)

- It was too lonely at the top.

System : PB 6.21(x64) and Win 11 Pro (x64)
Hardware: AMD Ryzen 9 5900X w/64 gigs Ram, AMD RX 6950 XT Graphics w/16gigs Mem
Post Reply