GMail hack: How to identify messages without an attachment

I send email to my own GMail account all the time, both to back up files, and as a to-do list of things I need to take care of later. As a general rule, if the message has an attachment, I'm probably trying to back up a file. If there is no attachment, the message should go to my to do list for later action.

I wanted to set up a filter to be able to distinguish between the two types of messages. The filter should apply a label based on whether or not the message has an attachment. For messages with an attachment, the filter should apply a "backup" label, and for those without an attachment, it should apply a "to do" label.

It turns out that GMail search cannot find just messages that do not have an attachment. It does a good job at finding messages with attachments, but otherwise returns a mixed set of messages with and without attachments. For example, negating the "has" operator with "-has:attachment", does not return the expected result.

The solution is to apply a label to messages having an attachment, then search and filter with the "-label" operator. Here's how:

STEP 1: LABEL MESSAGES THAT HAVE ATTACHMENTS

Search for messages having an attachment by using the "has:attachment" operator, then apply the "backup" label to the result set.
You can also create a filter for messages having an attachment, then apply the "backup" label in the next step.

STEP 2: SEARCH FOR MESSAGES WITHOUT THE LABEL

Now you can search for messages without attachments by using the "-label:hasAttachment" operator. GMail will return only the messages that do not have an attachment.

MAKING IT AUTOMATIC.

You can set up a filter to apply the the "backup" label automatically. Enter (from:me to:me has:attachment) as the filter criteria in the "Has the words" field. Then in the next step, apply the "backup" label.

All incoming messages having an attachment will receive the "backup" label, and you can now search your messages for your backed up files with "(from:me to:me label:backup)".

However, this only solves part of the problem. What if you want a filter to apply your "to do" label to messages that do not have attachments? In that case, you set up a second filter to with "(from:me to:me -label:backup)". Negating the label operator will give you only the messages without attachments.

Notice that it's important that the previous filter run first, so that the "backup" label is applied. Otherwise the "-label:backup" operator will not work.