arrays - 如何从 powershell 中的扩展类型数组中提取/匹配文件类型扩展

标签 arrays powershell

我想使用 Powershell 从 Sharepoint 库中仅提取所需的文件类型

我正在使用以下逻辑,但它并没有 100% 正确工作。它仍然匹配文件名包含不好的扩展类型。

请帮我更正脚本。

$libfiles = ".pptx",".xlsx",".xls",".docx",".doc",".pdf",".jpeg",".jpg",".ppt",".xlsm",".gif",".png"
$libfilesregex = [string]::Join('|', $libfiles)
$arr = @("xlsfile.avi","asxlsxfile.mdf","navigation.css")
foreach($i in $arr)
{
    if($i -match $libfilesregex)
    {
        Write-Host " $($i) File type exists "
    }
    else
    {
        Write-Host " $($i) File type does not exist "
    }
}

正在输出:

xlsfile.avi File type does not exist

asxlsxfile.mdf File type exists

navigation.css File type does not exist

注意没有一个应该被报告为存在,但是 asxlsxfile.mdf 出乎意料的是。

谢谢 维斯瓦

最佳答案

我相信这就是您要找的。我明确地从文件数组中提取文件扩展名,如果扩展名存在则打印。

$libfiles = ".pptx",".xlsx",".xls",".docx",".doc",".pdf",".jpeg",".jpg",".ppt",".xlsm",".gif",".png"
$arr = @("xlsfile.avi","asxlsxfile.mdf","navigation.css", "lib.pptx")
foreach($i in $arr)
{
    if($libfiles -contains [io.path]::GetExtension($i))
    {
        Write-Host  "$([io.path]::GetExtension($i)) File type exists "
    }
    else
    {
        Write-Host "$([io.path]::GetExtension($i)) File type does not exist "
    }
}

关于arrays - 如何从 powershell 中的扩展类型数组中提取/匹配文件类型扩展,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40242865/

相关文章:

c - 在 C 中在 main 外部定义一个数组,并在 main 内部分配其大小

powershell - 为什么没有将所有 Powershell 输出写入 txt 文件

Excel 复制粘贴单元格范围

iis - "PS c:\> New-WebSite -Blah:"抛出索引超出数组范围

javascript - 如何获取随机定位的对象的属性值

php - 比较两个关联数组的键顺序

c - 当我只分配 [30] 时,我可以访问数组元素 [32]。(C)

php - 将多个值插入数据库的最佳方法 - PHP MySQL

powershell - 过滤 cmd 命令的输出,而无需使用中间文件

windows - Powershell 获取凭据失败