B4J Question Find files filled with zeros

hibrid0

Active Member
Licensed User
Longtime User
Hi, I'm having a little problem and then I want to create a tool to check files if are filled with zeros.
I try with some batch tools, but they just check is the size is 0kb.
My problem is have files with normal size and filled by zeros.

Problem with a 3rd party software backup and I have a very big problem, because have almost 3 tb to check the files filled with zeros and replace with ok files from backups ok.


I have the part to list list files and folders. I try to load file by file on a variable
But how can I check if is filled by zeros on hex?
 

Daestrum

Expert
Licensed User
Longtime User
Done as non ui app
B4X:
Sub Process_Globals
 Dim flist As List
 Dim NotZero As Boolean
End Sub
Sub AppStart (Args() As String)
 flist.Initialize
 flist.AddAll(File.ListFiles("c:/temp"))
 For Each filename As String In flist
 checkforzeros("C:/temp/"&filename) 
 Next
End Sub
Sub checkforzeros(s As String)
 If File.IsDirectory("",s) Then Return ' ignore directories
 Dim in As InputStream
 Dim flen As Int
 in = File.OpenInput("",s)
 Dim buf(in.BytesAvailable) As Byte
 NotZero = False
 flen = in.ReadBytes(buf,0,in.BytesAvailable) ' read the file
 For a = 0 To flen-1  ' check each byte for 0x00 - exit on first non x00 as file is not just 0x00
 If buf(a)<> 0x00 Then 
 NotZero = True
 Exit
 End If
 Next
 If Not(NotZero) Then Log(s&" is zero file and needs replacing")' is zero file you would copy backup to replace it after in.close
  in.close  ' close input stream
' if Not(NotZero) then DoYourBackupCopyHere()
End Sub
'Return true to allow the default exceptions handler to handle the uncaught exception.
Sub Application_Error (Error As Exception, StackTrace As String) As Boolean
 Return True
End Sub


You said you had the logic for getting the filenames so you can just use checkforzeros sub and remove the directory check.
 
Upvote 0
Top