Mail Archives: cygwin/2003/01/19/02:09:35
> At 03:02 2003-01-18, svartsjel AT gmx DOT net wrote:
> >Quite a few problems arise from within self-written scripts when dealing
> >with (Windows') file and directory names containing spaces. I therefore
> >thought about a script which takes a directory as argument, recursively
> >processing both file and (sub)dir names in it (starting with the
> >argument's name itself), replacing each space with an underscore
> >(additionally also rendering everything lowercase), and cutting off
> >trailing space(s) from names, respectively. For instance, the processed
> >files fortunes-o_, index_2.htm_ and index2_.html should be detected and
> >fixed like this: fortunes-o, index_2.htm, index2.html.
> >
> >How would you accomplish that?
On Sat, Jan 18, 2003 at 09:23:00AM -0800, Randall R Schulz wrote:
> Most of what you want in terms of the name transformations you want can be
> effected with the "tr" command. Study it.
FWIW, I have been using a script based on the "sed" command.
I got the script from some book or ftp site, unfortunately
not recorded in the header, and it runs on Cygwin with my
AT&T Korn shell -- not sure why, maybe because of the syntax
for declaring functions?
The only problem I have had with this script is that if a
target filename already exists, that file will be overwritten.
Tom
#!/ast/bin/ksh
# Rename files with sed expression.
# Takes a sed command as the first argument (most usefully a substitute)
# and applies it to the remaining arguments (or stdin, if no arguments)
# considered as a list of filenames.
# e.g.
# rename 's/.txt$/.bak/' *
# ls o*.c | rename 's/^o/old\//'
function rename_1 {
typeset gname=`print -R "$1" | sed "$subst"`
if [ "$1" != "$gname" ]
then mv "$1" "$gname"
fi
}
case $# in
0) print >&2 "Usage: $0 'sed command' [file ...]"
exit 2
;;
1) subst="$1"
while read fname; do rename_1 "$fname"; done
;;
*) subst="$1"; shift
for fname; do rename_1 "$fname"; done
;;
esac
--
Dr. Thomas Baker Thomas DOT Baker AT bi DOT fhg DOT de
Institutszentrum Schloss Birlinghoven mobile +49-171-408-5784
Fraunhofer-Gesellschaft work +49-30-8109-9027
53754 Sankt Augustin, Germany fax +49-2241-144-2352
--
Unsubscribe info: http://cygwin.com/ml/#unsubscribe-simple
Bug reporting: http://cygwin.com/bugs.html
Documentation: http://cygwin.com/docs.html
FAQ: http://cygwin.com/faq/
- Raw text -