如何将所有文件和目录从磁盘移动到该磁盘上的文件夹?
问题描述:
我有磁盘D的文件和目录。我想将所有文件和目录(包括磁盘D的根目录)移动到保存树的新文件夹中。如何将所有文件和目录从磁盘移动到该磁盘上的文件夹?
- 例如,我有文件
D:\1.txt
。我想把它移到 - 第二个例子。我有档案
D:\1\1\1.txt
。我想将其移至
正如您所见,我想将磁盘D的所有内容移至文件夹D:\new_folder\
。
我试过robocopy,但是它不能从磁盘根目录移动。
答
没有测试:
for /d %%# in (D:\*) do (
xcopy "%%#" "D:\new_folder\%%~nx#" /i
)
for %%# in (D:\*) do (
copy /Y "%%#" "D:\new_folder\"
)
同样没有经过测试(使用PowerShell):
Get-ChildItem -Path "D:\*" | ForEach-Object {if((Get-Item $_) -is [System.IO.DirectoryInfo]){Copy-Item "$_.FullName" "D:\new_folder" -recurse } else {Copy-Item "$_.FullName" "D:\new_folder"}}
答
这将复制所有文件和文件夹保存的文件夹结构:
$sourcePath = "D:\"
$destPath = "D:\new_folder\"
Get-ChildItem $sourcePath -Recurse | Foreach-Object {
$destDir = Split-Path ($_.FullName -Replace [regex]::Escape($sourcePath), $destPath)
if (!(Test-Path $destDir)) {
New-Item -ItemType directory $destDir | Out-Null
}
Copy-Item $_.FullName -Destination $destDir
}
你有什么尝试?请分享代码。这似乎不是一个问题。获得childitem -recurse与移动项目应该这样做 –