r/PowerShell • u/Gold_Divide_3381 • Jul 13 '26
Solved Move-Item creates duplicate folder and stores it inside existing folder
EDIT: Solved...
Move-Item -Path $_ -Destination $DestinationPath -Force--------------------------------------------------------------------
I have the following module that moves the contents of a directory into another...
function PSTransfer {
param (
[string]$BasePath,
[string]$DestinationPath
)
Get-ChildItem -Path "$BasePath/*” -Force | ForEach-Object {
if (!($_.FullName -like "*.DS_Store") {
Move-Item -LiteralPath "$($_.FullName)" -Destination "$($DestinationPath)/$($_.Name)" -Force
}
}
return
}
Export-ModuleMember PSTransferHowever when it moves a directory and the destination already has a folder with the same name, the folder is put inside the folder instead of being merged, for example...
BasePath/
├─ synced_imgs/
│ ├─ IMG_2048.jpg
DestinationPath/
├─ synced_imgs/
│ ├─ IMG_0512.jpg
│ ├─ IMG_1024.jpgThen after I run the module...
DestinationPath/
├─ synced_imgs/
│ ├─ synced_imgs/
│ │ ├─ IMG_2048.jpg
│ ├─ IMG_0512.jpg
│ ├─ IMG_1024.jpgIt doesn't do it recursviely though which is the weird thing. If I run it again it'll do this...
BasePath/
├─ synced_imgs/
│ ├─ IMG_4096.jpg
DestinationPath/
├─ synced_imgs/
│ ├─ synced_imgs/
│ │ ├─ IMG_2048.jpg
│ │ ├─ IMG_4096.jpg
│ ├─ IMG_0512.jpg
│ ├─ IMG_1024.jpgWhat am I doing wrong here? I thought the -Force parameter was supposed to prevent this.
4
u/Icy-State5549 Jul 13 '26
I already commented once, but just replace all that shit with move-item wherever "pstransfer" is specified. That "module" is a waste of coding.
2
u/Ziptex223 Jul 13 '26
Show us how you're calling it. For the second run are you passing it just like c/example or c/example/synced_images? The first would make/merge a folder called synced_images in c/example, the second makes/merges a folder called synced_images in c/example/synced_images which would result in what you're seeing with it nested.
So I would check how you're calculating the destination path to be passed into this function.
4
u/BlackV Jul 13 '26
I'd recommend stepping through it and validating your variables (i.e. use your debugger)
look at the differences between
"$($DestinationPath)/$($_.Name)"vs"$($DestinationPath)"you could also look at the
-filter/-include/-excludeparameters instead of theif1
u/thehuntzman Jul 13 '26
You need to filter get-childitem to only look for files I think and then doing a move-item -force will create directories as needed. Granted I'm making an educated guess here before bed without testing but that's how I'd approach it.
That said, you could probably do this very easily with robocopy too if you're open to a not-so-powershell solution.
1
u/overlydelicioustea Jul 13 '26
"$($DestinationPath)/$($_.Name)"
this seems unneccesaary wehn you dont want to rename the items anyway.
just use desitnationpath
3
u/Icy-State5549 Jul 13 '26 edited Jul 13 '26
When you input the destination dir, don't specify the folder name, just the path where you want the folder to land.
-source c:\my\src\folder
-destination c:\my\destination
Should move c:\my\src\folder to c:\my\destination\folder
And not a subfolder named folder. When you specify -force it will create the move-item destination path. Without -force, it will error out way you are specifying the destination.
1
u/dodexahedron Jul 13 '26
That's how moves work on any platform.
If the destination path exists and is of the same type as the source (e.g. both resolve to directories or both resolve to files), the handling is to move source into destination. What that means is:
Move-Item has other parameters more appropriate for what you want to do. Take a look at
Get-Help -Detailed Move-Item.-1
u/ankokudaishogun Jul 13 '26 edited Jul 13 '26
--