Replace in File Names and Contents

You can search and replace through file names with the "rename files or folders" action type. You can search and replace through the contents of files with the "search and replace" action type. PowerGREP does not have an action type that does both at the same time. Fortunately, we can use the Sequence panel to execute two actions as one operation.

Let's say you have a set of files that need to be updated annually. The files that need updating have the year in their file name. You have to create a copy of those files with the new year in the file name. You also have to replace the year in the contents of those files.

  1. Select the files you want to update in the File Selector.
  2. Start with a fresh action.
  3. Set the action type to "rename files or folders".
  4. Set "what to rename" to "full path" if the folders containing the files also have year numbers in them, and you want to create new folders for the new year.
  5. In the Search box, enter the old year (e.g. 2016).
  6. In the Replace box, enter the new year (e.g. 2017).
  7. Set "target file creation" to "copy files".
  8. Set the backup file options as you like them.
  9. Click the New Step button on the Sequence panel to add the contents of the Action panel as the first step in the sequence.
  10. On the Action panel, change the action type to "search and replace".
  11. Set "target file creation" to "modify original files". In this case, the original files will be the files that were copied by the first step in the sequence.
  12. Click the New Step button on the Sequence panel to add the contents of the Action panel as the second step in the sequence.
  13. With the second step still selected on the Sequence panel, select "target files from other step" in the "file selection" drop down list.
  14. Select step 1 in the "step" drop-down list. The second step is now configured to process the target files created by the first step.
  15. Click the Execute button on the Sequence panel to execute both steps. The first step copies the files, changing 2016 in their paths into 2017. When that's done, the second step searches through the contents of the copied files, replacing 2016 with 2017.

This sequence is available in the PowerGREP5.pgl library as "Replace in file names and contents".