复制文件,并根据“creationdate”创建新文件夹(Copying files and cre

2019-09-20 16:29发布

我指望所有的文件在我的图片文件夹

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 ,然后复制相应的文件?

更新:

吉文已经帮我实现这个和我现在有下面的代码:

# 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"
        }
}    

它测试的文件夹OK,但跳过所有Test-Path上的文件。 我是不是打破了环不知何故? 或搞乱管道?

Answer 1:

尝试这个:

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
}


文章来源: Copying files and creating new folders based on “creationdate”