Skip to content

Commit

Permalink
Merge pull request #3976 from Jay-o-Way/add-examples-to-PowerRename
Browse files Browse the repository at this point in the history
PowerToys: Add two examples of RegEx search/replace
  • Loading branch information
Jak-MS authored Aug 1, 2022
2 parents a5b002b + 5990c93 commit 97a159d
Showing 1 changed file with 19 additions and 23 deletions.
42 changes: 19 additions & 23 deletions hub/powertoys/powerrename.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,12 @@ PowerRename is a bulk renaming tool that enables you to:
- Check expected rename results in a preview window before finalizing a bulk rename.
- Undo a rename operation after it is completed.


## Demo

In this demo, all instances of the file name "foo" are replaced with "foobar". Since all of the files are uniquely named, this would have taken a long time to complete manually one-by-one. PowerRename enables a single bulk rename. Notice that the Explorer's "Undo Rename" (Ctrl+Z) command enables the ability to undo the last change.

![PowerRename Demo.](../images/powerrename-demo.gif)


## PowerRename window

After selecting files in Windows File Explorer, right-clicking and selecting **PowerRename** (which will appear only when enabled in PowerToys), the PowerRename window will appear. The number of items you've selected will be displayed, along with search and replace values, a list of options, and a preview window displaying results of the search and replace values you've entered.
Expand Down Expand Up @@ -91,7 +89,6 @@ Select between four options to either convert items to be all lowercase, all upp

Appends a numeric suffix to file names that were modified in the operation. For example: `foo.jpg` -> `foo (1).jpg`


## Replace using file creation date and time

The creation date and time attributes of a file can be used in the _Replace with_ text by entering a variable pattern according to the table below. Selecting the tool-tip in the _Replace with_ field allows you to view and select from the supported patterns.
Expand Down Expand Up @@ -134,7 +131,6 @@ The value of the renamed file would result in:
- `Nov-02-20-powertoys.png`
- `Nov-03-20-powertoys-menu.png`


## Regular Expressions

For most use cases, a simple search and replace is sufficient. There may be occasions, however, in which complicated renaming tasks require more control. [Regular Expressions](https://wikipedia.org/wiki/Regular_expression) can help.
Expand All @@ -151,28 +147,30 @@ To use the [Boost library](https://www.boost.org/doc/libs/1_74_0/libs/regex/doc/

Simple matching examples

| Search for | Description |
| :--- | :--- |
| `^` | Match the beginning of the filename (zero size) |
| `$` | Match the end of the filename (zero size) |
| `.*` | Match all the text in the name |
| `^foo` | Match text that begins with "foo" |
| `bar$` | Match text that ends with "bar" |
| `^foo.*bar$` | Match text that begins with "foo" and ends with "bar" |
| `.+?(?=bar)` | Match everything up to "bar" |
| `foo[\s\S]*bar` | Match everything between and including "foo" and "bar"|
| Search for | Description |
|:----------------|:-------------------------------------------------------|
| `^` | Match the beginning of the filename (zero size) |
| `$` | Match the end of the filename (zero size) |
| `.*` | Match all the text in the name |
| `^foo` | Match text that begins with "foo" |
| `bar$` | Match text that ends with "bar" |
| `^foo.*bar$` | Match text that begins with "foo" and ends with "bar" |
| `.+?(?=bar)` | Match everything up to "bar" |
| `foo[\s\S]*bar` | Match everything between and including "foo" and "bar" |

Matching and variable examples. Capturing groups are defined in parentheses `()`. To refer to them, use `$` followed by a number: `$1` will refer to the first group, `$2` to the second etc.

_When using the variables, the "Match all occurrences" option must be enabled._

| Search for | Replace with | Description |
| :--- | :--- | :--- |
| `(.*).png` | `foo_$1.png` | Prepends "foo\_" to the existing file name for PNG files |
| `(.*).png` | `$1_foo.png` | Appends "\_foo" to the existing file name for PNG files |
| `(.*)` | `$1.txt` | Appends ".txt" extension to existing file |
| `(^\w+\.$)¦(^\w+$)` | `$2.txt` | Appends ".txt" extension to existing file name only if it does not have an extension |
| `(\d\d)-(\d\d)-(\d\d\d\d)` | `$3-$2-$1` | Move numbers in the filename: "29-03-2020" becomes "2020-03-29" |
| Search for | Replace with | Description |
|:--------------------------------------------------------|:-------------|:-------------------------------------------------------------------------------------|
| `(.*).png` | `foo_$1.png` | Prepends "foo\_" to the existing file name for PNG files |
| `(.*).png` | `$1_foo.png` | Appends "\_foo" to the existing file name for PNG files |
| `(.*)` | `$1.txt` | Appends ".txt" extension to existing file |
| `(^\w+\.$)¦(^\w+$)` | `$2.txt` | Appends ".txt" extension to existing file name only if it does not have an extension |
| `(\d\d)-(\d\d)-(\d\d\d\d)` or `(\d{2})-(\d{2})-(\d{4})` | `$3-$2-$1` | Move numbers in the filename: "29-03-2020" becomes "2020-03-29" |
| `^(.{n})(.*)` or `(.*)(.{n})$` | `$1foo$2` | Insert "foo" _n_ characters from the beginning or the end, respectively |
| `^.{n}` or `.{n}$` | nothing | Trim _n_ characters from the beginning or the end, respectively |

### Additional resources for learning regular expressions

Expand All @@ -182,7 +180,6 @@ There are great examples/cheatsheets available online to help you:

[ECMAScript Regular Expressions Tutorial](https://o7planning.org/en/12219/ecmascript-regular-expressions-tutorial)


## File List Filters

Filters can be used in PowerRename to narrow the results of the rename. Use the _Preview_ window to check expected results. Click the column headers to switch between filters.
Expand All @@ -197,7 +194,6 @@ Filters can be used in PowerRename to narrow the results of the rename. Use the

![PowerToys PowerRename Filter demo.](../images/powerrename-demo2.gif)


## Settings

Additional options can be configured from the PowerRename tab in the Settings menu as described below:
Expand Down

0 comments on commit 97a159d

Please sign in to comment.