Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Batch file to delete 0kb files in directory and sub directories 1

Status
Not open for further replies.

Jayz

ISP
Feb 17, 2002
59
0
0
Hi,
Could someone help write a batch file that will look into a particular folder (including sub folders)on my pc and delete any files that are 0kb in size.

much appreciated
 
You can search for files at most 1KB in size and then sort the results by size and delete the 0KB ones. (0 KB search doesn't work!)

 
This will be a fun excersize. I'll start with this
Code:
@echo off
::Set directory of files to be processed
set datadir=C:\Temp

::Create the list of files to be deleted
dir %datadir% /A:-d /O:s /S | find " 0 " > list.txt

::Get the names & delete
for /F "tokens=5" %%F in (list.txt) do (
del "%datadir%\%%F"
)

del list.txt
goto :eof

Obviously, datadir will need to be changed as required. My brief testing found a few problems with this batch file (there may be others).[ol][li]Files are found in the subfolders, but the subfolder name doesn't get saved to the list.txt file, therefore deleting of subfolder items fails[/li][li]Files in list.txt that have spaces in the filename don't get parsed properly, so consequently those also fail to get deleted[/li][li]Not really a problem, but because of the last line of 'list.txt' a final failing delete also occurs[/li][/ol] There are probably other issues that I haven't noticed, but it's a start. Anyone care to expand on this?
 
cable was out last night (GMT+800) so...

Code:
@ECHO OFF
PUSHD %~fs1
FOR /F "usebackq tokens=4-5" %%A IN (`DIR /X/A-D ^| FINDSTR /R "^[0-9]"`) DO IF '%%A'=='0' DEL %%~fB
FOR /F "usebackq tokens=5" %%A IN (`DIR /X/AD ^| FINDSTR /R "^[0-9]" ^| FINDSTR /V /R "\.*\.$"`) DO PUSHD %%A & CALL %~fs0 .
POPD
POPD
 
Very nice JustinEzequiel, - much cleaner than where I would have ended up.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top