gpt4 book ai didi

windows - 使用 Powershell 将所有文件夹和子文件夹从一个驱动器移动到另一个驱动器

转载 作者:可可西里 更新时间:2023-11-01 11:50:43 26 4
gpt4 key购买 nike

我正在使用 Powershell 脚本将文件夹从一个驱动器移动到另一个驱动器。

这是我尝试过的。

Get-ChildItem -Recurse "C:\Personal"  | where-object {$_.lastwritetime -gt '5-25-2015'} | foreach {move-item "$($_.FullName)" "D:\Personal"}

如果我在同一个驱动器内移动文件,即从 c 驱动器到 c 驱动器或从 d 驱动器到 d 驱动器,这是有效的。但是,当我尝试从 C 驱动器移动到 D 驱动器时,这不起作用,...我收到类似

的错误
Move-Item : The file exists.
At line:1 char:113
+ Get-ChildItem -Recurse "C:\Development" | where-object {($_.lastwritetime -lt (get-date))} | foreach {move-item <<<<
"$($_.FullName)" "D:\Development1"}
+ CategoryInfo : WriteError: (C:\Development\test3.txt:FileInfo) [Move-Item], IOException
+ FullyQualifiedErrorId : MoveFileInfoItemIOError,Microsoft.PowerShell.Commands.MoveItemCommand

请纠正我..

最佳答案

“这不起作用”是什么意思?

递归标志似乎表明您要复制目录结构。这仅在您的目标目录已经具有与源目录相同的结构时才有效。如果没有,您必须一路创建它。这样的事情会起作用:

function Move-Directories 
{
param (
[parameter(Mandatory = $true)] [string] $source,
[parameter(Mandatory = $true)] [string] $destination
)

try
{
Get-ChildItem -Path $source -Recurse -Force |
Where-Object { $_.psIsContainer } |
ForEach-Object { $_.FullName -replace [regex]::Escape($source), $destination } |
ForEach-Object { $null = New-Item -ItemType Container -Path $_ }

Get-ChildItem -Path $source -Recurse -Force |
Where-Object { (-not $_.psIsContainer) -and ($_.lastwritetime -ge (get-date)) } |
Move-Item -Force -Destination { $_.FullName -replace [regex]::Escape($source), $destination }
}

catch
{
Write-Host "$($MyInvocation.InvocationName): $_"
}
}

像这样调用:

Move-Directories "c:\personal" "d:\personal"

关于windows - 使用 Powershell 将所有文件夹和子文件夹从一个驱动器移动到另一个驱动器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30499129/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com