【问题标题】:Powershell: Copy selected files in subfoldersPowershell:复制子文件夹中的选定文件
【发布时间】:2013-02-27 10:12:35
【问题描述】:

我有一个文件夹 c:\graphics (root) 和一个创建子文件夹的程序,例如aabmczxmjpqzz(我有很多子文件夹,长度可变)。

在每个子文件夹中,我有 26 个文件和一个文件夹。例如,aa 文件夹有:aa_000.jpgaa_001.jpg、...、aa_023.jpg;另外两个文件aa_360.jpgaa.html 和一个文件夹。完整路径为C:\graphics\aa\aa_360

我需要移动子文件夹 c:\graphics\aa\aa_360 中的 24 个文件(aa_000.jpgaa_001.jpg、...、aa_023.jpg),并且也等于每个子文件夹。

另一个例子是c:\graphics\mjpq\mjpq_360,应该有mjpq_000.jpg, ..., mjpq_023.jpg

我开始编写脚本,想将所有.jpg(25 个文件)移动到子文件夹中(后来,我不得不想像提取xxx_360.jpg)但它不起作用:

Get-ChildItem c:\graphics -rec | where-object {$_.extension -match "jpg"} | ForEach-object {$newPath =(Get-Location).toString()+(Get-Location).ToString().SubString(10)+"_360"); Move-Item $_.FullName $newPath}

但是Get-Location 没有找到文件路径。


注意:我找到了一个可行的解决方案,但它在控制台中引发了一些错误:

Get-ChildItem c:\graphics -rec | where-object {$_.extension -match "jpg" -and (!($_.name -like "*360*"))} | ForEach-Object {$newPath =($_.DirectoryName).toString()+($_.DirectoryName).ToString().SubString(10)+"_360"; Move-Item $_.FullName $newPath}

【问题讨论】:

    标签: file powershell directory subdirectory


    【解决方案1】:

    我认为您可以将其简化为:

    Get-ChildItem c:\graphics -rec *.jpg | Move-Item -Dest {$_.Directory.FullName + "\" +
        $_.Directory.Name + "_360\" } -WhatIf
    

    如果建议的移动操作看起来正确,请删除 -whatif

    【讨论】:

    • 工作。我发现了错误,但它有效。创建一个文件更多;)。谢谢
    • 我认为您需要 Get-ChildItem 命令中的 -include 参数?
    • @Ant 这使用 -filter 参数按位置。
    • 啊太棒了 - 对 powershell 来说还是新手(你可能已经知道了!)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-04-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-10
    • 1970-01-01
    相关资源
    最近更新 更多