-8

Windows 7中,我想用来PowerShell将当前目录及其子目录、子子目录等中的所有文件,以一定的LastWrite YEAR,移动到具有它们相对路径的新目录中。

有人可以写下我需要PowerShell使用的行代码吗?

4

2 回答 2

0

这是保持相对路径的解决方案。你需要的是 CD 到源目录然后更新

    $files = Get-ChildItem -Recurse -File | where {$_.LastWriteTime.Year -eq 2012}

并运行

    $baselength = (Get-Location).Path.Length + 1
    $destDir = 'G:\dest'

    $files = Get-ChildItem -Recurse -File | where {$_.LastWriteTime.Year -eq 2012}

    $files | foreach {
        Write-Verbose "moving $_.fullname"    
        $dest = Join-Path $destDir $_.FullName.Substring($baselength)
        if ( -not (Test-Path (Split-Path $dest)))
        {
            New-Item -ItemType directory -Path (Split-Path $dest) | Out-Null
        }
        Move-Item $_.FullName $dest -Force
    }
于 2013-04-08T11:02:32.733 回答
0

您可以使用该Move-Item功能将文件从一个路径移动到另一个路径

例子:

Move-Item 'c:\YourFolder\*' 'c:\DestinationFolder'

这会将文件夹中的所有文件移动到目标文件夹 (*)。


要从某个日期移动文件,您必须将其通过管道传输到where-object函数中:

例子:

Get-ChildItem 'C:\folder' | where-object {$_.lastwritetime.Year -eq 2013} |
Move-Item -destination 'c:\DestinationFolder'

http://technet.microsoft.com/en-us/library/ee176910.aspx

于 2013-04-08T10:14:18.410 回答