Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

How to retrieve zero byte files and copy into a folder using batch script. can someone help me out.

i want the script should check the subfolders and displays the output as complete folder path in the output text file.

Operating system is WindowsXP. i would like to get the Zero byte files path and copy the path into a text file.

share|improve this question

migrated from May 14 '10 at 13:01

This question came from our site for professional and enthusiast programmers.

Create a batch file with the following commands:

@echo off
pushd %1
if exist *.txt for %%i in (*.txt) do if %%~zi==0 ECHO "%%i" >> list.txt

This should find all the *.txt files of zero length and write the results to list.txt. Run the batch file from the directory where the files are located.

The following command would go through all the subfolders and the resulting txt file will contain full paths. @afrazier thanks for the correction.

for /r %i in (*.*) do if %~zi==0 ECHO "%i" >> list.txt
share|improve this answer
There's no need for the if exist check, just run the for loop directly. It'll loop over every file in the (set) or do nothing if there's no files to operate on. Also of note, doing for /r %i instead of for %i will go through all subfolders and the resulting list.txt will contain full paths. – afrazier May 14 '10 at 14:01
@afrazier thanks I have updated the answer. – Shaji May 14 '10 at 14:23
Thank you! Helped me figure out how to delete empty files. – Alek Davis Oct 19 '11 at 17:21

If you're on a *nix system, you can use find:

find / -size 0 -type f

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.