I need to clean my call recordings of empty files from time to time. I used to do it manually, but that’s not what computers are for – right? So let’s automate it.
We’re going to use Windows Task Scheduler and a small batch, to be run once a day.
Let’s start with the batch.
First, we’ll be processing all the files in a specified folder – to get a simple list of files, we’ll use
Then, for every file in the list, we’ll be doing some action. We’ll use for loop. By putting the dir command in signle quotes, we tell the loop to run the command and then treat the output of it as a list of files.
for /F %I in ('dir /B') do echo %I
Now, we hit the first problem – %I contains only the first part of each file name – up to a space. It’s because the default delimiter for “for /F” loop includes space. We need to overwrite it. We’ll also take the %I into double quotations
for /F "delims=" "%I" in ('dir /B') do echo "%I"
OK, let’s take care of the action part. We need to check the file size and delete it if it’s zero. Luckily, “for /F” loop can give us the file size of every file processed, with ~z syntax.
for /F "delims=" "%I" in ('dir /B') do if %~zI EQU 0 del "%I"
Unfortunately, directories also report size of 0, so we need to make sure we’re not deleting any directories by the way – let’s differentiate them by the ending with “\”.
for /F "delims=" "%I" in ('dir /B') do if not exist "%I\" if %~zI EQU 0 del "%I"
And finally, we put it into a batch file. We need two things:
- To tell it in what folder to look the files for
- To change “%” to “%%” – that’s the loop’s requirement
Here’s our final version:
for /F "delims=" %%I in ('dir /B') do if not exist "%%I\" if %%~zI EQU 0 del "%%I"