On Wed, Dec 8, 2010 at 1:34 PM, john skaller <skal...@users.sourceforge.net> wrote: > > Another approach is probably better: directories simply don't exist. > All files live in the root, they just have structured names. > This interpretation is the most consistent and easiest to work with. > > You'd so something like: > > copy fred/(.*\.flx) joe/\1 > > which clearly descends into subdirectories of the src, or if that isn't > wanted: > > copy fred/([^/]*\.flx) joe/\1 > > because here the regex can't match a path name with a / in it. > > It's a bit messy because "." means "any character" and so you have to > use "\." when you mean the "." character. > > I plan to implement this :)
Conceptually this is pretty simple, though may be pretty inefficient to do it on top of python's regular expression library. The naive approach is to just do a full treewalk and to a regex substitution. To eliminate the treewalk you'd have to parse the regex to see which files match the partial directory. For instance, if you wrote (in python's regex extension): copy_regex(r'(?:(foo/.*/bar)|(bar/.*\.baz))', r'dst/\1') You could optimize it to just do a treewalk down foo/, and just match everything in bar/, but I'm not sure how to support that. zsh's zcp, however, may have the semantics you want. It lets you write the example like: zcp 'foo/(**)/bar' 'dst/$1' zcp 'bar/(*).baz' 'dst/$1' The nice thing is that their pattern matching is much simpler than a regular expression, and I don't think it'd be that hard to modify fnmatch to capture the subexpressions and return them. Here's it's docs from the zsh man page. zmv [ -finqQsvwW ] [ -C | -L | -M | -p program ] [ -o optstring ] srcpat dest Move (usually, rename) files matching the pattern srcpat to corresponding files hav- ing names of the form given by dest, where srcpat contains parentheses surrounding patterns which will be replaced in turn by $1, $2, ... in dest. For example, zmv ’(*).lis’ ’$1.txt’ renames ‘foo.lis’ to ‘foo.txt’, ‘my.old.stuff.lis’ to ‘my.old.stuff.txt’, and so on. The pattern is always treated as an EXTENDED_GLOB pattern. Any file whose name is not changed by the substitution is simply ignored. Any error (a substitution resulted in an empty string, two substitutions gave the same result, the destination was an existing regular file and -f was not given) causes the entire function to abort without doing anything. Options: -f Force overwriting of destination files. Not currently passed down to the mv/cp/ln command due to vagaries of implementations (but you can use -o-f to do that). -i Interactive: show each line to be executed and ask the user whether to exe- cute it. ‘Y’ or ‘y’ will execute it, anything else will skip it. Note that you just need to type one character. -n No execution: print what would happen, but don’t do it. -q Turn bare glob qualifiers off: now assumed by default, so this has no effect. -Q Force bare glob qualifiers on. Don’t turn this on unless you are actually using glob qualifiers in a pattern. -s Symbolic, passed down to ln; only works with -L. -v Verbose: print each command as it’s being executed. -w Pick out wildcard parts of the pattern, as described above, and implicitly add parentheses for referring to them. -W Just like -w, with the addition of turning wildcards in the replacement pat- tern into sequential ${1} .. ${N} references. -C -L -M Force cp, ln or mv, respectively, regardless of the name of the function. -p program Call program instead of cp, ln or mv. Whatever it does, it should at least understand the form ‘program -- oldname newname’ where oldname and newname are filenames generated by zmv. -o optstring The optstring is split into words and passed down verbatim to the cp, ln or mv command called to perform the work. It should probably begin with a ‘-’. Further examples: zmv -v ’(* *)’ ’${1// /_}’ For any file in the current directory with at least one space in the name, replace every space by an underscore and display the commands executed. For more complete examples and other implementation details, see the zmv source file, usually located in one of the directories named in your fpath, or in Func- tions/Misc/zmv in the zsh distribution. ------------------------------------------------------------------------------ _______________________________________________ Felix-language mailing list Felix-language@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/felix-language