5 pts.
 Delete files based on Size
Is there a way to write a simple Batch file that would allow me to delete all files in a folder with a size of 0KB. I have an application that churns out csv files every two minutes, but quite a few are empty, and I want to get rid of them automatically before archiving the rest.

Software/Hardware used:
win 2003 / XP
ASKED: November 10, 2009  10:29 AM
UPDATED: November 21, 2009  8:38 PM

Answer Wiki:
If you are looking for all files equal to 0 then the FOR Command will loop through each file. FOR Command example is shown below. FOR %%F IN (*.*) DO (IF %%~zF LSS 1 DEL %%F) The above example will go through all files in the current directory, and all files found with a length less than 1 in file size will be delete. Hope this helps Jutpro
Last Wiki Answer Submitted:  November 10, 2009  6:37 pm  by  jutpro   480 pts.
All Answer Wiki Contributors:  jutpro   480 pts.
To see all answers submitted to the Answer Wiki: View Answer History.


Discuss This Question:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _


 

Although I agree with Jutpro, you may want to check out some of the techniques listed here: Batch Help

Hope this helps!

-Schmidtw

 11,220 pts.

 

Here is a script that will delete files in a folder that are smaller than the given number of bytes. The size in bytes and folder path are both arguments to the script.

# Script DeleteBySize.txt
var str folder, bytes, filelist, ssize
var int bytes, filesize

# Get the list of files in $folder.
lf -n "*" $folder ($ftype=="f") > $filelist
while ($filelist <> "")
do

    # Get the next file from the list.
    lex "1" filelist > $file

    # Get this file's size.
    lf -s $file > $ssize ; set $filesize = makeint(str($ssize))

    # Is $filesize smaller than $bytes ?
    if ($filesize <= $bytes)
    do
        # Delete this file.
        system del ("""+$file+""")
    done
    endif
done

Script is in biterscripting. Copy and paste the script into file C:/Scripts/DeleteBySize.txt, start biterscrpting, then copy and paste the following command into command area.

script "C:/Scripts/DeleteBySize.txt" folder("C:/testfolder") bytes(0)

Above command will delete all files in folder “C:/testfolder” whose file size is 0.

script "C:/Scripts/DeleteBySize.txt" folder("C:/testfolder") bytes(100)

will delete all files in folder “C:/testfolder” whose file size is less than or equal to 100.

etc.

Test the script first before using. The documentation for the lf (list files) command is at http://www.biterscripting.com/helppages/lf.html .

 70 pts.