[英]Powershell move files but not sub-folders
我需要將所有文件從一個文件夾移動到另一個文件夾,而不是子文件夾或其中的任何內容。 我不知道我需要移動的文件的名稱或擴展名(其中一些沒有),所以我不能使用 -Include。 這是我的文件夾結構的示例:
我想在不移動 File3 和 File4 的情況下移動 Folder3 中的 File1 和 File2。
到目前為止,我有一些看起來像這樣的東西:
$SouceFolder = "c:\Folder1\*"
$DestinationFolder = "c:\Folder3"
Move-Item -Path $SouceFolder -Destination $DestinationFolder
但它正在復制整個文件夾結構,包括所有文件。
謝謝。
解決方案:
$SouceFolder = "c:\Folder1"
$DestinationFolder = "c:\Folder3"
Robocopy $SouceFolder $DestinationFolder /mov
注意:我從文件夾 1 中刪除了 \\*。
您是否嘗試使用這樣的 -Recurse 選項?
Get-ChildItem -Path "c:\\Folder1\\*.*" -Recurse | Move-Item -Destination "c:\\Folder3\\"
有更好的替代 Robocopy 來復制文件。 使用 robocopy 命令,如果失敗,您可以更好地控制正在復制的帶有錯誤代碼的內容。
在你的情況下,它只是一個像這樣的簡單代碼。
$SouceFolder = "c:\Folder1"
$DestinationFolder = "c:\Folder3"
Robocopy $SouceFolder $DestinationFolder
有關更多選項,請參閱robocopy 官方文檔
這應該有效。
注意我沒有將\\*
附加到 SourceFolder
$SourceFolder = "c:\Folder1"
$DestinationFolder = "c:\Folder3"
# create the target folder if it does not already exist
if (!(Test-Path -Path $DestinationFolder -PathType Container)) {
$null = New-Item -Path $DestinationFolder -ItemType Directory
}
Get-ChildItem -Path $SourceFolder -File | Move-Item -Destination $DestinationFolder
嘗試這個:
Get-Childitem "FilePath" -Recurse | Where-Object {!($_.PSIscontainer)} | Move-Item -Path $_.Fullname -Destination "Destination"
Move-Item -Path FolderToMove -Destination DestinationFolder
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.