用数组中的通配符解释扩展

时间:2019-07-26 16:34:34

标签: arrays powershell wildcard

为什么这样做:

$VideoExtensions = @('.mp4','.mov','.mpg','.mts','.3g2','.3gp','.avi','.mkv','.flv','.wmv')
$Files = Get-ChildItem -LiteralPath $PSScriptRoot -Recurse | Where-Object {
    $_.Extension -match '.jp*' -or
    $_.Extension -in $VideoExtensions
}

但不是这样:

$PicExtensions = @('.jp*','.png')
$VideoExtensions = @('.mp4','.mov','.mpg','.mts','.3g2','.3gp','.avi','.mkv','.flv','.wmv')
$Files = Get-ChildItem -LiteralPath $PSScriptRoot -Recurse | Where-Object {
    $_.Extension -in $PicExtensions -or
    $_.Extension -in $VideoExtensions
}

.jp*通配符被完全忽略。如果我使用-like,它将捕获不完全是.png的文件吗?我不喜欢使用-like运算符。

2 个答案:

答案 0 :(得分:0)

我强烈建议您使用正则表达式而不是像这样的字符串列表。

Imports Microsoft.Office.Interop.Excel
Public Class Form1
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        NumericUpDown1.Value = My.Settings.num
    End Sub

    ' Create class variables for Excel application and the opened workbook.
    private app As New Application
    private wb As Workbook

    Private Sub OpenToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles OpenToolStripMenuItem.Click
        Dim ws As Worksheet
        wb = app.Workbooks.Open("C:\Users\Alex\Desktop\Index.xlsx")
        ws = wb.Sheets(1)
        TextBox1.Text = ws.Range("A" & NumericUpDown1.Value).Value
        TextBox2.Text = ws.Range("B" & NumericUpDown1.Value).Value
        TextBox3.Text = ws.Range("C" & NumericUpDown1.Value).Value
        TextBox4.Text = ws.Range("D" & NumericUpDown1.Value).Value
        TextBox5.Text = ws.Range("E" & NumericUpDown1.Value).Value
        TextBox6.Text = ws.Range("F" & NumericUpDown1.Value).Value
        TextBox7.Text = ws.Range("G" & NumericUpDown1.Value).Value
        TextBox8.Text = ws.Range("H" & NumericUpDown1.Value).Value
    End Sub

    ' Create another menu item to close the workbook, and handle its Click event here.
    Private Sub OpenToolStripMenuItem2_Click(sender As Object, e As EventArgs) Handles OpenToolStripMenuItem2.Click
        wb.Close()
        app.Quit()
        app = Nothing
        wb = Nothing
        ws = Nothing
        My.Settings.num = NumericUpDown1.Value
        My.Settings.Save()
    End Sub
End Class

如果您想继续使用字符串列表(不建议使用),则必须逐个比较每个元素(势必性能很差),因为$PicExtensions = 'jp.*|png' $VideoExtensions = 'mp4|mov|mpg|mts|3g2|3gp|avi|mkv|flv|wmv' $Files = Get-ChildItem -LiteralPath $PSScriptRoot -Recurse | Where-Object { $_.Extension -match "^\.(${PicExtensions})$" -or $_.Extension -match "^\.(${VideoExtensions})$" } -contains运算符允许仅完全匹配,而您想进行通配符匹配。

-in

答案 1 :(得分:0)

如果不必一定是字面值,则可以在路径中指定多个模式。

$pic = echo *.jp* *.png
$vid = echo *.mp4 *.mov *.mpg *.mts *.3g2 *.3gp *.avi *.mkv *.flv *.wmv

get-childitem -path ($pic + $vid) -recurse