Q:

How to count how many files exist in a directory with file size > 0?

I would like to count how many *.diff files I have in a directory with a file size greater than zero.
If there is more then one file, I would need to change the ERRORLEVEL.
What is the batch command in order to do so?

A:

@ECHO OFF
SETLOCAL
SETLOCAL ENABLEDELAYEDEXPANSION
SET "sourcedir=U:\sourcedir"
:: kill any .diff files
DEL %sourcedir%\*.diff >NUL 2>NUL
:: call the detect routine
CALL :detect
ECHO run 1: %countfg0% non-zero .diff files found; errorlevel=%ERRORLEVEL%
:: 1 zero-length file
COPY NUL "%sourcedir%\1.diff" >NUL 2>NUL
CALL :detect
ECHO run 2: %countfg0% non-zero .diff files found; errorlevel=%ERRORLEVEL%
:: 2 zero-length file
COPY NUL "%sourcedir%\2.diff" >NUL 2>NUL
CALL :detect
ECHO run 3: %countfg0% non-zero .diff files found; errorlevel=%ERRORLEVEL%
:: 2 zero-length file + 1 non-zlf
ECHO.>"%sourcedir%\3.diff"
CALL :detect
ECHO run 4: %countfg0% non-zero .diff files found; errorlevel=%ERRORLEVEL%
:: 2 zero-length file + 2 non-zlf
ECHO.>"%sourcedir%\4.diff"
CALL :detect
ECHO run 5: %countfg0% non-zero .diff files found; errorlevel=%ERRORLEVEL%
:: 2 zero-length file + 3 non-zlf
ECHO.>"%sourcedir%\5.diff"
CALL :detect
ECHO run 6: %countfg0% non-zero .diff files found; errorlevel=%ERRORLEVEL%
:: 0 zero-length file + 3 non-zlf
DEL %sourcedir%\1.diff >NUL 2>NUL
DEL %sourcedir%\2.diff >NUL 2>NUL
CALL :detect
ECHO run 7: %countfg0% non-zero .diff files found; errorlevel=%ERRORLEVEL%
:: 0 zero-length file + 2 non-zlf
DEL %sourcedir%\4.diff >NUL 2>NUL
CALL :detect
ECHO run 8: %countfg0% non-zero .diff files found; errorlevel=%ERRORLEVEL%
:: 0 zero-length file + 1 non-zlf
DEL %sourcedir%\3.diff >NUL 2>NUL
CALL :detect
ECHO run 9: %countfg0% non-zero .diff files found; errorlevel=%ERRORLEVEL%

GOTO :EOF

:detect
:: set count-of-(files greater than 0)
SET /a countfg0=0
FOR %%a IN ("%sourcedir%\*.diff") DO IF %%~za neq 0 SET /a countfg0+=1
IF %countfg0%==0 EXIT /b 0
IF %countfg0%==1 EXIT /b 1
EXIT /b 2

Here's a test/detect routine creating 0-length and non-zero-length .diff files in a test directory. You would need to change the setting of sourcedir to suit your circumstances.
The :detect routine actually does the work and would need to be called using the current structure. The rest is simply establishing different test conditions for er, testing.

