Question Locked

Help with DOS batch

0 helpful
8 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

I have the below DOS batch that checks the length of every file in the temp folder and display the file name(s) of any file with zero bytes. In addition, if there are no files in the directory the message ?No Files in folder? will be displayed.

I need this batch file to do an additional task;If files are in the folder and all are greater than zero bytes I would like to echo a message, ?No Empty Files found?.
Everything I have tried have not worked. Any help will be greatly appreciated.

@ECHO OFF
CD C:\temp
if exist *.* goto CHKFILELEN
if not exist *.* goto MESSAGE

:CHKFILELEN
for %%f in (*.*) do (if %%~zf lss 1 echo %%f)
goto END

:MESSAGE
echo "No Files in folder"
goto END

:END

8 replies

by tobif

dir /n | find /v " File(s)"| find /c " 0"

This will run the command dir (with standard parameters (if there's a risk that dircmd is set, then you should specify all you need here to get a normal dir command with the /n parameter.

The result from this is sent to the command find /c, which will count the number of lines, containing " 0".
However, if you only have empty files, then the total size of all files would also give a match. This is corrected for by first filtering the result with the additional find in the middle.

Note: For this to work, your system must speak English.
I tested this on the command line of a Vista computer.

by tobif

You can use the same construction (dir - pipe - find) to immediately get a list of all empty files. Just remove the "/c".

by tobif

Where the previous post says " 0", I actually had a whole bunch of spaces, but the html-ish handling of the posts eat up all spaces, but one. (And I'm too lazy to escape it the correct way... Easier to just plug this additional post.)

by seanferd

there should be an errorlevel return when no files meeting the criteria are found. Find out what it is, then you can
if errorlevel = ?, then (some other message).

by Wizard57M-TR

to something like this

:CHKFILELEN
for %%f in (*.*) do (if %%~zf==0 echo %%f else echo "No Zero Byte Files")
goto END

by jutpro

Hi Wizard57,

Thanks for your suggestion, however in the beginning that is the statement I tried and did not have any success.

by jutpro

Thanks to everyone for their suggestions. I was able to figure it out.
I set a variable call "empty" to 0. Within my For statement, if any files are found less than 1 then "empty" is set to 1. If "empty" never gets set to 1 then the echo "No Empty Files found"

This discussion is closed to new replies.