powershell - 根据 "creationdate"复制文件并创建新文件夹

标签 powershell get-childitem copy-item

我正在用

计算我的图片文件夹中的所有文件
Get-ChildItem C:\pictures -force | Group-Object extension | Sort-Object count -descending | ft count,name -auto

然后我将我所有的 MTS 文件(视频)复制到一个单独的文件夹中

Get-ChildItem C:\pictures -force -recurse -include *.MTS | Copy-Item -Destination c:\video

这很好用。但是,如何在c:\video 中为每一年创建一个文件夹,然后复制相应的文件呢?

更新:

Shay 帮助我完成了这项工作,我现在有了以下代码:

# Create a folder for each year and move the specified files to the corresponding folders
Get-ChildItem $fromFolder -Force | 
Group-Object {$_.CreationTime.Year} | Foreach-Object {

    # Testing to see if the folder exist
    if(!(Test-Path $toFolder\$($_.Name))) { 
        $folder = New-Item -Path "$toFolder\$($_.Name)" Itemtype Directory -Force 
        echo "Created $toFolder\$($_.Name)"
    } else {
        echo "Folder $toFolder\$($_.Name) exist"
    }

    # Testing to see if the file exist in the target directory
    if(!(Test-Path $_.group)) { 
        $_.group | Copy-Item -Destination $folder.FullName
        echo "Copyied $_ to $folder"
        } else {
            echo "File exist"
        }
}    

它测试文件夹正常,但跳过文件上的所有 Test-Path。 我是否以某种方式打破了循环?还是弄乱了管道?

最佳答案

试试这个:

Get-ChildItem C:\pictures -Filter *.MTS -Force -Recurse | 
Group-Object {$_.CreationTime.Year} | Foreach-Object{
    $folder = New-Item -Path "c:\video\$($_.Name)" ItemType Directory -Force
    $_.Group | Where-Object { -not (Test-Path "$($folder.FullName)\$($_.Name)") } | Copy-Item -Destination $folder.FullName
}

关于powershell - 根据 "creationdate"复制文件并创建新文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11777466/

相关文章:

powershell - 如何找出我所在的域名的简称?

function - Powershell 函数字符串输出作为基于文件的函数的输入

Powershell copy-item 创建一个文件夹而不是复制文件

powershell - 删除大量文件而不会耗尽内存

Powershell:get-childitem 不适用于过滤器

powershell - powershell复制项路径中的非法字符

windows - 使用不同凭据在windows机器之间复制文件的解决方案

xml - 使用Powershell解析具有变量类型的XML文件

regex - powershell正则表达式用部分字符串替换整个字符串

powershell - 为什么命令行参数直接放入脚本中的命令时不起作用?