'''file rename''' ''?'''''-force'''''? ?- -? source target'' '''file rename''' ''?'''''-force'''''? ?- -? source ?source ...? targetDir'' ---- [2008-07-11] On windows, be aware of bug 2015723 ''regarding file rename -force'' I'm only annotating certain wiki pages with a pointer to this bug because it is a silent intermittent failure that may go undetected. ---- The first form takes the file or directory specified by pathname ''source'' and renames it to ''target'', moving the file if the pathname ''target'' specifies a name in a different directory. If ''target'' is an existing directory, then the second form is used. The second form moves each ''source'' file or directory into the directory ''targetDir''. Existing files will not be overwritten unless the '''-force''' option is specified. When operating inside a single filesystem, Tcl will rename symbolic links rather than the things that they point to. Trying to overwrite a non-empty directory, overwrite a directory with a file, or a file with a directory will all result in errors. Arguments are processed in the order specified, halting at the first error, if any. A '''- -''' marks the end of switches; the argument following the '''- -''' will be treated as a source even if it starts with a '''-'''. ---- [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. ---- See also: * [file] * [file copy] * [file delete] ---- [Tcl syntax help] - [Category Command] - [Category File]