Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Recursively delete 0KB files using windows cmd

Tags:

I have some process which creates some files of 0KB size in a directory and its sub-directories.
How can I delete the files from the file system using the windows command prompt?
Any single command or a script that will do the task will work.


I can only run simple cmd commands and scripts, working on a remote machine with restricted access.
like image 771
crodjer Avatar asked Nov 14 '10 09:11

crodjer


People also ask

How do I delete all recursive files in Windows?

To remove a directory and all its contents, including any subdirectories and files, use the rm command with the recursive option, -r . Directories that are removed with the rmdir command cannot be recovered, nor can directories and their contents removed with the rm -r command.

How do I delete a 0kb folder?

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.

How do I delete a zero byte file in Windows?

Open the Wise Force Deleter installer to add the program to Windows. Open the Wise Force Deleter window. Select the Add file option to select the 0-byte file to erase. Press the Unlock & Delete button, and click Yes to confirm.


1 Answers

  1. Iterate recursively over the files:

    for /r %F in (*) 
  2. Find out zero-length files:

    if %~zF==0 
  3. Delete them:

    del "%F" 

Putting it all together:

for /r %F in (*) do if %~zF==0 del "%F" 

If you need this in a batch file, then you need to double the %:

for /r %%F in (*) do if %%~zF==0 del "%%F" 

Note: I was assuming that you meant files of exactly 0 Bytes in length. If with 0 KB you mean anything less than 1000 bytes, then above if needs to read if %~zF LSS 1000 or whatever your threshold is.

like image 117
Joey Avatar answered Sep 21 '22 18:09

Joey