Reload files in Paraview

Here is a macro to reload files in Paraview. To use it go to Macro, Add new macro....

import re
import os

files = None
s = GetActiveSource()
    files = s.FileName
except AttributeError:
    print "Not a file source"

if files and len(files) == 1:
    # one time step
    s.FileName = [files[0]]
elif files:
    # multiple time steps
    reg = re.compile(r"(.+[^0-9])([0-9]+)\.(.+)")
    roots = set()
    exts = set()
    for f in files:
       m = reg.match(f)
       if m:
           roots = None
    if roots and len(roots) == 1 and len(exts) == 1:
        root = roots.pop()
        ext = exts.pop()
        basedir = os.path.dirname(root)
        basename = os.path.basename(root)
        unsorted_l = []
        for p in os.listdir(basedir):
            m = reg.match(p)
            if m and basename ==
                unsorted_l.append((int(, os.path.join(basedir, p)))
        s.FileName = [p for i, p in sorted(unsorted_l)]