Wildcard escape and regular expression escape
A simple problem can also lead to setbacks. First, you need to understand the differences between the two escape types.
Which must be escaped in powershellWildcardThere are four :('*','? ',' ['And'] ')
- Manually escape: Escape ''' (below the Escape key)
- Automatic escape: [management. Automation. wildcardpattern]: escape (string)
The regular expression must be escaped.MetacharactersAre: '\', '*', '+ ','? ',' | ',' {',' [',' (',') ',' ^ ',' $ ','. ',' # ', And blank space)
- Manually escape: backslash '\'
- Automatic escape: [RegEx]: escape (string)
Here is an interesting thing: The method names called by powershell are case-insensitive. Of course, this is also advantageous, and it is easier to lower the case. ===
Preparation
The purpose is to replace all files with a specific prefix in a folder.
$ Prefix = "[(Stanford University) programming method]. programming. methodology. (Stanford ). "# Count (LS *. MP4 ). count # wildcard escape $ prefixescapetest = [management. automation. wildcardpattern]: escape ($ prefix) # echo $ prefixescapetest # Result: "\ '[\ (Stanford University \) Programming Method \']. programming. methodology. \ (Stanford \). "# Regular Expression escape. $ Prefix = [RegEx]: escape ($ prefix) # Result: "\ [\ (Stanford University \) Programming Method] \. programming \. methodology \. \ (Stanford \)\. "Echo $ prefix
Rename-item
Ls *. MP4 |%{ Ren $ _ ($ _. name-Replace "^ $ prefix", '')} # Error # rename-item: cannot be renamed because there is no entry in" M: \ vid \ identifier. # Location G: \ Users \ LJ \ Documents \ Reg. PS1: 19 characters: 18 # + ls *. MP4 | % {Ren <$ _ ($ _. name-Replace "^ $ prefix", '')} # + categoryinfo: invalidoperation: (:) [rename-item], psinvalidoperationexception # + fullyqualifiederrorid: invalidoperation, Microsoft. powershell. commands. renameitemcommand
Move-item
-Literalpath <string []>
Specifies the path to the current position of the item. Different from path, the value of literalpath is strictly used as entered. No characters are interpreted as wildcards. If the path contains escape characters, enclose them in single quotes. Single quotes tell windows powershell not to interpret all characters as escape sequences.
Rename by moving is based on the method created in this article. Rename-item and square brackets in powershell (no-literalpath ):
ls *.mp4 | %{ mi -literalpath $_ ($_.name -replace "^$prefix",'') }
Learning Link
- Windows powershell Quick Start
- Technet windows powershell Core