Back to Script Page  |  Main
AppleScript Logo

Grep Rename

Tutorial: How to revert an erroneous renaming action
Description: The Grep Rename script provides a very powerful means to rename any number of files.
On the downside, only one character missing from your search or replacement string
might be sufficient to completely screw up hundreds of files!

If you haven't disabled the log feature (and haven't further renamed any of the files in
question) its possible to revert any erroneous renaming action by help of the information
stored in the log.

The log keeps the following information about each renaming action:
  • date and time
  • the search and replace pattern used
  • the number of files that were processed, renamed or that
    caused an error
  • plus, for each file that got renamed (or that caused an error)
    the log file will report:
    • a description of the error that occurred (if any)
    • the old and new file names as well as their file paths

Example:

Sonntag, 09. Dezember 2001 15:37:39 Uhr
---------------------------------------
Search pattern:     File(\d+)
Replace pattern:    TestFile_\1

◊ Err:  
  Old:  File1       Macintosh HD:Desktop Folder:File1
  New:  TestFile_1  Macintosh HD:Desktop Folder:TestFile_1
◊ Err:  
  Old:  File2       Macintosh HD:Desktop Folder:File2
  New:  TestFile_2  Macintosh HD:Desktop Folder:TestFile_2
◊ Err:  
  Old:  File3       Macintosh HD:Desktop Folder:File3
  New:  TestFile_3  Macintosh HD:Desktop Folder:TestFile_3
• Err:  File wasn't renamed but labeled with label index #6 since new file name already exists!
  Old:  File4       Macintosh HD:Desktop Folder:File4
  New:  TestFile_4  Macintosh HD:Desktop Folder:TestFile_4

∑ Files processed:  20
∑ Files renamed:    3
∑ Errors:           1

(Note: depending on your system language the date format might vary)


Now, to undo a particular renaming action, we simply need to extract the relevant old and new
paths, select them and execute the Rename Files of Path List script -- first upon the new paths,
then one more time on the old paths. Voila, that's it!

If you've got BBEdit, you can perform the steps below in order to achieve this:
 
  1. Download and decompress the script Rename Files of Path List and put it into your
    BBEdit "Scripts" folder (which resides inside your "BBEdit Support" folder)
     
  2. Open the log file in BBEdit (the log file resides within your system prefs folder)
     
  3. For all the files you'd like to undo, select their corresponding Err: / Old: / New: lines
     
  4. With the log file being in the front, choose File:New:Text Document (with selection)
    twice in order to copy your selection into two new windows
     
  5. Select the first new window and choose Search:Find.... Then, with grep enabled,
    perform a Replace All:
        Search for: ^.*Err:.*\r +Old:[^\t]+\t(.+)\r +New:.*
        Replace with: \1
    This will extract all old paths (each one on its own line)
     
  6. Select the second new window and choose again Search:Find.... Still with grep enabled,
    do another Replace All:
        Search for: ^.*Err:.*\r +Old:.*\r +New:[^\t]+\t(.+)
        Replace with: \1
    This will extract all new paths (again, each one on its own line)
     
  7. Still with the second new window in the front, choose Edit:Select All, then choose the
    Rename Files of Path List script from your BBEdit Script menu. This will cause the script to
    remember which files to rename.
     
  8. Now, bring the first new window to the front and choose again Edit:Select All.
    Once again, choose the Rename Files of Path List script from your BBEdit Script menu.
    This will restore all your old file names!

 


Contact: Matthias Steffens  |   Back to Script Page  |  Main  |  Last Updated: 14-Dec-01