Help with DOS batch

By jutpro ·
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.

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

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

echo "No Files in folder"
goto END


This conversation is currently closed to new comments.

Thread display: Collapse - | Expand +

All Answers

Collapse -

Would this help?

by TobiF In reply to Help with DOS batch

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.

Collapse -

By the way...

by TobiF In reply to Would this help?

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

Collapse -

Additonal note

by TobiF In reply to Would this help?

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.)

Collapse -

if errorlevel

by seanferd In reply to Help with DOS batch

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

Collapse -

Might try changing one line in :CHKFILELEN

by wizard57m_cnet Moderator In reply to Help with DOS batch

to something like this

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

Collapse -

Did not work

by jutpro In reply to Might try changing one li ...

Hi Wizard57,

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

Collapse -

I figured it out

by jutpro In reply to Help with DOS batch

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"

Related Discussions

Related Forums