Noirdesir
Noirdesir

Reputation: 85

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. Here is an exemple of the structure of my folder:

I want to move File1 and File2 in Folder3 without moving File3 and File4.

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.

Upvotes: 2

Views: 3628

Answers (5)

Maria MacCallum
Maria MacCallum

Reputation: 1

move files but not subdirs and their files:

Get-ChildItem -Path FROMHERE -Attributes  !Directory| move-item -Destination TOHERE

Upvotes: 0

Nando
Nando

Reputation: 120

[https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.management/move-item?view=powershell-7][1]

Move-Item -Path FolderToMove -Destination DestinationFolder

Upvotes: 0

Kundan
Kundan

Reputation: 1415

Did you try using -Recurse option like this?

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

There is better alternative Robocopy for copying files. With robocopy command you have better control on what is being copied with error codes if it fails.

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

Upvotes: 3

wasif
wasif

Reputation: 15478

Try this:

Get-Childitem "FilePath" -Recurse | Where-Object {!($_.PSIscontainer)} | Move-Item -Path $_.Fullname -Destination "Destination"

Upvotes: 0

Theo
Theo

Reputation: 61068

This should work.

Note I'm not appending \* to the 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

Upvotes: 1

Related Questions