- file rename ?-force? ?--? source target
- file rename ?-force? ?--? source ?source ...? targetDir
jmn 2008-07-11 On windows, check your version of Tcl is not subject to bug 2015723 [1] regarding file rename -forceI'm only annotating certain wiki pages with a pointer to this bug because it is a silent intermittent failure that may go undetected.
LV So, what kind of code would one need to take a directory of file names and rename them using regular expressions? For instance, say I wanted to remove spaces from files in a directory.HJG Here is a piece of working code to replace spaces with underscores in filenames:
bll 2015-2-18 Windows has filenames that preserve case, but are not distinguished by case. So both 'type xyzzy' and 'type XYZZY' will work no matter what case 'xyzzy' has. If you wish to change the filename's case using file rename, you must use "file rename -force", as file rename's check to see if the file exists will be true, as both 'xyzzy' and 'XYZZY' point to the same file.
LV So, what kind of code would one need to take a directory of file names and rename them using regular expressions? For instance, say I wanted to remove spaces from files in a directory.HJG Here is a piece of working code to replace spaces with underscores in filenames:
set files [glob {*.*}] foreach fname1 $files { if { [string first " " $fname1] >= 0 } { set fname2 [regsub -all " " $fname1 "_"] #puts "rename '$fname1' to '$fname2'" file rename -force -- $fname1 $fname2 ;# ! overwrites existing files ! } }However, to do the above generically, so that a general old and new pattern could be passed and observed, that would be more difficult I suppose. If this code could be generically written, it would make a nice addition to fileutil.Another useful application would be Rename and redate photo-files, i.e. from a digital camera, according to date and time.
bll 2015-2-18 Windows has filenames that preserve case, but are not distinguished by case. So both 'type xyzzy' and 'type XYZZY' will work no matter what case 'xyzzy' has. If you wish to change the filename's case using file rename, you must use "file rename -force", as file rename's check to see if the file exists will be true, as both 'xyzzy' and 'XYZZY' point to the same file.