【问题标题】:Powershell - Take a fix amount of files and move them to new folderPowershell - 获取固定数量的文件并将它们移动到新文件夹
【发布时间】:2017-12-03 15:09:08
【问题描述】:

我正在寻找我找到的类似这样的 powershell 脚本:

Get-ChildItem -File |  # Get files
  Group-Object { $_.Name -replace '_.*' } |  # Group by part before first underscore
  ForEach-Object {
    # Create directory
    $dir = New-Item -Type Directory -Name $_.Name
    # Move files there
    $_.Group | Move-Item -Destination $dir
  }

但不同的是,组对象应该从文件夹 A 中获取固定数量的 5 个文件,创建一个以第一个文件命名的新文件夹,并将 5 个文件移动到新文件夹中。请参见下面的示例图片(文件名不同)。 我是 powershell 的血腥初学者,所以如果可能的话,请保持建议简单;)

enter image description here
enter image description here

谢谢和问候!

【问题讨论】:

  • @Paul:Group-Object 按文件名中(第一个)_ 之前的共享文件名前缀对输入文件进行分组。然后ForEach-Object 对每个生成的组进行操作,创建一个以共享前缀命名的目录,并将组中的所有文件移动到该新目录。
  • @nosediver:您是否只想从每个组中获取前 5 个文件?如果您想跨组进行分区,则不清楚您正在寻找什么逻辑。请通过直接更新您的问题来澄清。
  • 将分组更改为除以 5 的计数器并四舍五入到最接近的整数$n = 0; Get-ChildItem -File | Group-Object -Property {$script:n++; [math]::Ceiling($n/5)} | ForEach-Object { your code here } 可能会这样做
  • @TessellatingHeckler:谢谢。它接缝它有点工作。结果:脚本列出了正确的文件,但命名错误,它仅以数字 5 开头,(下一个组 6 等)没有字母。 New-Item 有一些错误:+ $dir = New-Item -Type Directory -Name $_.Name + CategoryInfo : ResourceExists: ("path") [New- Item], IOException + FullyQualifiedErrorId : DirectoryExist,Microsoft .PowerShell.Commands.NewItemCommand 导致第一个错误,Move-Item 无法工作。
  • 对不起,错过了第一个,组的编号从 1 开始,而不是像我写的那样从 5 开始!

标签: powershell


【解决方案1】:

感谢@TessellatingHeckler,效果很好。

$n = 0; Get-ChildItem -File | Group-Object -Property {$script:n++; [math]::Ceiling($n/5)} | 
ForEach-Object {                               
    $dir = New-Item -Type Directory -Name $_.Name   # Create directory
    $_.Group | Move-Item -Destination $dir          # Move files there
}

对于重命名部分,我使用了来自@Dennis van Gils (How to rename a folder the same as the filename of the first file inside that folder using Windows batch programming?) 的批次 我确信有一种方法可以与 powershell 部分一起使用,但是这个解决方案对我有用。

【讨论】:

    【解决方案2】:

    你好。如果你想创建一个以第一个文件命名的新文件夹,你也可以试试这个:

    ForEach-Object { 
    $dname = $_.group.name    
    $fname=$dname.replace(".","")    
    $fl = $fname[0]
        $dir = New-Item -Type Directory -Name $fl                      
    
        $_.Group | Move-Item -Destination $dir 
    

    【讨论】:

    • 谢谢你,维塔利。完美运行!它只需要删除以文件夹名称结尾的文件。
    • 如果您不需要使用文件扩展名,您可以在此替换 $fname=$dname.replace(".","") $fl = $fname[0]:$fname= $dname $fl = $fname[0].Split(".")[0]
    【解决方案3】:

    感谢@Vitaly!该解决方案效果很好。
    这是完整的脚本:

    $n = 0; Get-ChildItem -File | Group-Object -Property {$script:n++; [math]::Ceiling($n/5)} | 
    ForEach-Object { 
    $dname = $_.group.name    
    $fname=$dname 
    $fl = $fname[0].Split(".")[0]
        $dir = New-Item -Type Directory -Name $fl                      
    
        $_.Group | Move-Item -Destination $dir     
    }
    

    【讨论】:

      猜你喜欢
      • 2013-12-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-06
      相关资源
      最近更新 更多