7

Is there a built-in command in Windows 7 or higher to fill a file with zero / NULL bytes?

The processing should happen in-place (i.e. it should modify the actual disk sectors / bytes of the file), and not create a new file.

Something like:

zero c:\temp\*.*

or

zero hello.bin

that would do this:

Before: hello.bin (500 MB)
5D 1A CB FF FF C0 ... AA CD 0F FF


After: hello.bin (500 MB)
00 00 00 00 00 00 ... 00 00 00 00

If there's nothing built-in for this, what other solutions exist? Or would I have to do it in C?

I say Reinstate Monica
  • 25,487
  • 19
  • 95
  • 131
Basj
  • 1,489
  • 7
  • 47
  • 90
  • Can you shed some light on why you want 500 MB of `00 00`, vs. `FF FF` or securely deleting the file? – I say Reinstate Monica Oct 17 '17 at 23:49
  • @TwistyImpersonator When using a secure delete tool, we cannot check that it has been zeroed before deleting with wxHexEditor because the fîle doesn't exist anymore (or we would need to remember the sector on disk and open whole disk with wxHexEditor). If the fîle is just zeroed and not deleted, we can open and view it and check. – Basj Oct 18 '17 at 06:06
  • 1
    I'm not so sure this is truly secure. For example, this method doesn't sound like it will securely delete any of the file's data that's stored in the MFT. – I say Reinstate Monica Oct 18 '17 at 13:03

2 Answers2

9

Is there a built-in command in Windows to fill a file with zero / NULL bytes?

Yes. You can use fsutil for this:

> fsutil file setzerodata /?
Usage : fsutil file setzerodata offset=<val> length=<val> <filename>
   offset : File offset, the start of the range to set to zeroes
   length : Byte length of the zeroed range
   Eg : fsutil file setzerodata offset=100 length=150 C:\Temp\sample.txt

To zero fill a complete file you will need to use an offset of 0 and you will need to know the file length.


Can we use batch file that would compute the size automatically?

Of course.

Use the following batch file (zero.cmd):

@echo off
setlocal enabledelayedexpansion
for %%a in (%1) do (
  fsutil file setzerodata offset=0 length=%%~za %%a
)
endlocal

Usage:

  • You can pass a single filename as an argument: zero test.txt or a wildcard: zero *.txt

Example:

> type test.txt
abc
foo$
foo
bar

> zero test.txt
Zero data is changed

> type test.txt

>

enter image description here


Further Reading

DavidPostill
  • 153,128
  • 77
  • 353
  • 394
  • Thanks. Can we do a batch that would work like this `zero myfile.bin` ie that would compute the size automatically? – Basj Oct 18 '17 at 06:04
  • Thanks @DavidPostill, with the batch it's wonderful, since it accepts wildcards `*.txt`! A small thing (not a big deal if not possible easily, just asking in case it's possible with just one or two more lines in the batch): would it work also recursively for all files in subfolders? Like `zero -r c:\temp\ ` would zero all files in this folder or in its subfolders. – Basj Oct 18 '17 at 15:35
  • 1
    ... And a last one: is `fsutil setzerodata` really 100% safe? i.e. isn't there a risk that it *allocates new sectors* on disk to write the zero bytes, and leave the previous data still somewhere on disk? ;) Is it documented that it replaces by zero bytes **in-place**? – Basj Oct 18 '17 at 15:38
  • @Basj I've no idea whether it is totally safe :) – DavidPostill Oct 18 '17 at 15:39
  • @Basj `zero -r c:\temp` could be done, but would require a more complicated batch file (parsing parameters etc). It's not something I would want to spend the time on to get it right. – DavidPostill Oct 18 '17 at 15:42
  • Thanks for your answer @DavidPostill. It's already perfect as it is now (I just asked this in case subdir recursive could have been added with just a few more characters in the batch, like adding a flag `-r` in the `for` loop, but as it's not possible, no need to spend more time on this, it's good already!) – Basj Oct 18 '17 at 15:51
2

Not exactly a solution, but if you have CygWin installed, you just do (example for a 500000000 bytes file) :

dd if=/dev/zero of=MyFile.zero bs=1 count=500000000

This is the same method you would use at any Unix-like operating system.

Sopalajo de Arrierez
  • 6,603
  • 11
  • 63
  • 97