Reply to comment

Using Sparse Files sample

Sparse files are only supported under Windows 2000 and later and the file must be on a volume that is NTFS 5.0 or later.

MSDN links:

The code below sets the SPARSE attribute and zeros all the data in the file.

This is sample code. Add error handling and adjust to your requirements as necessary.

CLEAR
#DEFINE OPEN_EXISTING                   3
#DEFINE GENERIC_WRITE          1073741824  && 0x40000000
#DEFINE INVALID_HANDLE_VALUE           -1
#define FILE_ANY_ACCESS           0
#define FILE_WRITE_ACCESS         0x00000002
#define FILE_DEVICE_FILE_SYSTEM   0x00000009
 
#define FSCTL_SET_SPARSE BITLSHIFT(FILE_DEVICE_FILE_SYSTEM, 16) + ;
					BITLSHIFT(FILE_ANY_ACCESS,14) + BITLSHIFT(49, 2) 
#define FSCTL_SET_ZERO_DATA BITLSHIFT(FILE_DEVICE_FILE_SYSTEM, 16) + ;
					BITLSHIFT(FILE_WRITE_ACCESS,14) + BITLSHIFT(50, 2)
 
= DeclareAPI()
 
lcFileName = "H:\tmp\Copy1.dbf" + CHR(0)
 
hFile = CreateFile(lcFileName ,	GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0)
 
IF hFile == INVALID_HANDLE_VALUE
	? "Error Opening file", Apierror(GetLastError())
	RETURN .F.
ENDIF
? "File handle returned: ", hFile
lnSize = GetFileSize(hFile, 0)
lnCompSize = GetCompressedFileSize(lcFileName , 0)
? "Size before", lnSize, lnCompSize
lnBytesReturned = 0
lnResult = DeviceIoControl(hFile, FSCTL_SET_SPARSE, Null,0,0,0, @lnBytesReturned,0)
IF lnResult = 0
	? "Set Sparse Error", Apierror(GetLastError())
	RETURN .F.
ENDIF	
lcFILE_ZERO_DATA_INFORMATION = 	Num2Long(0) + Num2Long(0) + Num2Long(lnSize) + Num2Long(0) 
lnBytesReturned = 0
lnResult = DeviceIoControl(hFile, FSCTL_SET_ZERO_DATA, ;
				@lcFILE_ZERO_DATA_INFORMATION, 16,0,0, @lnBytesReturned,0)
IF lnResult = 0
	? "Zero Data Error", Apierror(GetLastError())
	RETURN .F.
ENDIF	
lnSize = GetFileSize(hFile, 0)
lnCompSize = GetCompressedFileSize(lcFileName , 0)
? "Size after", lnSize, lnCompSize
= CloseHandle (hFile)
RETURN
*----------------------------------------------------------------
 
FUNCTION Num2LOng(tnNum)
DECLARE RtlMoveMemory IN WIN32API AS RtlCopyLong ;
	STRING @Dest, Long @Source, Long Length
LOCAL lcString
lcString = SPACE(4)
=RtlCopyLong(@lcString, BITOR(tnNum,0), 4)
RETURN lcString
 
FUNCTION Apierror(tnErrorCode)
Local lcErrBuffer, lcErrorMess, liNewErr, lnErrorCode
lcErrBuffer = REPL(CHR(0),1000)
lnErrorCode = tnErrorCode
liNewErr = FormatMessage(0x1000;
	 	,.NULL., lnErrorCode, 0, @lcErrBuffer,500,0)
lcErrorMess = Transform(lnErrorCode) + "    " + LEFT(lcErrBuffer, AT(CHR(0),lcErrBuffer)- 1 )
RETURN lcErrorMess
 
FUNCTION DeclareAPI
DECLARE Long DeviceIoControl  IN kernel32 ;
		Long hDevice, Long dwIoControlCode, ;
		String @ lpInBuffer, Long nInBufferSize, ;
		Long lpOutBuffer, Long nOutBufferSize, ;
		Long @ lpBytesReturned, Long lpOverlapped
DECLARE Long GetLastError IN kernel32
Declare Long FormatMessage In kernel32.dll as FormatMessage ;
		Long dwFlags, String @lpSource, Long dwMessageId, ;
		Long dwLanguageId, String @lpBuffer, Long nSize, ;
		Long Arguments
DECLARE Long CreateFile IN kernel32 ;
		STRING    lpFileName, Long   dwDesiredAccess, Long   dwShareMode, ;
		Long   lpSecurityAttr, Long   dwCreationDisp, Long   dwFlagsAndAttrs, ;
		Long   hTemplateFile
DECLARE Long CloseHandle IN kernel32 Long hObject
DECLARE Long GetFileSize IN kernel32 ;
		Long hFile , Long @ lpFileSizeHigh
DECLARE Long GetCompressedFileSize IN kernel32 ;
		String FileName , Long @ lpFileSizeHigh
RETURN

Reply

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <java>, <powershell>, <tsql>, <visualfoxpro>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.
  • Web page addresses and e-mail addresses turn into links automatically.

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image.