代码之家  ›  专栏  ›  技术社区  ›  crichavin

获取目录中扩展名为.JPG的文件,目录名格式为YYYY-MM-DD[重复]

  •  1
  • crichavin  · 技术社区  · 7 年前

    我想获取当前目录中的所有.JPG文件以及目录名所在的所有子目录 YYYY-MM-DD

    D:\Pictures\2018-01-01\DSC_0001.JPG <- yes, include
    D:\Pictures\2018\01\DSC_0001.JPG <- do not include
    

    这就是我尝试过的,运气不好。

    $testFiles = Get-ChildItem -Path $srcFolder -Filter *.JPG | ? { (Split-Path (Split-Path $_ -Parent) -Leaf) -match '^\d{4}-\d{2}-\d{2}$' }

    3 回复  |  直到 7 年前
        1
  •  2
  •   Drew    7 年前

    你差一点就成功了。

    $testFiles = Get-ChildItem -Path $srcFolder -Recurse -Filter *.JPG 
    | Where-Object { (Split-Path (Split-Path $_.FullName -Parent) -Leaf) -match '^\d{4}-\d{2}-\d{2}$' }
    

    您需要使用设置路径 $_.Fullname $_ 传递整个对象。

        2
  •  1
  •   Lee_Dailey    7 年前

    下面是一种稍微不同的获取文件列表的方法。[ ]它是针对 .Directory 文件的属性。

    _[编辑-与原始版本匹配的原始版本] 全部的

    $SourceDir = $env:temp
    $Filter = '*.log'
    # this pattern will give embedded date patterns
    #$DirPattern = '\d{4}-\d{2}-\d{2}'
    # this pattern gives ONLY a date pattern
    $DirPattern = '^\d{4}-\d{2}-\d{2}$'
    
    $GCI_Params = @{
         LiteralPath = $SourceDir
         Filter = $Filter
         File = $True
         Recurse = $True
        }
    $FileList = Get-ChildItem @GCI_Params |
        # this matches against the entire directory
        #Where-Object {$_.Directory -match $DirPattern}
        # this one correctly filters against only the parent dir
        Where-Object {(Split-Path -Path $_.DirectoryName -Leaf) -match $DirPattern}
    
    $FileList.Count
    

    在我的系统上,此时它返回~~ 67~~ 54 作为匹配文件的计数。

        3
  •  0
  •   Rich Moss    7 年前

    我不确定你在用嵌套的分割路径做什么,但在我的测试中它不起作用。

    这是有效的,并且可能满足您的需求:

    gci *.jpg -Recurse | ? { $_.FullName -match '\\\d{4}-\d{2}-\d{2}\\' }`
    
    推荐文章