在Windows 7
中,我想用来PowerShell
将当前目录及其子目录、子子目录等中的所有文件,以一定的LastWrite YEAR
,移动到具有它们相对路径的新目录中。
有人可以写下我需要PowerShell
使用的行代码吗?
在Windows 7
中,我想用来PowerShell
将当前目录及其子目录、子子目录等中的所有文件,以一定的LastWrite YEAR
,移动到具有它们相对路径的新目录中。
有人可以写下我需要PowerShell
使用的行代码吗?
这是保持相对路径的解决方案。你需要的是 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
}
您可以使用该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'