#line 361 "/home/travis/build/felix-lang/felix/src/packages/filetools.fdoc"
  // File renumbering
  
  if System::argc < 4 do
    println "Usage: rentut dir regexp first dst";
    println "For tutorial try:";
    println r"  dir = 'src/web'";
    println r"  re = 'tut_(\d*)\\.fdoc'";
    System::exit(1);
  done
  
  s_dir := System::argv 1;
  s_re := System::argv 2;
  s_first := System::argv 3;
  s_moveto  := System::argv 4;
  
  first := size s_first;
  moveto := size s_moveto;
  re := RE2(s_re);
  if first == moveto do
    println$ "src = dst, not moving anything";
    System::exit 0;
  done
  
  println$ "Renumber files in " + s_dir+ " matching "+"'"+s_re+"'"+" from " + str first + " to " + str moveto;
  
  docs := FileSystem::regfilesin(s_dir, re);
  var files = varray docs;
  
  // direction: if first < moveto, we're moving up, so we have to start at the end and work down.
  // if first > moveto, we're moving down, so we have to start at the start and work up.
  comparator := if first < moveto then \(\gt\) of (string * string) else \(\lt\) of (string * string) endif;
  
  sort comparator of (string * string) files;
  println$ "Files = " + str files;
  var groups : array[StringPiece,2];
  
  iter
    (proc(var f:string){
      println f;
      res := Match(re, StringPiece f,0,ANCHOR_BOTH,C_hack::cast[+StringPiece] (&groups),2);
      if res do
        //println$ "Group 1 = " + str (groups.1);
        n := size (str (groups.1));
        if n >= first do
          m := n + moveto - first;
          s := f"%02d" m.int;
          soffset := groups.1.data - (&f).stl_begin;
          var newf = f;
          replace(&newf,soffset.size,2uz,s);
          res2 := FileSystem::rename_file(
            Filename::join (s_dir,f),
            Filename::join (s_dir,newf)
          );
          if res2 != 0 do
            println$ "Rename " + f + " -> " + newf + " failed";
          else
            println$ f + " -> " + newf;
          done
        else
          // println$ str n + " Unchanged";
        done
      else
        println "NO match";
      done
    })
  files;