繁体   English   中英

Powershell:将文件夹和子文件夹中的所有文件移动到单个文件夹中

[英]Powershell: Move all files from folders and subfolders into single folder

试图在 8K 文件和 2K 文件夹中索引和搜索文件..

是否有一个简单的 Powershell 脚本可以将所有文件从文件夹和/或子文件夹移动到一个主文件夹中?

不需要删除空文件夹,但会有所帮助。

help -Examples Move-Item下的第四个示例接近您所需要的。 要将source目录下的所有文件移动到dest目录,您可以执行以下操作:

Get-ChildItem -Path source -Recurse -File | Move-Item -Destination dest

如果以后要清除空目录,可以使用类似的命令:

Get-ChildItem -Path source -Recurse -Directory | Remove-Item

使用.parent作为父目录。 它可以递归使用: .parent.parent

我知道这篇文章有点旧,但我遇到了一个类似的问题,但这并没有涵盖我需要维护所有 fsubolder 结构的情况,所以这是我维护子文件夹结构的解决方案

$sourcePath = "C:\FolderLocation"
$destPath = "C:\NewFolderLocation"
Write-Host "Moving all files in '$($sourcePath)' to '$($destPath)'"
$fileList = @(Get-ChildItem -Path "$($sourcePath)" -File -Recurse)
$directoryList = @(Get-ChildItem -Path "$($sourcePath)" -Directory -Recurse)
ForEach($directory in $directoryList){
    $directories = New-Item ($directory.FullName).Replace("$($sourcePath)",$destPath) -ItemType Directory -ea SilentlyContinue | Out-Null
}
Write-Host "Creating Directories"
ForEach($file in $fileList){
    try {
        Move-Item -Path $file.FullName -Destination ((Split-Path $file.FullName).Replace("$($sourcePath)",$destPath)) -Force -ErrorAction Stop
    }
    catch{
        Write-Warning "Unable to move '$($file.FullName)' to '$(((Split-Path $file.FullName).Replace("$($sourcePath)",$destPath)))': $($_)"
        return
    }
}
Write-Host "Deleting folder '$($sourcePath)'"
Remove-Item -Path "$($sourcePath)" -Recurse -Force -ErrorAction Stop

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM