尝试在8K文件和2K文件夹中索引和搜索文件..
是否有一个简单的PowerShell脚本,可以将所有文件从文件夹和/或子文件夹移动到一个主文件夹中?
不需要删除空文件夹,但这会有所帮助。
尝试在8K文件和2K文件夹中索引和搜索文件..
是否有一个简单的PowerShell脚本,可以将所有文件从文件夹和/或子文件夹移动到一个主文件夹中?
不需要删除空文件夹,但这会有所帮助。
help -Examples Move-Item
[1] 中的第四个示例与您需要的非常接近。要将 SOURCE
目录下的所有文件移动到 DEST
目录下,您可以执行以下操作:
Get-ChildItem -Path SOURCE -Recurse -File | Move-Item -Destination DEST
如果您想在之后清除空目录,可以使用类似的命令:
Get-ChildItem -Path SOURCE -Recurse -Directory | Remove-Item
[1] https://learn.microsoft.com/zh-cn/powershell/module/microsoft.powershell.management/move-item
使用.parent
表示父级目录。它可以递归使用:.parent.parent
$files = Get-ChildItem -Path . -Recurse -File
foreach ($file in $files) {
$dest = Join-Path . $file.Name
if (Test-Path $dest) {
$i = 1
do {
$newName = "$($file.BaseName)_$i$($file.Extension)"
$newDest = Join-Path . $newName
$i++
} while (Test-Path $newDest)
Move-Item -Path $file.FullName -Destination $newDest
} else {
Move-Item -Path $file.FullName -Destination $dest
}
}
Get-ChildItem -Directory | Where-Object {(Get-ChildItem $_ -Recurse -File).count -eq 0} | ForEach-Object {Remove-Item $_ -Recurse}
Get-ChildItem -Path ./ -Recurse -File | Move-Item -Destination ./ ; Get-ChildItem -Path ./ -Recurse -Directory | Remove-Item;
- Vael Victus