[英]Powershell copying specific files from all subfolders to a single folder
[英]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.