18.11 Renaming Files with renThe CD-ROM contains a command called ren that you can use to rename multiple files. The advantage of ren is that it can be used to rename files in a flexible fashion. For example, I have a set of PostScript files that are named ps.ch01 , ps.ch02 , and so on. I need these files to follow the usual convention of having the ps extension as a suffix, not a prefix - i.e. ch01.ps , ch02.ps , ch03.ps , etc. I could do this with a simple shell script, but it's much easier to just use ren .
% Use the % If ren completes execution silently, everything worked just fine and the files were renamed. Check by listing the directory again: % ren doesn't let you overwrite existing files without warning. Suppose we had another file in the same directory called ch07.ps : % Now when we try renaming the files, ren warns you about overwriting the ch07.ps file: % This feature can be suppressed with the -d option, which says to overwrite files without prompting. Related options are -k , which says not to overwrite any files, also without prompting; and -a , which says to abort the entire procedure if any files will be overwritten. Using -a , ren aborts before any files are renamed, so you can start all over again. ren is also smart enough to detect internal naming conflicts before it actually renames any files. For example, suppose we had both files with both ps. and eps. prefixes that we wanted renamed with .ps suffixes. If there were any conflicts, ren would tell us right away, and none of the files would be renamed: % ren has the restriction that it can only be used to move files within a single directory. Although this makes it inconvenient for some applications, it also makes it more secure. To show ren in a more complicated situation, let's take another example. Every week I write a report and then store it in a directory under the name month .day .year . After a while, I realized that because of the default sorting used by ls , the files weren't being listed in chronological order. % What I needed to do was to rename them year .month .day , and use leading 0s for the first nine months. This can be quickly done with two ren commands: % The first command renames any reports for single-digit months (0-9). In the second command, I'm careful not to match any of the names of files I've already moved. - |
|