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.
- Select the files you want to update in the File Selector.
- Start with a fresh action.
- Set the action type to “rename files or folders”.
- 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.
- In the Search box, enter the old year (e.g. 2016).
- In the Replace box, enter the new year (e.g. 2017).
- Set “target file creation” to “copy files”.
- Set the backup file options as you like them.
- Click the New Step button on the Sequence panel to add the contents of the Action panel as the first step in the sequence.
- On the Action panel, change the action type to “search and replace”.
- 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.
- Click the New Step button on the Sequence panel to add the contents of the Action panel as the second step in the sequence.
- With the second step still selected on the Sequence panel, select “target files from other step” in the “file selection” drop down list.
- 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.
- 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”.