Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: help with 'rename' function



Timothy J Luoma wrote:
>You would do
>
>	rename *.THIS THIS=THAT

function rename {
	emulate zsh
	setopt localoptions
	integer st=0
	local pat old new f
	if (( $# < 2 )) || { pat=$argv[$#]; [[ $pat != *\=* ]]; }; then
		echo >&2 "Usage: rename <files...> <foo>=<bar>"
		return 2
	fi
	old=${pat%%\=*}
	new=${pat#*\=}
	for f in $argv[1,-2]; do
		if [[ $f != *$old ]]; then
			st=1
			echo >&2 "$f: does not end with suffix \`$old'"
		elif ! mv $f ${f%$old}$new; then
			st=1
		fi
	done
	return $st
}

If you use the files module, then this function does not use any external
commands.

-zefram



Messages sorted by: Reverse Date, Date, Thread, Author