简体   繁体   English

Powershell 移动文件但不移动子文件夹

[英]Powershell move files but not sub-folders

I need to moves all the files from a folder to another but not the sub-folders or anything inside it.我需要将所有文件从一个文件夹移动到另一个文件夹,而不是子文件夹或其中的任何内容。 I have no idea of the name or the extension of the files I'll need to move (and some of them will not have any) so I can't use -Include.我不知道我需要移动的文件的名称或扩展名(其中一些没有),所以我不能使用 -Include。 Here is an exemple of the structure of my folder:这是我的文件夹结构的示例:

  • Folder1文件夹 1
    • File1文件 1
    • File2文件 2
    • Folder2文件夹 2
      • File3文件 3
      • File4文件 4
  • Folder3文件夹3

I want to move File1 and File2 in Folder3 without moving File3 and File4.我想在不移动 File3 和 File4 的情况下移动 Folder3 中的 File1 和 File2。

So far, I have something that looks like this:到目前为止,我有一些看起来像这样的东西:

$SouceFolder = "c:\Folder1\*"
$DestinationFolder = "c:\Folder3"

Move-Item -Path $SouceFolder -Destination $DestinationFolder

But it's copying the whole folder structure, with all the files.但它正在复制整个文件夹结构,包括所有文件。

Thanks.谢谢。

Solution:解决方案:

$SouceFolder = "c:\Folder1"
$DestinationFolder = "c:\Folder3"

Robocopy $SouceFolder $DestinationFolder /mov

Note: I removed the \\* from the folder1.注意:我从文件夹 1 中删除了 \\*。

Did you try using -Recurse option like this?您是否尝试使用这样的 -Recurse 选项?

Get-ChildItem -Path "c:\\Folder1\\*.*" -Recurse | Move-Item -Destination "c:\\Folder3\\"

There is better alternative Robocopy for copying files.有更好的替代 Robocopy 来复制文件。 With robocopy command you have better control on what is being copied with error codes if it fails.使用 robocopy 命令,如果失败,您可以更好地控制正在复制的带有错误代码的内容。

in your case it would be just a simple code like this.在你的情况下,它只是一个像这样的简单代码。

$SouceFolder = "c:\Folder1"
$DestinationFolder = "c:\Folder3"
Robocopy $SouceFolder $DestinationFolder

see the robocopy official documentation for more option有关更多选项,请参阅robocopy 官方文档

This should work.这应该有效。

Note I'm not appending \\* to the SourceFolder注意我没有\\*附加到 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"

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

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