Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PowerToys: Add two examples of RegEx search/replace #3976

Merged
merged 1 commit into from
Aug 1, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
|:--------------------------------------------------------|:-------------|:-------------------------------------------------------------------------------------|
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a note about this table formatting: The dashes here are just a formatting preference for the markdown. Doesn't impact the way the table appears after being built.

Copy link
Contributor Author

@Jay-o-Way Jay-o-Way Aug 1, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hah I know, just clicked "expand this table" in VS Code. I can use both.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh! Right on - haven't tried that. Good to know that's how it handles it. I've seen writers have strong opinions on what is "best" (which clearly doesn't matter... but you know, folks love to have strong opinions). 😜

| `(.*).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