Mailing-List: contact cygwin-help AT cygwin DOT com; run by ezmlm List-Subscribe: List-Archive: List-Post: List-Help: , Sender: cygwin-owner AT cygwin DOT com Mail-Followup-To: cygwin AT cygwin DOT com Delivered-To: mailing list cygwin AT cygwin DOT com Date: Sun, 19 Jan 2003 07:54:41 +0100 From: Thomas Baker To: cygwin AT cygwin DOT com Subject: Re: How to automatically process file/dir names? Message-ID: <20030119065441.GA1920@LEPIDUS> Mail-Followup-To: Thomas Baker , cygwin AT cygwin DOT com References: <7705 DOT 1042887770 AT www33 DOT gmx DOT net> <5 DOT 2 DOT 0 DOT 9 DOT 2 DOT 20030118090212 DOT 01d86eb8 AT pop3 DOT cris DOT com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <5.2.0.9.2.20030118090212.01d86eb8@pop3.cris.com> User-Agent: Mutt/1.4i > 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/