This is the mail archive of the cygwin@cygwin.com mailing list for the Cygwin project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: How to automatically process file/dir names?


> At 03:02 2003-01-18, svartsjel@gmx.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.Baker@bi.fhg.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/


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]