Home  >  Article  >  Backend Development  >  Powershell - Find files using regular expressions

Powershell - Find files using regular expressions

高洛峰
高洛峰Original
2017-01-22 14:05:062309browse

Supports all PS versions

Get-ChildItem does not support advanced filtering of files. It can only use simple wildcards, but not regular expressions.

Revolving around this problem, we can use the -match command to filter.

The following example will obtain all files in the windows directory that contain at least two consecutive numbers and the file name length does not exceed 8 characters:

Get-ChildItem -Path $env:windir -Recurse -ErrorAction SilentlyContinue |
 Where-Object { $_.BaseName -match '\d{2}' -and $_.Name.Length -le 8 }

Pay attention to the file The attribute "BaseName" does not include the extension, so numbers appearing in the extension will not be counted.

For more Powershell-using regular expressions to find files related articles, please pay attention to the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn