wc2fn -- wildcards in target filenames
Download
Introduction
The tool wc2fn (wildcards2filenames) is a wrapper for programs and shell commands that expect
pairs of filenames. For historic reason --the wrapper was first written for copy and move commands--
the first filename of the pair is referred to as 'source', the second as 'target'.
The set of source files is selected by matching a wildcard pattern (Unix glob);
a corresponding set of target filenames is generated by wc2fn by expanding the target pattern.
The target pattern is also provided in wildcard format.
Since the target filenames are only generated, files with those names can either exist, or
can be created during the process, or may be just listed.
After the set of source:target filenames is created the specified program or shell
command is executed for each source:target pair.
If this sounds rather abstract have a look at the Examples.
Notation used in documentation and Usage message
- pattern -- a combination of wildcards and 'tame-cards'; the term 'tame-cards' refers to the
static string segments that are common in all matched files, as opposed to the wildcards witch
match different string segments in each individual file.
- source pattern; pat1 -- pattern of the source, or pattern1; to match existing files
- target pattern; pat2 -- pattern of the target, or pattern2; to generate the target
filenames
- source:target pair -- matched source filename, and the corresponding (generated)
target filename
- set of source:target pairs -- the entire set of the above for a given source and
target pattern
- source, match characters -- ' * ? [char-range] [^char-range] '
- target, replace characters -- ' * ? (replace-text) | ' -- but not [] or [^] ;
see How it works for details
Details and assorted notes (needs more work)
Source file match and target name generation are performed by using the python modules glob,
fnmatch and os.path -- the shell has to be prevented from wildcard expansion by quoting source
and target patterns.
The actual work horse that does the match/replace is the included python class
Wildcards2Filenames; python programmers can import the module and use this class to write their
own front-end.
Currently only regular files are processed, directories, symlinks, etc. are ignored.
wc2fn can be used in several ways:
- General form in which a shell command [with options], and source and target pattern are
provided.
- Frequently used shell commands can be wrapped for ease of use; the syntax of the wrapper
resembles that of the original command, e.g.
"cp source-file target-file" becomes "wccp 'source-pattern' 'target-pattern'
- python programmers import the module and write their own command line front-end.
How target filename generation works
- Both source and target patterns are split into wild- and tame-cards.
- The source files are matched using python module glob
- Matched source filenames are split into 'wild' and 'tame' fragments according to source
pattern.
- Replacements in target are done on a positional bases.
- Wildcards in the target pattern are replaced by the fragments of the
source filename that matched the wildcard at the corresponding position.
- Tame-cards in the target pattern replace the tamecards fragments of the
source filename at the corresponding position.
- Therefore, the wild-/tame-card pattern of the target has to match that
of the source. (However, see (_X_)exception)
- For simple substitution the kind of wildcard in the target pattern is irrelevant
and can be either '*' or'?' , but not '[' .
- For special substitution using '|' and '(replace-text)' see (to-be-documented).
Installation
- Python 2.2 or better installed
- Platforms tested:
- Linux (several distros)
- Solaris
- NetBSD
- cygwin (cmd shell on Win89, XP)
- DOS (limited testing only -- see (to-be-documented))
- Download from http://sourceforge.net/projects/wc2fn/
- Decompress wc2fn-version#.tar.gz and make wc2fn executable
- Move wc2fn.py into a directory that is in your executable path
The exact location may depend on your platform, your user privileges, and personal preferences
- To get full functionality add some symbolic links, e.g.:
- "ln -s wc2fn wccp" -- Example for 'wildcard_copy'
- For further examples, or to create your own customization, see dict{} specialCmds in file wc2fn
- To import the module wc2fn.py or its class Wildcards2Filenames
into other python programs make a symbolic link to path/wc2fn and call the link wc2fn.py