Code: Select all
If CreateFile(0, "FREE_SPACE")
For a=1 To 1
Repeat
WriteByte(0, 0) ; Write 0 byte to 'FREE_SPACE' file
ForEver ; Until FreeDiskSpace = 0 byte
Next
CloseFile(0)
EndIfCode: Select all
If CreateFile(0, "FREE_SPACE")
For a=1 To 1
Repeat
WriteByte(0, 0) ; Write 0 byte to 'FREE_SPACE' file
ForEver ; Until FreeDiskSpace = 0 byte
Next
CloseFile(0)
EndIfTo fill free disk space with zeroes (most of wipe utilities works in that way: WinHex, SDelete: http://www.sysinternals.com/Utilities/SDelete.html, nullfile).Kale wrote:Why do you need to use code like this? Maybe it can be achieved in a nicer way?

Code: Select all
Procedure CleanFile(Filename.s, FileSize.l, ChunkSize.l = 0)
;Note: if Chunk Size is left zero it will just write the file in one pass.
;in chunk cleaning remeber that the size of the file will exccede by 1 chunk.
Handle = CreateFile(#PB_Any, Filename)
If IsFile(Handle) = #False
ProcedureReturn #False
EndIf
If ChunkSize = 0
ChunkLocation = AllocateMemory(FileSize)
ZeroMemory_(ChunkLocation, FileSize)
WriteData(Handle, ChunkLocation, FileSize)
FreeMemory(ChunkLocation)
Else
NumberOfChunks = Round(ChunkSize / FileSize, 1)
ChunkLocation = AllocateMemory(ChunkSize)
ZeroMemory_(ChunkLocation, ChunkSize)
For Chunk = 0 To NumberOfChunks
If
WriteData(Handle, ChunkLocation, ChunkSize)
Next
FreeMemory(ChunkLocation)
EndIf
CloseFile(Handle)
DeleteFile(Filename)
ProcedureReturn #True
EndProcedureCode: Select all
Procedure.q GetDiskSpace(drive$,type$)
Value.q=-1
If GetDiskFreeSpaceEx_(drive$,@free.q,@total.q,0)
Select LCase(type$)
Case "t" : Value=total
Case "f" : Value=free
Case "u" : Value=total-free
Default : Value=-1
EndSelect
EndIf
ProcedureReturn Value
EndProcedure
Procedure CreateFilledFile(Name.s, fsize.q)
Protected file.l = CreateFile(#PB_Any, Name)
If file
If fsize<=0
CloseFile(file)
ProcedureReturn #True
Else
FileSeek(file, fsize-1)
WriteByte(file, 0)
CloseFile(file)
ProcedureReturn #True
EndIf
EndIf
EndProcedure
CreateFilledFile("C:\FREE_SPACE",GetDiskSpace("c:","f")-1)Thanks b1be, I can't believe how fast it is - 5,47 GB is filled for 47 milliseconds. Great!!b1be wrote:this may help
http://www.purebasic.fr/english/viewtop ... 551#152551
but of course, you must determine how much FREE SPACE do you have on the drive, exceeding size with 1 byte will produce ZeroFile (0 Bytes)
quick test
this should fill C:Code: Select all
Procedure.q GetDiskSpace(drive$,type$) Value.q=-1 If GetDiskFreeSpaceEx_(drive$,@free.q,@total.q,0) Select LCase(type$) Case "t" : Value=total Case "f" : Value=free Case "u" : Value=total-free Default : Value=-1 EndSelect EndIf ProcedureReturn Value EndProcedure Procedure CreateFilledFile(Name.s, fsize.q) Protected file.l = CreateFile(#PB_Any, Name) If file If fsize<=0 CloseFile(file) ProcedureReturn #True Else FileSeek(file, fsize-1) WriteByte(file, 0) CloseFile(file) ProcedureReturn #True EndIf EndIf EndProcedure CreateFilledFile("C:\FREE_SPACE",GetDiskSpace("c:","f")-1)

It is really fast, but doesn't fill the disk. I have to return to do it this way:Xombie wrote:Ah. Yeah, I am slow. So the idea was to seek to a spot, right a zero and then assume the rest of the area before is also zero - empty space.
Not sure that will actually accomplish securely wiping a disk drive. I thought Windows had some strange ways of allocating files and handling stuffs.
eJan - You mention SDelete - you know they provide the source code, yeah? You could grab it and study it to see how they handle things. It seems they also have a nice little description of the process on the project page.
Code: Select all
Procedure.q GetDiskSpace(drive.s, type.s)
Value.q=-1
If GetDiskFreeSpaceEx_(drive,@free.q,@total.q,0)
Select LCase(type)
Case "t" : Value=total
Case "f" : Value=free
Case "u" : Value=total-free
Default : Value=-1
EndSelect
EndIf
ProcedureReturn Value
EndProcedure
Procedure FillFreeSpace(drive.s,f_name.s)
Protected file.l=CreateFile(#PB_Any,f_name)
If file
free=GetDiskSpace(drive,"f")
a=0
Repeat
a=a+1
WriteByte(file,0) ; Write 0 byte to 'f_name' file
Until a=free ; free=5.9 Gb, but only 1.9 Gb is written??
CloseFile(file)
DeleteFile(f_name)
EndIf
EndProcedure
FillFreeSpace("C:","FREE_SPACE")Code: Select all
Procedure CreateFilledFile(Name.s, fsize.q)
Protected file.l = CreateFile(#PB_Any, Name)
If file
If fsize<=0
CloseFile(file)
ProcedureReturn #True
Else
FileSeek(file, fsize-1)
WriteByte(file, 0)
CloseFile(file)
ProcedureReturn #True
EndIf
EndIf
EndProcedure
CreateFilledFile("C:\Temp.hex",6)
OpenFile(0,"C:\Temp.hex")
FileSeek(0,2) ;replace 3rd byte with "a" 97-ASCII
WriteByte(0,97)
CloseFile(0)Yes it does fill free disk space to 0 bytes, but I can't use it because it doesn't fill free disk space with 00's. Only working solution is:b1be wrote:then, open with a hex editor "C:\Temp.hex"
the bytes are written to hdd, you can see the 3rd byte is "a" 97-ASCII 61-HEX
i'm using XPSP2 with NTFS, Cant Test on other OS/FileSystem
dont know what are you looking for exactly tho'
P.S.:@Xombie ok, it Writes only 1 byte, but it does the job, i think it's a Windows Feature/Bug
Code: Select all
Procedure.q GetDiskSpace(drive.s, type.s)
Value.q=-1
If GetDiskFreeSpaceEx_(drive,@free.q,@total.q,0)
Select LCase(type)
Case "t" : Value=total
Case "f" : Value=free
Case "u" : Value=total-free
Default : Value=-1
EndSelect
EndIf
ProcedureReturn Value
EndProcedure
Procedure FillFreeSpace(drive.s,f_name.s)
Protected file.l=CreateFile(#PB_Any,f_name)
If file
free=GetDiskSpace(drive,"f")
a=0
Repeat
a=a+1
WriteByte(file,0) ; Write 0 byte to 'f_name' file
Until a=free
CloseFile(file)
DeleteFile(f_name)
EndIf
EndProcedure
FillFreeSpace("C:","FREE_SPACE")Code: Select all
a=0
Repeat
a=a+1
Until a=5902540800 ; cannot count more than 1607573504
MessageRequester("",Str(a))Code: Select all
For a=0 To 5902540800 ; cannot count more than 1607573505
Next
MessageRequester("", Str(a))Purebasic Help wrote:Quad .q 8 Bytes Length -9223372036854775808 to +9223372036854775807
Code: Select all
a.q=590254079997
Repeat
a=a+1
Debug a
Until a=590254080000 What is the Difference Between 0's and 00's ? 0 Dec = 00 Hex ?eJan wrote:Yes it does fill free disk space to 0 bytes, but I can't use it because it doesn't fill free disk space with 00's