Some applications create empty 0-byte files in their data folders and fail to clear them up. Over time, if you see many zero-byte files in a folder hierarchy, here are some methods to find all those 0-byte files and delete them.
Find and/or delete empty (0 byte) files in Windows:
Find and delete 0-byte files recursively in a folder tree
It’s important to note that deleting 0-byte files arbitrarily can be problematic sometimes, as some applications may need them as a placeholder or for some other reason. If you’re sure that you don’t need any 0-byte files in a folder path and want to delete them all, follow one of the methods below.
Let’s start with a neat 3rd party freeware GUI tool, and then cover the native methods next.
1. Using the “Find Empty Files-n-Folders” utility
Find Empty Files-n-Folders is an excellent tool that can find and delete empty files (0-byte) and empty folders recursively under a folder tree.
Download Find Empty Files-n-Folders (600KB installer) from Ashisoft.com.
Select the folder and click Scan Now.
The tool will list empty files and folders in separate tabs.
From the Empty Files tab, click Mark all Files and then click Delete Files.
Similarly, to delete the 0-byte files in the selected folder tree, click on the Empty Files tab.
Ashisoft.com has other awesome tools that you can check out!
2. Using Windows Search
Windows Search allows you to list all 0-byte files using the size:
query operator.
Open the folder where you want to find or delete empty files.
In the search box, type size:empty
or size:0 KB
To filter the results by a file extension (e.g., javascript files → extension .js
), use the following Advance Query Syntax (AQS):
size:empty AND ext:js
3. Using Command Prompt
To list all 0-byte (0 KB) files is a folder and sub-folders recursively and output the names to a file, use the following command.
Note that you’ll need to run the command from the folder where you want to find or delete empty (0 KB) files.
for /r %F in (*) do @if %~zF==0 echo "%F" >>d:\0byte-files.txt
Alternately, you can include the target folder path in the for
command so that you don’t have to change the directory in the console window. Example:
for /r "d:\websites" %F in (*) do @if %~zF==0 echo "%F" >>d:\0byte-files.txt
That way, you don’t have to switch over to that particular folder in Command Prompt
The complete list of 0-byte files output is written to the file named 0byte-files.txt
on the D:\
drive.
To delete the files, you’d use the del
command instead of echo
.
for /r %F in (*.*) do @if %~zF==0 del "%F"
or mention the target folder path in the command itself:
for /r "d:\websites" %F in (*.*) do @if %~zF==0 del "%F"
Find and delete 0-byte files having a specific file extension
In the above examples, you can even filter by file extension. For instance, to delete 0-byte .txt
files, you’d use *.txt
instead of *.*
or *
for /r %F in (*.txt) do @if %~zF==0 del "%F"
or with mentioning the folder path:
for /r "d:\websites" %F in (*.txt) do @if %~zF==0 del "%F"
That would delete all the empty .txt
files from the current folder and sub-folders, or in the specified folder tree recursively.
Create a Batch file
If you’d like to make a batch file to find and list empty files and output the results to a text file, here is one:
@echo off set out="d:\0byte-files.txt" for /r "%~1." %%A in (*.*) do if %%~zA == 0 echo "%%~fA" >> %out%
Save the above contents as find-empty-files.bat
.
To delete empty files rather than outputting the list of files, use this batch file:
@echo off for /r "%~1." %%A in (*.*) do if %%~zA == 0 del "%%~fA"
To run the batch file against a folder recursively, you’d use the following syntax:
d:\scripts\find-empty-files.bat d:\websites
What does the above command do?
for /r %F in (*)
iterates files recursively in the mentioned folder and subfolders.if %~zF==0
checks if the iterated file is a 0-byte filedel %%~fA
delete the 0-byte file
4. Using PowerShell
Start PowerShell.exe and use one of the following methods:
List empty (0 KB) files
To get the list of 0-byte files under a folder tree, use this command-line syntax:
Get-ChildItem -Path "D:\websites\test" -Recurse -Force | Where-Object { $_.PSIsContainer -eq $false -and $_.Length -eq 0 } | Select -ExpandProperty FullName
To output the list to a file:
Get-ChildItem -Path "D:\websites" -Recurse -Force | Where-Object { $_.PSIsContainer -eq $false -and $_.Length -eq 0 } | Select -ExpandProperty FullName | Set-Content -Path d:\found.txt
To output the list to grid view:
Get-ChildItem -Path "D:\websites" -Recurse -Force | Where-Object { $_.PSIsContainer -eq $false -and $_.Length -eq 0 } | out-gridview
To list only a specific file type (e.g., .bmp
) :
Get-ChildItem -Path "D:\websites" -include *.bmp -Recurse -Force | Where-Object { $_.PSIsContainer -eq $false -and $_.Length -eq 0 } | out-gridview
Delete empty (0 KB) files
To delete all the 0-byte files under a folder tree, use this command-line syntax:
Get-ChildItem -Path "D:\websites" -Recurse -Force | Where-Object { $_.PSIsContainer -eq $false -and $_.Length -eq 0 } | remove-item
To delete 0-byte files having a specific extension (e.g., .bmp
)
Get-ChildItem -Path "D:\websites" -include *.bmp -Recurse -Force | Where-Object { $_.PSIsContainer -eq $false -and $_.Length -eq 0 } | remove-item
5. Using VBScript
The following VBScript clears empty (0-byte) files in a folder tree recursively.
Copy the following code to Notepad and save it as del-zero-byte-files.vbs
Option Explicit If (WScript.Arguments.Count <> 1) Then WScript.Echo("Usage: cscript DeleteEmptyFolders.vbs {path}") WScript.Quit(1) End If Dim strPath : strPath = WScript.Arguments(0) Dim fso : Set fso = CreateObject("Scripting.FileSystemObject") Dim objFolder : Set objFolder = fso.GetFolder(strPath) Dim sDelList, sDelErr, sFilePath Dim iCnt iCnt = 0 DeleteZeroByteFiles objFolder Sub DeleteZeroByteFiles(folder) Dim subfolder, file On Error Resume Next 'Skip errors when accessing Junctions, etc. For Each subfolder In folder.SubFolders DeleteZeroByteFiles subfolder Next On Error Goto 0 For Each file In folder.files If file.size = 0 Then sFilePath = file.Path On Error Resume Next fso.DeleteFile file, True If Err.number <> 0 Then sDelErr = sDelErr & Err.number & ": " & Err.description & _ vbCrLf & sFilePath & vbCrLf & vbCrLf Else sDelList = sDelList & vbCrLf & sFilePath iCnt = iCnt + 1 End If On Error Goto 0 End If Next End Sub If sDelList = "" And sDelErr = "" Then WScript.Echo "No Empty files found under the " & _ """" & strPath & """" & " tree" WScript.Quit End If If sDelList <> "" then sDelList = "List of empty files deleted" & vbCrLf _ & String(38,"-") & vbCrLf & sDelList & vbCrLf & _ vbCrLf & "Total: " & iCnt & " files deleted." If sDelErr <> "" then sDelErr = "These files could not be deleted" & _ vbCrLf & String(45,"-") & vbCrLf & sDelErr WScript.Echo sDelList & vbCrLf & vbCrLf & sDelErr
Usage
To run the script against a folder, you can use wscript.exe or cscript.exe, like below:
cscript d:\scripts\del-zero-byte-files.vbs "d:\travel documents" wscript d:\scripts\del-zero-byte-files.vbs "d:\travel documents"
CScript.exe shows the outputs to the console window. That means you’ll need to run it from a Command Prompt window to see the output.
WScript.exe shows the outputs in the GUI.
via the Send To menu
You can create a shortcut to the script in your SendTo folder and name it as Delete 0-byte Files. Prefix wscript.exe
in the shortcut properties target field.
Then, right-click on a folder where you want to delete empty files in the folder tree recursively → click Send To → click Delete 0-byte Files in the Send To menu.
You’ll see the list of empty files deleted and the total, and files which couldn’t be deleted with the respective error codes displayed.
6. Using DelEmpty.exe
DelEmpty.exe is a console tool from IntelliAdmin that can delete empty directories recursively. This program can also swiftly delete the empty 0-byte files recursively.
The following is the command-line syntax for the program:
DelEmpty.exe OPTIONS [PATH]
Argument | Description |
-f | Delete empty (0-byte) files |
-d | Delete empty directories |
-v | Verbose mode |
-c | Confirm mode (Shows what was deleted) |
-s | Include sub-directories (traverse subfolders) |
-l | List what would be deleted (will not delete) |
-y | Delete without (y/n) prompt |
Example 1: To list the empty files under a directory and its subdirectories, I used the following command-line syntax:
DelEmpty.exe "New Folder" -f -c -s -y -l
The above command shows the list of empty folders, but will not delete them since the -l
(list only) switch is used.
For folder names containing space(s) — e.g., Mozilla Firefox
, be sure to include the double-quotes around the path.
Example 2: To delete the empty files in a folder and subfolders, I ran the same command-line but without the -l
switch:
DelEmpty.exe "New Folder" -f -c -s -y
Do you know any other utility that can traverse sub-folders and delete empty files? Let’s know your comments.
One small request: If you liked this post, please share this?
One "tiny" share from you would seriously help a lot with the growth of this blog. Some great suggestions:- Pin it!
- Share it to your favorite blog + Facebook, Reddit
- Tweet it!
Too complex
None of the methods on this page ” work ” because said zero byte file is never seen by them. So again ALL methods described here are fully ” useless.”
The “Find Empty Files n Folders” application crashes after it finds the 0kb files, so unfortunately is unuseable
Administrator powershell Fails with error:
+ … ct { $_.PSIsContainer -eq $false -and $_.Length -eq 0 } | remove-item
+ ~~~~~~~~~~~
+ CategoryInfo : PermissionDenied: (D:\cjlofs:FileInfo) [Remove-Item], IOException
+ FullyQualifiedErrorId : RemoveFileSystemItemUnAuthorizedAccess,Microsoft.PowerShell.Commands.RemoveItemCommand
remove-item : Das Element D:\cjlojz kann nicht entfernt werden: Sie besitzen keine ausreichenden
Zugriffsberechtigungen zum Ausführen dieses Vorgangs.
In Zeile:1 Zeichen:113
+ … ct { $_.PSIsContainer -eq $false -and $_.Length -eq 0 } | remove-item
+ ~~~~~~~~~~~
+ CategoryInfo : PermissionDenied: (D:\cjlojz:FileInfo) [Remove-Item], IOException
+ FullyQualifiedErrorId : RemoveFileSystemItemUnAuthorizedAccess,Microsoft.PowerShell.Commands.RemoveItemCommand
remove-item : Das Element D:\cjlosr kann nicht entfernt werden: Sie besitzen keine ausreichenden
Zugriffsberechtigungen zum Ausführen dieses Vorgangs.
In Zeile:1 Zeichen:113
+ … ct { $_.PSIsContainer -eq $false -and $_.Length -eq 0 } | remove-item
+ ~~~~~~~~~~~
+ CategoryInfo : PermissionDenied: (D:\cjlosr:FileInfo) [Remove-Item], IOException
+ FullyQualifiedErrorId : RemoveFileSystemItemUnAuthorizedAccess,Microsoft.PowerShell.Commands.RemoveItemCommand
Please check the folder permissions for D:\cjlosr.