Removing `sparse` file attribute
What's the sparse
file attribute on NTFS partitions and how can one remove it?
Using Windows 7, Windows 8 and Windows 8.1.
Nicole Hamilton provided the description of what sparse files are.
Let's learn it with an experiment.
Creating a Sparse File
Start a Command Prompt (cmd.exe
) as Administrator.
-
Creating large file:
fsutil file createnew test.nul 1048576
- Check its size in Explorer:
- Size: 1.00 MB (1,048,576 bytes)
- Size on disk: 1.00 MB (1,048,576 bytes)
-
Let's mark it sparse:
fsutil sparse setflag test.nul
- Nothing changed so far, the both sizes are still 1.00 MB.
-
Let's mark the entire byte range as sparse:
fsutil sparse setrange test.nul 0 1048576
- Now the sizes changed dramatically:
- Size: 1.00 MB (1,048,576 bytes)
- Size on disk: 1.00 KB (1 024 bytes)
-
You can query if the file is sparse or not with
fsutil
:fsutil sparse queryflag test.nul This file is set as sparse
Or
This file is NOT set as sparse
The value of Size on disk depends on the cluster size of the file system. The default cluster size on NTFS is 4 KB. The drive I used for testing was formatted with 1 KB cluster.
I used this article as the source.
Removing Sparse Attribute
The utility I used in the example does not provide a flag to remove sparse attribute.
The simplest way to do is to copy the file. The file will lose this special attribute if the application that copies it does not specially preserve the sparse attribute and sparse range.
You can use Far Manager to clear the sparse attribute. Navigate to the file and press Ctrl + A to open Attribute dialog. Clear Sparse check box. And click Set button.
A sparse file is one that has holes in it. Large sections of binary zeros aren't written to disk. Here's Microsoft's explanation from Sparse Files (Windows):
When sparse file functionality is enabled, the system does not allocate hard disk drive space to a file except in regions where it contains nonzero data. When a write operation is attempted where a large amount of the data in the buffer is zeros, the zeros are not written to the file. Instead, the file system creates an internal list containing the locations of the zeros in the file, and this list is consulted during all read operations. When a read operation is performed in areas of the file where zeros were located, the file system returns the appropriate number of zeros in the buffer allocated for the read operation. In this way, maintenance of the sparse file is transparent to all processes that access it, and is more efficient than compression for this particular scenario.
This sparse attrib problem happens all the time with downloaded ISOs for me.
Put this in a batch file and drag-and-drop the target file onto it (with Windows Explorer). Et voilà.
fsutil sparse queryflag %1
pause
fsutil sparse setflag %1 0
fsutil sparse queryflag %1
pause
This does not work in Windows 7 and 8.x however (possibly Vista too) as setflag
only accepts <filename>
as a valid parameter and there is no corresponding clearflag
command. It does however work in Windows 10.