'Refile one or more messages -- may raise os.error.
\'tofolder\' is an open folder object.'

def refilemessages(self, list, tofolder, keepsequences=0):

    errors = []
    refiled = {}
    for n in list:
        ton = (tofolder.getlast() + 1)
        path = self.getmessagefilename(n)
        topath = tofolder.getmessagefilename(ton)
        try:
            os.rename(path, topath)
        except os.error:
            try:
                shutil.copy2(path, topath)
                os.unlink(path)
            except (IOError, os.error) as msg:
                errors.append(msg)
                try:
                    os.unlink(topath)
                except os.error:
                    pass
                continue
        tofolder.setlast(ton)
        refiled[n] = ton
    if refiled:
        if keepsequences:
            tofolder._copysequences(self, refiled.items())
        self.removefromallsequences(refiled.keys())
    if errors:
        if (len(errors) == 1):
            raise os.error, errors[0]
        else:
            raise os.error, ('multiple    errors:', errors)
