Powershell - Find files using regular expressions

高洛峰
Release: 2023-03-05 09:36:01
Original
2436 people have browsed it

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 }
Copy after login

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!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template