簡體   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