#!/bin/tcsh -f #(ie run the cshell on this but don't read the .cshrc) echo version = 1.20 of usfn 2009 May 29 # 2009 May 29, 1.20: %20 becomes _ # 2009 Feb 19, 1.19: debug for Mac OSX # 2009 Feb 19, 1.18: debug for Mac OSX # 2009 Feb 19, 1.17: reverse underscores back to space with ' ' input # 2008 Jun 27, 1.16: remove #! # 2008 Apr 22, 1.15: spaces AND '?' # 2008 Feb 16, 1.14: handle "&" inside a file name! # 2007 mar 16, 1.13: possible way to speed this: use `ls | grep ' '` # 2005 May 22, 1.12: remove ls # 2005 May 14, 1.11: Handle "[]" inside a file name! switch to tcsh # 2005 Apr 15, 1.10: Handle "()" inside a file name! # 2004 Jun 22, 1.09: Handle "-" inside a file name! # 2003 May 17, 1.08: Handle "?" inside a file name! # 2002 Oct 13, 1.07: handle quotes inside a file name! # 2002 Oct 12, 1.06: fix test for existance - could be a directory or link! # 2002 Oct 12, 1.05: debugging # 2002 Oct 12, 1.04: show exact file name when debugging # 2002 Jul 9, 1.03: spaces converted to ^G internally, underscores left alone # 2002 Jul 9, 1.02: handles cases that have both space and underscore! # underscore becomes tilde then spaces become underscore # origin 2001Sep07.15:32:46 if ($#argv > 1) then echo 'usage: usfn [character]' echo 'Un Space File Name: usfn [character]' echo 'Remove spaces from all file names in the current directory.' echo 'Without the given character, the default character is underscore "_".' echo 'Without the character, the default for "(" or ")" is underscore "+".' echo 'Characters converted are: "()+ ?#". Also "%20" is made to underscore.' echo echo 'If the character is a space, " ", then underscores are reverted' echo 'to spaces. Thus one can undo the mess one made!' echo echo 'Purpose: Mac and PC computer operating systems allow spaces' echo 'in names, as do Unix. However, such names interfere with' echo 'file name continuation and other speed tricks.' echo 'This script converts the spaces to other characters.' # echo '' # echo 'The default' # echo '' # 'Method: Spaces are converted to ^G internally' # '(a bell\! Unlikely to be used\!)' echo '' echo 'Dr. Thomas D. Schneider' echo 'National Institutes of Health' echo 'National Cancer Institute' echo 'Center for Cancer Research Nanobiology Program' echo 'Molecular Information Theory Group' echo 'Frederick, Maryland 21702-1201' echo 'toms@ncifcrf.gov' echo 'permanent email: toms@alum.mit.edu' echo 'http://www.ccrnp.ncifcrf.gov/~toms/' exit endif # swap these lines to turn on debugging: set debugging = true set debugging = false set space = " " if ($#argv == 1) then set unspace = "$1" else set unspace = "_" endif echo "space is set to $unspace" if ($#argv == 1) then set unparen = "$unspace" else set unparen = "+" endif echo "parenthesis is set to $unparen" # Reverse direction if space is used!! if ("$unspace" == ' ') then set space = "_" set unparen = " " endif # ls -d List directories like other files, rather than listing their # contents. # The main difficulty is that the foreach list ignores carriage returns, # so spaces in the name cause the individual parts of a name to # look like individual files. # The way around this is to put quote marks around the list ... set showline = 'echo ----------------------------' set showedfirstline = false # echo THERE NOW # ls -d -- * | head # echo THERE # exit # this failed when the file name had a single quote in it ... # set final = "`echo $original | tr ' ' "$unspace"`" # 2006 may 14: # foreach original ("`ls -d -- *`") # In csh, foreach in the above fails inside this script for files # that have '[]' in them. Switching from csh to tcsh works. # The -- means files that start with '-' will be handled ok: foreach original ("`ls -d -- *`") # echo "$original" # make single quotes and spaces into $unspace set step1 = `echo "$original" |tr "'" " " | tr "$space" "$unspace" ` set step2 = `echo "$step1" | sed "s/\%20/_/g" ` set final = `echo "$step2" |tr '()[]&?#' " " | tr ' ' "$unparen" ` if ($debugging == true) then echo "original: ""'"$original"'" echo "final: ""'"$final"'" endif if !("$final" == "$original") then if ($showedfirstline == false) then set showedfirstline = true $showline endif # this did not handle "?" inside the name! # echo "original: ""'"$original"'" # echo "final: ""'"$final"'" echo "original:" "'""$original""'" echo "final: " "'""$final""'" # test that the original file or directory EXISTS (-e): if (-e "$original") then mv "$original" "$final" echo '*MOVED*' $showline else echo "*WARNING* cannot find '"$original"' to change it into $final\!" echo "original: ""'"$original"'" echo "final: ""'"$final"'" $showline endif endif end exit ******************************************************************************** # functinal but buggy: echo ---------------------------- foreach internal (`ls -d * | tr ' ' "$bell"`) set final = "`echo $internal | tr "$bell" "$unspace"`" set original = `echo "$internal" | sed -e "s/$bell/ /g"` if ($debugging == true) then echo "internal: ""'"$internal"'" echo "final: ""'"$final"'" echo "reconstructed original: ""'"$original"'" endif if !("$final" == "$original") then echo "$original" echo "->" echo "$final" # test that the original file or directory EXISTS (-e): if (-e "$original") then mv "$original" "$final" echo '*MOVED*' echo ---------------------------- else echo "*WARNING* cannot find '"$original"' to change it into $final\!" echo "internal: ""'"$internal"'" echo "final: ""'"$final"'" echo "reconstructed original: ""'"$original"'" echo ---------------------------- endif endif end ******************************************************************************** # functional but too simple: foreach f (`ls -d * | tr " " "_"`) echo "$f" end # foreach f (`ls -d $1 | tr " " "_"`) # that form works only if one types unspace filename "*"