Rename files in batches using powershell

Source: Internet
Author: User
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.